Find Min/Max Miles in milesTracker Program

  • C/C++
  • Thread starter ineedhelpnow
  • Start date
  • Tags
    Program
In summary, the programmer was trying to loop through the rows and columns of the milesTracker array, and set the values of minMiles and maxMiles based on whether the value in milesTracker[i][j] was less than or greater than minMiles.
  • #1
ineedhelpnow
651
0
ok so i haven't looked at my programming homework in almost two weeks so I am very confused on how to do this.

Find the maximum value and minimum value in milesTracker. Assign the maximum value to maxMiles, and the minimum value to minMiles. Sample output for the given program:

Min miles: -10
Max miles: 40
Sample program:


Code:
#include <iostream>
using namespace std;

int main( ) {
   const int NUM_ROWS = 2;
   const int NUM_COLS = 2;
   int milesTracker[NUM_ROWS][NUM_COLS];
   int i = 0;
   int j = 0;
   int maxMiles = -99; // Assign 0 before loop
   int minMiles = -99; // Assign 0 before loop

   milesTracker[0][0] = -10; 
   milesTracker[0][1] = 20; 
   milesTracker[1][0] = 30; 
   milesTracker[1][1] = 40;

   <STUDENT CODE>

    cout << "Min miles: " << minMiles << endl;
    cout << "Max miles: " << maxMiles << endl;
}

help please :(
 
Technology news on Phys.org
  • #2
You are going to want to loop through the array...since you know the dimensions of the array, what kind of looping construct do you think is appropriate?
 
  • #3
for?
 
  • #4
ineedhelpnow said:
for?

Correct! (Nod)

Now, since you have two dimensions in the array, you are going to need a for loop nested within another for loop. The outer loop will loop through the rows, and the inner loop will loop through the columns. Can you set up the loops, using the provided indices and dimensions?
 
  • #5
MarkFL said:
Correct! (Nod)

Now, since you have two dimensions in the array, you are going to need a for loop nested within another for loop. The outer loop will loop through the rows, and the inner loop will loop through the columns. Can you set up the loops, using the provided indices and dimensions?

for(i=0;i<NUM_ROWS;++i) is the first loop
for(j=0;i<NUM_COLS;++j) is the second loop

i think that's wrong but I am not sure.
 
  • #6
ineedhelpnow said:
for(i=0;i<NUM_ROWS;++i) is the first loop
for(j=0;i<NUM_COLS;++j) is the second loop

i think that's wrong but I am not sure.

That's correct...I would set them up as follows:

Code:
for (i = 0; i < NUM_ROWS; i++)
{
    for (j = 0; i < NUM_COLS; j++)
    {
        //code goes here
    }
}

Now, we are going to be doing some comparisons, but on the very first value, we have no previous values to compare, so...what do you think we should do on the very first iteration of the loops? In other words:

Code:
for (i = 0; i < NUM_ROWS; i++)
{
    for (j = 0; i < NUM_COLS; j++)
    {
        if (i == 0 && j == 0)
        {
            //first iteration
        }
        else
        {
            //all other iterations
        }
    }
}
 
  • #7
im not sure
 
  • #8
ineedhelpnow said:
im not sure

Well, what I would do is assign the first value to both the max and min values:

Code:
for (i = 0; i < NUM_ROWS; i++)
{
    for (j = 0; i < NUM_COLS; j++)
    {
        if (i == 0 && j == 0)
        {
            minMiles = milesTracker[i][j];
            maxMiles = milesTracker[i][j];
        }
        else
        {
            //all other iterations
        }
    }
}

Now, for all other iterations...what conditions will cause you to change the values of minMiles and maxmiles?
 
  • #9
i was trying this the whole time.
Code:
for(i=0;i<NUM_ROWS;++i){
      for(j=0;i<NUM_COLS;++j){
             if (milesTracker[i][j]<minMiles){
                   minMiles = milesTracker[i][j];
            }
             else if (milesTracker[i][j] > maxMiles){
                   maxMiles = milesTracker[i][j];
            }
   } 
}

- - - Updated - - -

i got it to work. the way i was doing it, i just forgot to initialize them to 0 before starting the loop.

thanks.
 
  • #10
ineedhelpnow said:
i was trying this the whole time.
Code:
for(i=0;i<NUM_ROWS;++i){
      for(j=0;i<NUM_COLS;++j){
             if (milesTracker[i][j]<minMiles){
                   minMiles = milesTracker[i][j];
            }
             else if (milesTracker[i][j] > maxMiles){
                   maxMiles = milesTracker[i][j];
            }
   } 
}

Okay good...I would suggest putting your if/else construct where I have "all other iterations." What you have would work with the values you are given for the array, but it would fail in general. Consider the case where you initialize the min and max values to some value, but then all of the values in the array are greater than that initial value (or all of them are smaller)...your min/max values would never get changed. :D
 
  • #11
ineedhelpnow said:
...i got it to work. the way i was doing it, i just forgot to initialize them to 0 before starting the loop.

thanks.

Yes, but a good coder tries to make his/her code work as generally as possible. ;)
 
  • #12
MarkFL said:
Yes, but a good coder tries to make his/her code work as generally as possible. ;)

:) i posted that before your last post. i agree with you
 
  • #13
ineedhelpnow said:
i was trying this the whole time.
Code:
for(i=0;i<NUM_ROWS;++i){
      for(j=0;i<NUM_COLS;++j){
             if (milesTracker[i][j]<minMiles){
                   minMiles = milesTracker[i][j];
            }
             else if (milesTracker[i][j] > maxMiles){
                   maxMiles = milesTracker[i][j];
            }
   } 
}

- - - Updated - - -

i got it to work. the way i was doing it, i just forgot to initialize them to 0 before starting the loop.

thanks.

on your second for statement make this correction:
Code:
for(j=0;j<NUM_COLS;++j)

this is what it said before:
Code:
for(j=0;i<NUM_COLS;++j)

all i changed was the i and made it j, this completed the code and ran everything.
 

Related to Find Min/Max Miles in milesTracker Program

What is the "Find Min/Max Miles" feature in the milesTracker program?

The "Find Min/Max Miles" feature in the milesTracker program is a function that allows users to easily find the minimum and maximum mileage entered in their mileage log. This can be helpful for tracking progress and identifying any discrepancies in mileage data.

How do I use the "Find Min/Max Miles" feature?

To use the "Find Min/Max Miles" feature, open the milesTracker program and navigate to the mileage log. Then, click on the "Find Min/Max Miles" button. This will generate a report displaying the minimum and maximum mileage entered in the log.

Can I specify a time period for the "Find Min/Max Miles" feature?

Yes, the "Find Min/Max Miles" feature allows users to specify a time period for the mileage data they want to analyze. This can be helpful for tracking progress over a specific time frame or for identifying any changes in mileage patterns.

What if I have multiple vehicles in the milesTracker program?

If you have multiple vehicles in the milesTracker program, the "Find Min/Max Miles" feature will generate a report for each vehicle individually. This allows for more accurate and detailed analysis of mileage data for each vehicle.

Is the "Find Min/Max Miles" feature available in all versions of the milesTracker program?

Yes, the "Find Min/Max Miles" feature is available in all versions of the milesTracker program. It is a standard feature included in the program to help users easily track and analyze their mileage data.

Similar threads

  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
1
Views
975
  • Programming and Computer Science
Replies
4
Views
891
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
2
Replies
35
Views
3K
Back
Top