-1

My program takes the grades of a group of students the rows represent each student, while each column represents each of the exams. I need the average for each row (that is, the average of grades for each student) just as I need the general average for each exam, I do not know how to continue.

import java.util.Scanner;
public class students
{ private Scanner keyboard;
    private double [][]mat;
    public void classroom(){
        keyboard = new Scanner (System.in);
        System.out.println("how many students does your classroom have?");
        //We know that 3 exams were done, but not how many students are in the classroom
        int rows = keyboard.nextInt();
        int columns = 3;
        mat= new double [rows][columns];
        for (int i=0; i<mat.length;i++){
            for (int c=0;c<mat[i].length;c++){
                System.out.println ("enter grades");
                mat [i][c] = keyboard.nextDouble();
            }
        }
    }

    public void print () {
        for(int i=0;i<mat.length;i++) {
            for(int c=0;c<mat[i].length;c++) {
                System.out.print(mat[i][c]+" ");
            }
            System.out.println();
        }
    }


    public static void main (String [] args){
        students e = new students();
        e.classroom();
        e.print();

    }
}
John Kugelman
  • 307,513
  • 65
  • 473
  • 519

3 Answers3

1

This function returns the list of student averages given matrix and row_count as arguments. The index of each student will be maintained in the returned list.

public List<Double> average(double[][] mat, int row_count){
   List<Double> averageList = new ArrayList<>();
   for(int i = 0; i < row_count; i++){
       double averageOfStudent = (mat[i][0] + mat[i][1] + mat[i][2])/3;
       averageList.add(averageOfStudent);
   }
   return averageList;
}
Nandu Raj
  • 1,979
  • 7
  • 19
0

When working with a 2 Dimensional array try to fix one of the dimensions and work with the other one, for example you want the average grades for ONE student! Let's imagine his grades where saved in 1 array, grades! you'd do something like the following:

int sum=0;
int average=0;
for(int i=0;i<grades.length;i++){
    sum+=grades[i];
}
average = sum/grades.length

So now back to our 2-d array all you need to do is fix one of the dimension to get something similar to what we did above!

int sum=0;
int average=0;
for(int i=0;i<grades.length;i++){ //grades[0][0] will indicate the first grade for 
                                    //the first student, so grades[0]x will represent
                                   // all of their grades basically
    sum+=grades[0][i];
}
average = sum/grades.length

Now finally, say you want to apply the above for all of the students! instead of 0 we can use a variable and loop!

int[] sum// make an array of sum so you get a sum per student
int[] average// make an array of average so you get a average per student
for(int i=0;i<grades.length;i++){
    for(int j=0;j<grades[0].length;j++){
        sum[i]=grades[i][j];
    }
}
average[i] = sum[i]/grades.length

For the average of per exam rather than per student is the same but loop horizontally rather than vertically.

Note: You can obviously save the info you need for both averages (per exams and per student) but I advise you to solve it by looping twice to make it clear!

Edit: Another note: I advise you to read more about 2-d arrays as things can get messy/complicated really fast if you don't actually understand what you're dealing with!

Ali_Nass
  • 577
  • 6
  • 18
0

The answer from bleh10 explains the logic, so I won't repeat it here. Nonetheless there are some small details that are either missing or may be misleading which would cause your program not to work as expected, thus leaving you scratching your head as to why.

Firstly, bleh10's answer uses int arrays, when it is clearly stated in your question that grades are double.

Secondly, there are some quirks to be aware of when using Scanner. Refer to this question: Scanner is skipping nextLine() after using next() or nextFoo()?

Finally, I tried to make the below code more "user friendly" by printing appropriate prompts when asking the user to enter values. Also, I changed your print() method so that it displays a table of the results. I presume you may not yet have learned about method printf(), so for an explanation of the formatting strings I used, refer to javadoc for class java.util.Formatter

Here is my complete solution - which started with your original code that I modified. Note that I changed the name of your class according to the Java naming conventions.

import java.util.Scanner;

public class Students {
    private static final int NUMBER_OF_EXAMS = 3;

    private Scanner keyboard;
    private double[][] mat;
    private double[] examAverages = new double[NUMBER_OF_EXAMS];
    private double[] studentAverages;

    public void classroom() {
        keyboard = new Scanner(System.in);
        System.out.print("How many students does your classroom have? ");
        // We know that 3 exams were done, but not how many students are in the classroom.
        int rows = keyboard.nextInt();
        studentAverages = new double[rows];
        mat = new double[rows][NUMBER_OF_EXAMS];
        double[] examTotals = new double[NUMBER_OF_EXAMS]; // each array element implicitly initialized to zero
        double studentTotal = 0.0d;
        for (int i = 0; i < rows; i++) {
            System.out.println("Enter grades for student " + (i + 1));
            studentTotal = 0.0d;
            for (int c = 0; c < mat[i].length; c++) {
                mat[i][c] = getGrade(c);
                studentTotal += mat[i][c];
                examTotals[c] += mat[i][c];
            }
            studentAverages[i] = studentTotal / NUMBER_OF_EXAMS;
        }
        for (int i = 0; i < NUMBER_OF_EXAMS; i++) {
            examAverages[i] = examTotals[i] / rows;
        }
    }

    /**
     * Accepts, from user, a single grade for a single exam for a single student.
     * 
     * @param index - exam index
     * 
     * @return Grade entered by user.
     */
    private double getGrade(int index) {
        if (keyboard.hasNextLine()) {
            keyboard.nextLine();
        }
        System.out.printf("Enter grade %d: ", (index + 1));
        double grade = keyboard.nextDouble();
        return grade;
    }

    /**
     * Displays table of exam results for all students including each student's average grade
     * plus the average result for each exam.
     */
    public void print () {
        // Print column headers for results table.
        System.out.println("            Exam 1  Exam 2  Exam 3  Average");
        for (int i = 0; i < mat.length; i++) {
            System.out.printf("Student %2d: ", (i + 1));
            for(int c = 0; c < mat[i].length; c++) {
                System.out.printf("%6.2f  ", mat[i][c]);
            }
            System.out.printf("%.2f%n", studentAverages[i]);
        }
        System.out.print("Averages:   ");
        for (int i = 0; i < NUMBER_OF_EXAMS; i++) {
            System.out.printf("%6.2f  ", examAverages[i]);
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Students e = new Students();
        e.classroom();
        e.print();
    }
}

Here is the output for a sample run of the above code:

How many students does your classroom have? 2
Enter grades for student 1
Enter grade 1: 70
Enter grade 2: 70
Enter grade 3: 70
Enter grades for student 2
Enter grade 1: 30
Enter grade 2: 30
Enter grade 3: 30
            Exam 1  Exam 2  Exam 3  Average
Student  1:  70.00   70.00   70.00  70.00
Student  2:  30.00   30.00   30.00  30.00
Averages:    50.00   50.00   50.00  
Abra
  • 11,631
  • 5
  • 25
  • 33