Jay
Jay

Reputation: 109

multidimensional arrays and methods

I have a homework assignment and the program generates a random number of students taking a random number of tests. The user must enter the students names and test scores. Once that is done the program will print on the screen : STUDENT REPORT Scores for Fred: Test #1: 78 Test #2: 80 Average for Fred: 79.0 Scores for Sue: Test #1: 91 Test #2: 94 Average for Sue: 92.5

... and so on depending on what number of students and tests you get.

I get everything right up until the average part how can I fix it?

Here's my code:

   public static void populateNames(String[] names)
   {
     for(int i = 0; i<names.length; i++)
     {
       out.print("Enter student " + (i+1) + " name: ");
       names[i] = keyboard.next();
     }
   } 

  // Code your 3 methods below...
 //Ask the user for the student name as well as the test scores
   public static void populateTestScores(String[] names, int[][] scores)
   {
     out.println();
     for(int i=0; i<names.length; i++)
     {
        out.println();
        out.println("Entering scores for: " + names[i]);

        for(int s=0; s<scores[0].length; s++)
        {
            out.print("Enter score for test #" + (s+1) + ": ");
            scores[i][s] = keyboard.nextInt();
        }
    }
    out.println();
}

  //Print each studdent test scores and ther average
  public static void printStudentReport(String[] names, int[][] scores)
  {
    out.println("STUDENT REPORT");
    for(int i=0; i<names.length; i++)
    {
        out.print("Scores for " + names[i] + ": ");
        out.println();

        for(int s=0; s<scores[0].length; s++)
        {
            out.println("Test #" + (s + 1) +  ": " + scores[i][s]);
        }
        out.println();
    int s=0;
    if(s<scores[0].length)
    {
        s++;
        double sum = 0;
        sum += scores[i][s];
        double average = sum / scores[0].length;
        out.println("Average for " + names[i] + ": " + average);
        out.println();
    }
    }
    out.println();
  }

Upvotes: 1

Views: 80

Answers (1)

1337joe
1337joe

Reputation: 2377

The problem is in the way you're computing sum in printStudentReport: it only contains the second element in the list of scores because you're creating and adding to sum after the for loop (instead of creating before the loop and adding in it).

I'd rearrange that method to (reformatted for compactness):

// Print each studdent test scores and ther average
public static void printStudentReport(String[] names, int[][] scores) {
    out.println("STUDENT REPORT");
    for (int i = 0; i < names.length; i++) {
        out.print("Scores for " + names[i] + ": ");
        out.println();

        double sum = 0;
        for (int s = 0; s < scores[0].length; s++) {
            out.println("Test #" + (s + 1) + ": " + scores[i][s]);
            sum += scores[i][s];
        }
        out.println();
        if (0 < scores[0].length) {
            double average = sum / scores[0].length;
            out.println("Average for " + names[i] + ": " + average);
            out.println();
        }
    }
    out.println();
}

Upvotes: 2

Related Questions