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

• November 23rd, 2012, 01:07 AM
rayan2004
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).

Code :

```  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( );     }```
• November 23rd, 2012, 09:50 AM
aesguitar
Re: How to print the same 2D Array Vertically and Horizontally
Couple things,

Quote:

Code java:

``` 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?
• November 23rd, 2012, 10:10 AM
rayan2004
Re: How to print the same 2D Array Vertically and Horizontally

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 ? ?

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

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(); 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( );     }         }     }```
• November 23rd, 2012, 10:22 AM
aesguitar
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) )

Code java:

```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?
• November 23rd, 2012, 10:48 AM
rayan2004
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.
• November 23rd, 2012, 10:51 AM
aesguitar
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?
• November 25th, 2012, 09:03 AM
rayan2004
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. :(

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();   }   }   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 ? ? ? ?
• November 25th, 2012, 03:05 PM
aesguitar
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

Code java:

```//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.

Code :

```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.

Code java:

```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+ " "); }     }```
• November 25th, 2012, 03:06 PM
aesguitar
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

Code java:

```//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.

Code :

```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.

Code java:

```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+ " "); }     }```