can anyone help... 2d Array

• 03-12-2008, 08:22 PM
Mark1989
can anyone help... 2d Array
I am currently having a problem with a question i have for college. I have tried so much and still can only get half my question done can anyone help PLEASE!!

OK so my question is!

"You are required to write a program to monitor the grades of 4 students over 3 subjects. You should model this using a 2 dimensional array. At the end of each row output, you should also output the average grade of all students in that subject. Use random numbers to populate your array with grades between 1 and 100.

The three subjects are
Programming
Networking
Mathematics

The totals should be calculated for each row in order to determine the average grade.

A sample output would thus look like this:

B001 B002 B003 B004

Programing 90 50 100 60 Average: 75

Networking 80 40 80 40 Average: 60

Mathematics 30 60 30 60 Average: 45

Extend your program to include the following:

Determine and calculate the student number and subject of the highest grade across the board.

For example, in the above sample data the output should be

Student B003 scored the highest mark with 100% in Programming."

Now i Can get the table to print out its the second half i just cant get with the highest mark etc..

Code:

```class Arrays{  //opens program       public static void main(String[]args){         int grades[][] = new int[3][4];       arraysone(grades);         }     static void arraysone(int a[][]){     int average=0;     System.out.println("\t\tB001\tB002\tB003\tB004");           ///////////////////////////         for(int r=0;r<3;r++)         {       System.out.println();    // New line for every row       average = 0;       Subject_Name(r);           for(int c=0;c<4;c++)             {                 a[r][c] = (int)(Math.random()* 10000)%100+1;         System.out.print(a[r][c] + "\t");             average = average + a[r][c] / 4;             }       System.out.print(" Average: " + average);       }       /////////////////////////////       System.out.println("\n");   }   ///////////////////////////////////   // Process to add subject   //////////////////////////////////////   static void Subject_Name(int sub){     switch(sub){     case 0:System.out.print("Programming\t");break;     case 1:System.out.print("Networking\t");break;     case 2:System.out.print("Mathematics\t");break;     }   }  } //closes program```
• 03-12-2008, 09:55 PM
hardwired
Code:

```public class ArraysRx {     public static void main(String[]args) {         int grades[][] = new int[3][4];         arraysone(grades);         showHighMark(grades);     }     private static void showHighMark(int[][] grades) {         // grades array info organized by:         // grades[subject][student]         int highestGrade = -1;         int subjectIndex = -1;         int studentIndex = -1;         for(int j = 0; j < grades.length; j++) {             for(int k = 0; k < grades[j].length; k++) {                 if(grades[j][k] > highestGrade) {                     highestGrade = grades[j][k];                     subjectIndex = j;                     studentIndex = k;                 }             }         }         System.out.println("highestGrade = " + highestGrade + "  " +                           " found at grades[" + subjectIndex + "]" +                           "[" + studentIndex + "] = " +                             grades[subjectIndex][studentIndex]);     }     static void arraysone(int a[][]) {         int average=0;         System.out.println("\t\tB001\tB002\tB003\tB004");         for(int r = 0; r < 3; r++) {             System.out.println();    // New line for every row             average = 0;             Subject_Name(r);             for(int c = 0; c < 4; c++) {                 a[r][c] = (int)(Math.random()* 10000)%100+1;                 System.out.print(a[r][c] + "\t");                 average = average + a[r][c] / 4;             }             System.out.print(" Average: " + average);         }         System.out.println("\n");     }     // Method to add subject     static void Subject_Name(int sub){         switch(sub){             case 0: System.out.print("Programming\t"); break;             case 1: System.out.print("Networking\t");  break;             case 2: System.out.print("Mathematics\t"); break;         }     }  }```
• 03-12-2008, 09:59 PM
Mark1989
Wow.. That is perfect thank you so much.

Actually understand it aswell cheers :)