0
for (i = 0, i < NUM_ROWS; i++;){
    for (j = 0, j < NUM_COLS; j++;){ 
        if (milesTracker[i][j] > maxMiles)
            maxMiles = milesTracker[i][j];
            
        if (milesTracker[i][j] < minMiles)
            minMiles = milesTracker[i][j];
    }
}

maxMiles & midMiles both given/initialized at 0 before the nested loop. When I run it (cout << minMiles), it only gives back a zero for either, no matter the inputs.

Where is my logic flawed here? It should go:

  1. Go through each number in the array
  2. First time, is it larger than zero, if so then make it the new number.
  3. Loop, repeat, until over.

Currently having to pay for my sins from CompSci 1 in CompSci 2, so I'm just trying to review what I never learned...

Chris
  • 26,361
  • 5
  • 21
  • 42
  • What part of your code do you believe corresponds to step 2 as described? – Nathan Pierson Sep 15 '22 at 05:38
  • maxMiles = milesTracker[i][j]; ? but that second step shouldn't be there, which was causing the problems with negative numbers. thanks to your comment, it made me realize i should be checking the first number with ITSELF, not 0. :D – SpitfirePls Sep 15 '22 at 05:47

1 Answers1

1

your for loop declarations have mistakes. A for loop requires

for (index; range; increment)

and those segments to be seperated by the semicolon. right now you have a comma-operator in the index clause, meaning both index and the range checks i = 0, i < NUM_ROWS and j = 0, j < NUM_COLS appear in in the first segment, and therefore the < checks are effectively ignored (how does the comma operator work?). So i++ and j++ are now at the range check instead , and those will always result in 0 because i and j, respectively, start at 0.

You should change your for loops to look like this:

for (i = 0; i < NUM_ROWS; i++){
    for (j = 0; j < NUM_COLS; j++){ 
        //...
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
  • oh my GOD i'm about to blow up, yes thank you :( i can't upvote yet since im new, but i will send you best wishes for helping me realize my syntax error. I will think of you whenever I write for loops from now until the end of time ALSO, IT WORKED LOL. i still need to figure out some small logic when it starts as a negative number, but WE'RE GETTING THERE – SpitfirePls Sep 15 '22 at 05:39
  • 1
    Suggestion: Instead of initializing `maxMiles` and `minMiles` to `0`, initialize them to `milesTracker[0][0]`. – Nathan Pierson Sep 15 '22 at 05:41
  • @NathanPierson the [`numeric_limits`](https://en.cppreference.com/w/cpp/types/numeric_limits) header might also be useful for setting initial values. – Chris Sep 15 '22 at 06:08