Find Min/Max Miles in milesTracker Program

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    Program
Click For Summary
SUMMARY

The discussion focuses on finding the minimum and maximum values in a two-dimensional array named milesTracker within a C++ program. The correct implementation involves initializing maxMiles and minMiles to the first element of the array during the first iteration of nested loops. The final solution includes a nested for loop structure that iterates through the array, updating the min and max values based on comparisons with the current element. The corrected loop structure is for(i=0;i and for(j=0;j.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with arrays and their indexing
  • Knowledge of control flow statements, specifically for loops
  • Basic concepts of variable initialization and comparison operators
NEXT STEPS
  • Implement error handling for empty or invalid arrays in C++
  • Explore the use of functions to modularize the min/max calculation
  • Learn about C++ STL algorithms for finding min/max values
  • Investigate performance implications of nested loops in large datasets
USEFUL FOR

C++ programmers, computer science students, and anyone looking to improve their skills in array manipulation and control flow in programming.

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.
 

Similar threads

Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K