C/C++ Find Min/Max Miles in milesTracker Program

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Program
AI Thread Summary
The discussion centers around solving a programming homework problem that involves finding the maximum and minimum values in a two-dimensional array called milesTracker. The user expresses confusion after not reviewing the assignment for two weeks. The solution involves using nested for loops to iterate through the array, with the outer loop for rows and the inner loop for columns. Initially, the user mistakenly initializes the loop variables, but guidance is provided to correct this. It is emphasized that on the first iteration, both maxMiles and minMiles should be set to the first value in the array. The user successfully implements the logic to compare values and update minMiles and maxMiles accordingly. A critical point raised is the importance of initializing these variables correctly to ensure the code works under all conditions, not just with specific input values. The final solution is confirmed to work after correcting the loop initialization.
ineedhelpnow
Messages
649
Reaction score
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
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?
 
for?
 
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?
 
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.
 
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
        }
    }
}
 
im not sure
 
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?
 
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.
 
Back
Top