Issue
I am working on this program to find minium and maximum values of the variable milesTracker and have been successful for a few tests but it breaks testing it with values {{-5}, {-93}, {-259}}.
Note: I cannot edit anything outside of the commented area, however, I can add more lines anywhere within the commented area.
I'd appreciate the help.
My code:
import java.util.Scanner;
public class ArraysKeyValue {
public static void main (String [] args) {
final int NUM_ROWS = 2;
final int NUM_COLS = 2;
int [][] milesTracker = new int[NUM_ROWS][NUM_COLS];
int i = 0;
int j = 0;
int maxMiles = 0; // Assign with first element in milesTracker before loop
int minMiles = 0; // Assign with first element in milesTracker before loop
milesTracker[0][0] = -10;
milesTracker[0][1] = 20;
milesTracker[1][0] = 30;
milesTracker[1][1] = 40;
// Start of area to be modified
for(i=0;i<NUM_ROWS;++i){
for(j=0;j<NUM_COLS;++j){
if (milesTracker[i][j]<minMiles){
minMiles = milesTracker[i][j];
}
else if (milesTracker[i][j] > maxMiles){
maxMiles = milesTracker[i][j];
}
}
}
// End of area to be modified
System.out.println("Min miles: " + minMiles);
System.out.println("Max miles: " + maxMiles);
}
}
Errors:
Solution
// Start of area to be modified
maxMiles = milesTracker[0][0];
minMiles = milesTracker[0][0];
for(i=0; i < NUM_ROWS; i++){
for(j=0; j < NUM_COLS; j++){
if (milesTracker[i][j] < minMiles){
minMiles = milesTracker[i][j];
}
else if (milesTracker[i][j] > maxMiles){
maxMiles = milesTracker[i][j];
}
}
}
Answered By - Rumado