# Problem finding MIN value of 2D arry.

• November 22nd, 2012, 12:53 AM
rayan2004
Problem finding MIN value of 2D arry.
Hi Guys

I'm trying to find max and min values of 2D array. I managed to get the right out put for MAX, But MIN value does not come correct.

Regards
Rayan

Code :

```  import java.util.*;   class sq{ public static void main(String[] args) { Scanner input = new Scanner(System.in);   System.out.print("Input No of student : "); int st = input.nextInt(); System.out.print("Input No of subjects : "); int su = input.nextInt();   int [][] marks = new int[st][su];     for (int i = 0; i < marks.length; i++){ System.out.println("Input marks for student "+i+":" );   for (int j = 0; j < marks[i].length; j++){   System.out.print("subect "+j+":");   marks[i][j] = input.nextInt();   }   }       for (int m = 0; m < marks.length; m++) { int max = marks[0][0]; int min = marks[0][0];   for (int n = 0; n < marks[m].length; n++){   if (max < marks[m][n]){   max = marks[m][n];       if (min > marks[m][n]){   min = marks[m][n]; }   System.out.print(marks[m][n]+" "); } System.out.print(" " + max + " " + min ); System.out.println( );     }     }   }```
• November 22nd, 2012, 06:24 AM
curmudgeon
Re: Problem finding MIN value of 2D arry.
You're declaring your min and max variables inside the outermost of your nested for loops, and so the are re-declared with each iteration of that loop -- not good. Instead why not declare both of these variables before the nested for loops, specifically right before this line: for (int m = 0; m < marks.length; m++)
• November 22nd, 2012, 06:36 AM
rayan2004
Re: Problem finding MIN value of 2D arry.
Quote:

Originally Posted by curmudgeon
You're declaring your min and max variables inside the outermost of your nested for loops, and so the are re-declared with each iteration of that loop -- not good. Instead why not declare both of these variables before the nested for loops, specifically right before this line: for (int m = 0; m < marks.length; m++)

I already tried declaring before that line. but still i didnt get correct result. :( :(
• November 22nd, 2012, 06:38 AM
curmudgeon
Re: Problem finding MIN value of 2D arry.
Quote:

Originally Posted by rayan2004
I already tried declaring before that line. but still i didnt get correct result. :( :(

Please do show this attempt. Let me see how you do it. Also, only print out max and min after both nested loops are complete.