java2019
java2019

Reputation: 75

Find 2D array max and min

This is the original prompt:

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

Here is 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; 
    int minMiles = 0; 

    milesTracker[0][0] = -10;
    milesTracker[0][1] = 20;
    milesTracker[1][0] = 30;
    milesTracker[1][1] = 40;

    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];
          }
       } 
    }

    System.out.println("Min miles: " + minMiles);
    System.out.println("Max miles: " + maxMiles);
  }
}

Here is the output:

  Testing with milesTracker = {{-10, 20}, {30, 40}}

Your output:

Min miles: -10
Max miles: 40

   Testing with milesTracker = {{73, 0}}

Your output:

Min miles: 0
Max miles: 73

✖   Testing with milesTracker = {{-5}, {-93}, {-259}}

Expected output:

Min miles: -259
Max miles: -5

Your output:

Min miles: -259
Max miles: 0

Why is the last test failing?

Upvotes: 1

Views: 42200

Answers (9)

Ahmed De Alba
Ahmed De Alba

Reputation: 1

If you set up your maxMiles as a very small negative (unrealistic input) number and a large minMiles, basically any number will satisfy the condition to become the next max/min and as iterations go, you will eventually find a true max and min.

So, I am doing this exact same problem right now as homework and I came up with this:

  maxMiles = -9999;
  minMiles = 9999;
  for (i = 0; i < milesTracker.length; i++){
     for (j = 0; j < milesTracker[i].length; j++){
        if (milesTracker[i][j] < minMiles){
           minMiles = milesTracker[i][j];
        }
        if (milesTracker[i][j] > maxMiles) {
           maxMiles = milesTracker[i][j];  
        }
        
     }

Upvotes: 0

Soul
Soul

Reputation: 23

For the confused people who just want to copy paste the final answer -

/* Your solution goes here  */
maxMiles = milesTracker[0][0];
minMiles = milesTracker[0][0];


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

Worked for me :)

Upvotes: 0

Cosmopus
Cosmopus

Reputation: 3

The answer of @Filkolev is really good, but you can be even shorter:

//The solution requires students to initialize max_miles (or maxMiles) or and min_miles (or minMiles) before writing their nested for loop. 
maxMiles = milesTracker[0][0];
minMiles = milesTracker[0][0];

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

Upvotes: 0

jam
jam

Reputation: 1

Assign with first element in milesTracker before loop

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] > maxMiles) {
            maxMiles = milesTracker[i][j];            
        }
        if (milesTracker[i][j] < minMiles) {
           minMiles = milesTracker[i][j];  
        }
    }       
}

Upvotes: 0

Mikes
Mikes

Reputation: 1

Insert array coordinates before you start your for loops:

EX:

minMiles=milesTracker[0][0];
maxMiles=milesTracker[0][0];

for(i...){max
    for(j...){min

Upvotes: -1

Lubrious
Lubrious

Reputation: 21

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

        if (milesTracker[i][j] > maxMiles) {
            maxMiles = milesTracker[i][j];
        }

        else if (milesTracker[i][j] < minMiles) {
            minMiles = milesTracker[i][j];
        }

    }
}

Upvotes: 1

codeN00b
codeN00b

Reputation: 1

Here is what I used.

 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;

      /* Your solution goes here  */
      for (i = 0; i < NUM_ROWS; i++) {

         for (j = 0; j < NUM_COLS; j++) {

            if (i == 0 && j == 0) {
               maxMiles = milesTracker[i][j];
               minMiles = milesTracker[i][j];
            }

               if (milesTracker[i][j] > maxMiles) {
                  maxMiles = milesTracker[i][j];
               }

               else if (milesTracker[i][j] < minMiles) {
                 minMiles = milesTracker[i][j];
               }

            }

         }

      System.out.println("Min miles: " + minMiles);
      System.out.println("Max miles: " + maxMiles);
   }
}

Upvotes: 0

choeger
choeger

Reputation: 3567

As an addition to @Fikolev's answer: If you are only allowed to edit the loop bodies, you can move the initialization there:

for(i=0;i<NUM_ROWS;++i) {
  for(j=0;j<NUM_COLS;++j) {
    if (i == 0 && j == 0) {
      maxMiles = Integer.MIN_VALUE; 
      minMiles = Integer.MAX_VALUE; 
    }
  ...

Upvotes: 1

Filkolev
Filkolev

Reputation: 460

You need to initialize your initial values differently.

int maxMiles = Integer.MIN_VALUE; 
int minMiles = Integer.MAX_VALUE; 

Initializing them both to 0 leads to problems as you can see in the example with only negative values in the matrix. If you have only positive numbers, minMiles will stay 0 and will be wrong as you'll never get a value smaller than the initial 0.

For minValue you need to make sure that whatever you have in the array is smaller than the initial value, hence you assign it the maximal possible value of the type you use. For maxValue it's the opposite.

Another possible error is the else if condition.

if (milesTracker[i][j] < minMiles) {
    minMiles = milesTracker[i][j]; 
} else if (milesTracker[i][j] > maxMiles) {
    maxMiles = milesTracker[i][j];
}

These two are not mutually exclusive. What if you reach a number that is both larger than your current maxMiles and smaller than the current minMiles? It can certainly happen and you'll fail to update one of them, in your case - maxMiles.

Upvotes: 5

Related Questions