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

Discussion Overview

The discussion revolves around a programming homework problem that involves finding the minimum and maximum values in a two-dimensional array called milesTracker. Participants explore how to implement the logic for iterating through the array and updating the min and max values based on the contents of the array.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant expresses confusion about how to approach the problem after not reviewing it for some time.
  • Another participant suggests using a loop to iterate through the array, prompting a discussion about the appropriate looping constructs.
  • There is a suggestion to use nested for loops to handle the two-dimensional array structure.
  • One participant questions the correctness of their loop setup, indicating uncertainty about the loop indices.
  • A participant proposes initializing minMiles and maxMiles to the first value in the array during the first iteration of the loops.
  • Another participant shares their implementation of the loop and the conditions for updating minMiles and maxMiles.
  • One participant notes that they initially forgot to initialize minMiles and maxMiles to 0, which caused issues in their code.
  • There is a suggestion to ensure that the code works generally, regardless of the values in the array.
  • A later reply corrects a mistake in the loop condition from 'i' to 'j', which resolved the issue in the code.

Areas of Agreement / Disagreement

Participants generally agree on the need for nested loops to iterate through the array and the importance of initializing min and max values correctly. However, there are differing views on the best practices for ensuring the code works under various conditions, and some uncertainty remains about the initial setup of loop indices.

Contextual Notes

There are unresolved issues regarding the initialization of min and max values and the implications of their initial values on the correctness of the code. Additionally, there are concerns about the robustness of the proposed solutions in different scenarios.

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