Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem finding MIN value of 2D arry.

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    19
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question 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.
    Please help me. my code is below -

    Regards
    Rayan

     
    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( );
     
     
    	          }
     
     
          }
     
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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++)

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    19
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Problem finding MIN value of 2D arry.

    Quote Originally Posted by curmudgeon View Post
    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.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Problem finding MIN value of 2D arry.

    Quote Originally Posted by rayan2004 View Post
    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.

Similar Threads

  1. New to Java: Arry of Objects
    By Jack Hammered in forum Collections and Generics
    Replies: 8
    Last Post: January 5th, 2012, 07:17 AM
  2. Help, Building List & finding min and max
    By Margaret_Girl87 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 30th, 2011, 07:15 PM
  3. Replies: 5
    Last Post: May 24th, 2011, 10:39 AM
  4. Help with Finding Max and Min Values of ArrayList
    By CheekySpoon in forum Collections and Generics
    Replies: 3
    Last Post: March 2nd, 2011, 08:57 PM
  5. Finding min in linked list
    By lieles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2011, 02:06 AM

Tags for this Thread