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 9 of 9

Thread: How to print the same 2D Array Vertically and Horizontally

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

    Unhappy How to print the same 2D Array Vertically and Horizontally

    Hi I have 2D array program prints students marks and next total, grade, max and min.
    I Managed to prints total Max..etc Horizontally. (A)

    A) Student markes Total Max Min Avg Grade


    But i also want to calculate grades , total , max , min vertically (B). Please help me to understand how can i print Total, Max..etc vertically as on (B). In other words, Total, Max..etc should read and prints horizontally and vertically same time.

    B) Student markes Total Max Min Avg Grade




    Total

    Max

    Min


    I used these 2 for loops to print Array and Total, Max etc Horizontally as on (A).

     
    for (int m = 0; m < marks.length; m++)
    			{		
     
     
    					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];
    					 }
     
    						total += marks[m][n];
     
    						avg = total/marks[m].length;
     
     
    						if (avg >= 75){
    						grade = 'A';
    					    }
    						else if ((avg >= 65) & (avg < 75)){
    						 grade= 'B';
    						}   
    						else if ((avg >= 55)& (avg < 65)){
    						grade = 'C';
    					    }
    						else if((avg >= 35)& (avg < 55)){
    						grade= 'D';
    					    }
    					    else{
    						grade = 'F';
    					    }
     
    						System.out.print(marks[m][n]+ "  ");
     
    					}
    					System.out.print("\t"+max + "\t" + min+ "\t");
    					System.out.print(total +"\t"+ avg + "\t"+ grade);
    					System.out.println( );
     
     
    	          }


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: How to print the same 2D Array Vertically and Horizontally

    Couple things,

                                                if (avg >= 75){
    						grade = 'A';
    					    }
    						else if ((avg >= 65) & (avg < 75)){
    						 grade= 'B';
    						}   
    						else if ((avg >= 55)& (avg < 65)){
    						grade = 'C';
    					    }
    						else if((avg >= 35)& (avg < 55)){
    						grade= 'D';
    					    }
    					    else{
    						grade = 'F';
    					    }
    Why have it check if the avg is lower than the score above it if the only way to get there is if the avg is less than the next? If your grade is 69 then it will fail being >= 75 but pass >= 65. All I'm saying is you can shorten your if/then/else statements a bit.

    In order to help, can you provide a bit more of the program? Or better yet, explain what your code is doing using comments or a more thorough explanation of the problem you're having. Is it printing the way you want it too? Are all the values that are printed correct? Are you getting errors, if so, what errors/exceptions are you getting?

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

    Default Re: How to print the same 2D Array Vertically and Horizontally

    Hi, Thank you for your reply.

    I managed to write above program to find min max avg and grade horizontally. ----->
    Now i need to print min max avg grade vertically.

    student marks min max avg grade
    10 29 45 54 10 54 34 f
    54 27 43 54 10 54 34 F
    32 54 67 54 10 54 34 F
    14 29 45 54 10 54 34 F
    min ?

    max ? ?

    avg ? ?

    grade ? ?


    Here is the full code for program to print max, min .. horizontally.

     
    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();
    		    char space = ' ';
    			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();	
     
     
    				}
     
    			}
     
     
    			System.out.println("Student Marks     Max    Min   Total   Avrg   Grade");
     
    			    int max = marks[0][0];
    			    int min = marks[0][0];
    				int total = 0;
    				int avg = 0;
    			    char grade = '0';
     
    			for (int m = 0; m < marks.length; m++)
    			{		
     
     
    					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];
    					 }
     
    						total += marks[m][n];
     
    						avg = total/marks[m].length;
     
     
    						if (avg >= 75){
    						grade = 'A';
    					    }
    						else if ((avg >= 65) & (avg < 75)){
    						 grade= 'B';
    						}   
    						else if ((avg >= 55)& (avg < 65)){
    						grade = 'C';
    					    }
    						else if((avg >= 35)& (avg < 55)){
    						grade= 'D';
    					    }
    					    else{
    						grade = 'F';
    					    }
     
    						System.out.print(marks[m][n]+ "  ");
     
    					}
    					System.out.print("\t"+max + "\t" + min+ "\t");
    					System.out.print(total +"\t"+ avg + "\t"+ grade);
    					System.out.println( );
     
     
    	          }
     
     
     
     
    		}
     
     
     }

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: How to print the same 2D Array Vertically and Horizontally

    Oh, OK. 2-d arrays are fun, aren't they? Anyways, this loop prints the contents of a 2-d array with horizontal priority ( (0,0 - 0,x) - (x,0 - x,x) )

    public class tester {
     
    	/**
    	 * @param args
    	 */
     
     
    	public static void main(String[] args){
     
     
    		int[][] arr = new int[10][10];
    		for(int x = 0; x < 10; x++)
    		{
    			for(int y = 0; y < 10; y++)
    			{
    				arr[x][y] = 2+x+y;
    			}
    		}
    		//loop goes through each y-value that corresponds with the x-value
    		for(int x = 0; x < 10; x++)//loops through x-values
    		{
    			for(int y = 0; y < 10; y++)//loops through y-values
    			{
    				System.out.print(arr[x][y]+"	");
    			}
    			System.out.println();
    		}
    	}
    }

    Now, in order to do it vertically, what variables would you have to flip around to make it iterate through with vertical priority?

  5. The Following User Says Thank You to aesguitar For This Useful Post:

    rayan2004 (November 24th, 2012)

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

    Default Re: How to print the same 2D Array Vertically and Horizontally

    Im new to Java and programming. Im struggling with this program around 2 days.
    As i can understand, i need to flip x and y loops. I tried that with my program. But i couldnt get expected out put vertically under each students marks. Could you please give me bit more explanation with my program. Thank you in advance.

  7. #6
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: How to print the same 2D Array Vertically and Horizontally

    OK, the more I know, the more I can help. Can you give me the output of what you tried versus what you expected?

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

    Default Re: How to print the same 2D Array Vertically and Horizontally

    Hi Again,
    I took little break from this program. I Managed to get total horizontally. But still im having incorrect figures on my programs. Min value repeats. Also i want to get min max avg vertically. i marked it with ? on below out put.
    Im very new to programming. So still struggling with this small program.
    I really appreciate your help.

     
    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();	
     
    				}
     
    			}
     
    			System.out.println("Student Marks     Max    Min   Total   Avrg   Grade");
     
    			    int max = marks[0][0];
    			    int min = marks[0][0];
    			    int max2 = marks[0][0];
    			    int min2 = marks[0][0];
    				int avg = 0;
    			    char grade = '0';
     
    			for (int m = 0; m < marks.length; m++)
    			{		
    					int total = 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];
    						 }
     
    							total += marks[m][n];
     
    							avg = total/marks[m].length;
     
     
    							if (avg >= 75){
    							grade = 'A';
    							}
    							else if ((avg >= 65) & (avg < 75)){
    							 grade= 'B';
    							}   
    							else if ((avg >= 55)& (avg < 65)){
    							grade = 'C';
    							}
    							else if((avg >= 35)& (avg < 55)){
    							grade= 'D';
    							}
    							else{
    							grade = 'F';
    							}
     
    						System.out.print(marks[m][n]+ "   ");
     
    					}
    					System.out.print("\t"+max + "\t" + min+ "\t");
    					System.out.print(total +"\t"+ avg + "\t"+ grade);
    					System.out.println( );
     
     
    	          }
     
     // ------------------------------------------------------------------------------------------------
     
     		for (int m = 0; m < st; m++)
    			{		
    					int total2 = 0;		
    					for (int n = 0; n < su; n++){
     
     
    					/*	 if (max2 < marks[n][m]){	 
    							 max2 = marks[n][m];
    						 }
     
    						 if (min2 > marks[n][m]){
    							min2 = marks[n][m];
    						 }
     
                           */
    							total2 = total2 + marks[n][m];
     
    					}
     
    					System.out.print(total2 + "  " );//  + "  " + max2 + "  "+ min2+ "  ");
    	          }
     
     
    		}
     
     }


    Input marks for student 0:
    subect 0:60
    subect 1:50
    subect 2:70
    Input marks for student 1:
    subect 0:80
    subect 1:70
    subect 2:60
    Input marks for student 2:
    subect 0:50
    subect 1:70
    subect 2:8
    Student Marks Max Min Total Avrg Grade
    60 50 70 70 50 180 60 C
    80 70 60 80 50 210 70 B
    50 70 8 80 8 128 42 D
    190 190 138 ? ? ? ?

  9. #8
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: How to print the same 2D Array Vertically and Horizontally

    Well, the great thing with java, is there is a lot of programs you can use to find stuff like this. java.lang.Math can easily do mins and maxes.

    so for instance

    //something like this
     
    int max = 0, min = 100;
    int num = /*a value*/;
     
    if(num < min){
         min = num;
    }
     
    if(num > max){
         max = num;
    }
     
    //becomes
     
    int max = 0, min = 100;
    int num = /*a value*/
     
    max = Math.max(max, num);
    min = Math.min(min, num);
     
    /**
      * It's just a bit faster and easier
      * and should alleviate some of the errors you are getting
    **/

    I ran your code, with the same test params. I will start the inccorect values.

    Input No of student : 3
    Input No of subjects : 3
    Input marks for student 0:
    subect 0:60
    subect 1:50
    subect 2:70
    Input marks for student 1:
    subect 0:80
    subect 1:70
    subect 2:60
    Input marks for student 2:
    subect 0:50
    subect 1:70
    subect 2:8
    Marks			Max	Min	Total	Avrg	Grade
    60	50	70	70	50	180	60	C
    80	70	60	80	*50	210	70	B
    50	70	8	*80	8	128	42	D
    190	80	50	190	80	50	138	80	8

    You can try moving where where you actually initiate the variables in to the following for loop. As for the vertical calculations, you didn't finish it and you didn't put comments into your code, so until you explain what you don't understand with the vertical portion or add comments so that it is easier to follow your logic, there isn't much I can do other than help fix your existing code.

    I marked what I saw was wrong or poor. You can make changes where and if you want.

    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();	
     
    					}
     
    				}
    		     	//close the scanner after you're done with it.
    		     	input.close();
     
    				System.out.println("Marks			Max	Min	Total	Avrg	Grade");//hit the tab key or use the \t command here, it's neater than pressing space.
     
    				    int max = marks[0][0];//initiate them here
    				    int min = marks[0][0];
    				    int max2 = marks[0][0];
    				    int min2 = marks[0][0];
    					int avg = 0;
    				    char grade = '0';
     
    				for (int m = 0; m < marks.length; m++)
    				{		//reset them each time here.
    						int total = 0;		
    						for (int n = 0; n < marks[m].length; n++){
     
    							 if (max < marks[m][n]){	 //max = Math.max(max, marks[m][n]);
    								 max = marks[m][n];
    							 }
     
    							 if (min > marks[m][n]){	//min = Math.min(min, marks[m][n]);
    								min = marks[m][n];
    							 }
     
    								total += marks[m][n];
     
    								avg = total/marks[m].length;//change marks[m].length to su. It's easier to follow.
     
     
    								if (avg >= 75){
    								grade = 'A';
    								}
    								else if ((avg >= 65) & (avg < 75)){//don't need & (avg<75)
    								 grade= 'B';
    								}   
    								else if ((avg >= 55)& (avg < 65)){//don't need (avg<65) same for the next one
    								grade = 'C';
    								}
    								else if((avg >= 35)& (avg < 55)){
    								grade= 'D';
    								}
    								else{
    								grade = 'F';
    								}
     
    							System.out.print(marks[m][n]+ "	");
     
    						}
    						System.out.print(max + "\t" + min+ "\t");
    						System.out.print(total +"\t"+ avg + "\t"+ grade);
    						System.out.println( );
     
     
    		          }
     
    	 // ------------------------------------------------------------------------------------------------
    	 /**
    	  * Can't help here until you finish or explain what exactly you don't understand
    	  * I'm willing to help, but you gotta help me help you.
    	  */
    	 		for (int m = 0; m < st; m++)
    				{		
    						int total2 = 0;		
    						for (int n = 0; n < su; n++){
     
     
    							 if (max2 < marks[n][m]){	 
    								 max2 = marks[n][m];
    							 }
     
    							 if (min2 > marks[n][m]){
    								min2 = marks[n][m];
    							 }
     
     
    								total2 += marks[n][m];
    								System.out.print(marks[m][n]+ "	");
     
    						}
     
    						System.out.println();
    						System.out.print(total2  + "	" + max2 + "	"+ min2+ "	");
    		          }
     
     
    			}

  10. #9
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: How to print the same 2D Array Vertically and Horizontally

    Well, the great thing with java, is there is a lot of programs you can use to find stuff like this. java.lang.Math can easily do mins and maxes.

    so for instance

    //something like this
     
    int max = 0, min = 100;
    int num = /*a value*/;
     
    if(num < min){
         min = num;
    }
     
    if(num > max){
         max = num;
    }
     
    //becomes
     
    int max = 0, min = 100;
    int num = /*a value*/
     
    max = Math.max(max, num);
    min = Math.min(min, num);
     
    /**
      * It's just a bit faster and easier
      * and should alleviate some of the errors you are getting
    **/

    I ran your code, with the same test params. I will star the inccorect values.

    Input No of student : 3
    Input No of subjects : 3
    Input marks for student 0:
    subect 0:60
    subect 1:50
    subect 2:70
    Input marks for student 1:
    subect 0:80
    subect 1:70
    subect 2:60
    Input marks for student 2:
    subect 0:50
    subect 1:70
    subect 2:8
    Marks			Max	Min	Total	Avrg	Grade
    60	50	70	70	50	180	60	C
    80	70	60	80	*50	210	70	B
    50	70	8	*80	8	128	42	D
    190	80	50	190	80	50	138	80	8

    You can try moving where where you actually initiate the variables in to the following for loop. As for the vertical calculations, you didn't finish it and you didn't put comments into your code, so until you explain what you don't understand with the vertical portion or add comments so that it is easier to follow your logic, there isn't much I can do other than help fix your existing code.

    I marked what I saw was wrong or poor. You can make changes where and if you want.

    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();	
     
    					}
     
    				}
    		     	//close the scanner after you're done with it.
    		     	input.close();
     
    				System.out.println("Marks			Max	Min	Total	Avrg	Grade");//hit the tab key or use the \t command here, it's neater than pressing space.
     
    				    int max = marks[0][0];//initiate them here
    				    int min = marks[0][0];
    				    int max2 = marks[0][0];
    				    int min2 = marks[0][0];
    					int avg = 0;
    				    char grade = '0';
     
    				for (int m = 0; m < marks.length; m++)
    				{		//reset them each time here.
    						int total = 0;		
    						for (int n = 0; n < marks[m].length; n++){
     
    							 if (max < marks[m][n]){	 //max = Math.max(max, marks[m][n]);
    								 max = marks[m][n];
    							 }
     
    							 if (min > marks[m][n]){	//min = Math.min(min, marks[m][n]);
    								min = marks[m][n];
    							 }
     
    								total += marks[m][n];
     
    								avg = total/marks[m].length;//change marks[m].length to su. It's easier to follow.
     
     
    								if (avg >= 75){
    								grade = 'A';
    								}
    								else if ((avg >= 65) & (avg < 75)){//don't need & (avg<75)
    								 grade= 'B';
    								}   
    								else if ((avg >= 55)& (avg < 65)){//don't need (avg<65) same for the next one
    								grade = 'C';
    								}
    								else if((avg >= 35)& (avg < 55)){
    								grade= 'D';
    								}
    								else{
    								grade = 'F';
    								}
     
    							System.out.print(marks[m][n]+ "	");
     
    						}
    						System.out.print(max + "\t" + min+ "\t");
    						System.out.print(total +"\t"+ avg + "\t"+ grade);
    						System.out.println( );
     
     
    		          }
     
    	 // ------------------------------------------------------------------------------------------------
    	 /**
    	  * Can't help here until you finish or explain what exactly you don't understand
    	  * I'm willing to help, but you gotta help me help you.
    	  */
    	 		for (int m = 0; m < st; m++)
    				{		
    						int total2 = 0;		
    						for (int n = 0; n < su; n++){
     
     
    							 if (max2 < marks[n][m]){	 
    								 max2 = marks[n][m];
    							 }
     
    							 if (min2 > marks[n][m]){
    								min2 = marks[n][m];
    							 }
     
     
    								total2 += marks[n][m];
    								System.out.print(marks[m][n]+ "	");
     
    						}
     
    						System.out.println();
    						System.out.print(total2  + "	" + max2 + "	"+ min2+ "	");
    		          }
     
     
    			}
    Last edited by aesguitar; November 25th, 2012 at 04:07 PM. Reason: spell check

Similar Threads

  1. How do I print my 2d array?
    By dunnage888 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 14th, 2012, 06:24 PM
  2. How tp printing array elements vertically through loop.
    By bikes_28 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 6th, 2012, 01:51 PM
  3. Replies: 1
    Last Post: August 5th, 2012, 09:28 PM
  4. Applet array paint/print question
    By allhuber in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 30th, 2012, 08:42 AM
  5. Print array with commas
    By GoPro in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 27th, 2012, 12:41 AM

Tags for this Thread