How do I to print out the average of each each row of a 2D array separately?

The assignment I have is to find the average of the temps in each row of the array. There are 3 rows. I have to find the average of the 3 rows separately, not together, and then print out the averages like so:

Avg for student 1 is x
Avg for students 2 is A
Avg for student 3 is h.

This is the code I have right now. I need to find a way to find the averages separately and print them the way above. I can’t seem to do that. Can anyone help me, please?

This is the actual assignment paper:

Name this program testScoresArray. Write a program that stores the following temps in a 2D array.

Student 1: 100, 80, 93
Student 2: 74, 83, 91, 87
Student 3: 94, 98

Find the average temp for EACH student separately. Hint: you need to keep track of the score but after every time it loops through a column and finds the average, you need to set the value of the score back to 0 or it will count the previous scores!


Average temp for Student 1: 91.0
Average temp for Student 2: 83.75
Average temp for Student 3: 96.0 
public class testScoresArray {
    public static void main(String args[]) {
        int[][] temps = {{100, 80, 93}, {74, 83, 91, 87}, {94, 98}};
        for (int i = 0; i < temps.length; i++) {
            int sum = 0;
            for (int g = 0; g < temps[i].length; g++) {
                sum += temps[i][g];
            System.out.println(sum / temps[i].length);


Not the best, but the easiest solution would be to simply change the type of sum to float or double

float sum = 0;

This will make the division that happens on the println to change from integer division to a floating point one, if only one of the operands has a floating point then your print will have a dot

Leave a Reply

Your email address will not be published. Required fields are marked *