Results 1 to 3 of 3
  1. #1
    Mark1989 is offline Member
    Join Date
    Nov 2007
    Location
    Dublin, Ireland
    Posts
    12
    Rep Power
    0

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


    Java 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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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;
            }
        }
     }

  3. #3
    Mark1989 is offline Member
    Join Date
    Nov 2007
    Location
    Dublin, Ireland
    Posts
    12
    Rep Power
    0

    Default

    Wow.. That is perfect thank you so much.

    Actually understand it aswell cheers :)
    Theres 10 types of people in this world, Those who understand binary and those who dont

Similar Threads

  1. Array Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 02:25 AM
  2. Would appreciate your help with 2d Array..
    By cloudkicker in forum New To Java
    Replies: 1
    Last Post: 02-11-2008, 02:34 PM
  3. 2D array
    By bluekswing in forum New To Java
    Replies: 2
    Last Post: 01-15-2008, 05:57 PM
  4. Bounded Array
    By bugger in forum New To Java
    Replies: 4
    Last Post: 01-04-2008, 09:41 AM
  5. Help with Array
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •