2

I want to output max and min value of 2d array. Max works well, but min always outputs zero even when theres no zeros in array.I set Math.random() to 99 to prevent smaller chance to get zero in array for this example. Heres full code:

public class e {

public static void main(String[] args)   {

    int a[][] = new int [5][5];
    int l = a[0][0];
    int m = a[0][0];
    int i,j,r,k;


    for(i=0;i<a.length;i++)                   // 
        for(j=0;j<a[i].length;j++){           // 2d array random number generator
            a[i][j] =(int)(Math.random()*99); //
         }
    for(i=0;i<a.length;i++){               //
        for(j=0;j<a[i].length;j++)         //
                                           // create 2d array and output it
    System.out.print(a[i][j] + "\t");      //   
    System.out.println();                  //

}
    System.out.println("\t"); 
        for(r=0;r<a.length;r++){           //
            for(k=0;k<a.length;k++)        //
                if(a[r][k] < m){           // finds a min value
                    m = a[r][k];           //

            }
        }

    System.out.println("\t");               // 
        for(i=0;i<a.length;i++){            //
            for(j=0;j<a.length;j++)         // finds a max value
                if(a[i][j] > l){            //
                    l = a[i][j];            //

            }
        }
    System.out.println("min value is " + m); //outputs min value
    System.out.println("max value is " + l); // outputs max value
            }
       }
4
  • 2
    There may be no zeros in your array after you change the values to random numbers, but there certainly are zeros in your array when you initialize m with int m = a[0][0]; which is why m remains 0 when your print it. Commented Apr 4, 2015 at 22:45
  • @jedwards it wasn't the problem, in m = a[0][0] it just define first integer in 2d array Commented Apr 4, 2015 at 23:04
  • it absolutely was the problem. When you initialize m there, a[0][0] is 0. So when you run your loop, you never find a value less than m, so m remains unchanged as 0. By using Andy's solution (which is correct), you're reassigning m to be an arbitrary value in the matrix, allowing your loop to correctly find the minimum value. Commented Apr 4, 2015 at 23:22
  • 1
    @jedwards ou now I get it, my mistake, now I understand it, thank you for your response and for explaining to me Commented Apr 4, 2015 at 23:29

1 Answer 1

4

Because of the way you choose the random values in a, there will be no value less than zero - but there is also no guarantee that any of the values will be exactly zero. However, you initialize m to be zero, since that is the default value of the array elements; nothing can be smaller than this, so the answer is always zero.

You should initialize your m = a[0][0] immediately before you start the outer for loop in the block labelled "finds a min value", i.e.

    m = a[0][0];
    for(r=0;r<a.length;r++){           //
        for(k=0;k<a.length;k++)        //
            if(a[r][k] < m){           // finds a min value
                m = a[r][k];           //

        }
    }

Alternatively, you can set m = Integer.MAX_VALUE (and l = Integer.MIN_VALUE), since these are guaranteed to have values smaller and larger than them, respectively.

Sign up to request clarification or add additional context in comments.

2 Comments

you were right, but can you explane me why it work with max, but didn't work for min?
Your random values are in the range 0..99, so even in the highly unlikely event that all of the random values are set to zero, the initial value of l is less than or equal to the actual maximum.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.