Consider a 10 x 5 matrix where each row stores five (5) test scores for a studen
ID: 3860421 • Letter: C
Question
Consider a 10 x 5 matrix where each row stores five (5) test scores for a student in a class of size ten (10) students. Complete the following program. In the main method, populate the array with random grades between 0 and 100. Next, call method averageScores(), which returns a single dimensional array storing the average score for each student in the class. Finally prints out the class averages as shown below (values shown are just examples).
The class averages are: 80.0 50.2 66.4 58.2 45.4 35.8 56.6 36.6 50.4 41.4
import java.util.Scanner;
public class ClassScores {
public static void main(String[] args)
{ Scanner input = new Scanner(System.in);
int[][] grades = new int[10][5];
double[] averages = new double[10];
}
public static double[] averageScores(int[][] scores){
}
}
}
// Return the first two characters of the building name as a string
public String getAbbreviatedBuildingName()
{
}
}
Explanation / Answer
PROGRAM CODE:
import java.util.*;
public class ClassScores {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int[][] grades = new int[10][5];
double[] averages = new double[10];
// Random object to populate random numbers
Random rand = new Random();
for(int i=0; i<10; i++)
{
for(int j=0; j<5; j++)
grades[i][j] = rand.nextInt(100);
}
averages = averageScores(grades);
System.out.printf("The class averages are: ");
for(int i=0; i<10; i++)
System.out.printf("%.1f ", averages[i]);
}
public static double[] averageScores(int[][] scores){
double averages[] = new double[10];
for(int i=0; i<10; i++)
{
double sum = 0.0;
for(int j=0; j<5; j++)
sum += scores[i][j];
averages[i] = sum/5;
}
return averages;
}
}
OUTPUT:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.