Answer:
- import java.util.Arrays;
- public class Main {
- public static void main(String[] args) {
- double [][] studentScores = {
- {78, 40, 97},
- {89, 90, 68},
- {70, 35, 88}
- };
- System.out.println(Arrays.toString(quizAverages(studentScores)));
- }
- public static double[] quizAverages(double [][] scores){
- double average[] = new double[scores[0].length];
- for(int col = 0; col < scores[0].length; col++){
- double sum = 0;
- for(int row =0; row < scores.length; row++){
- sum += scores[row][col];
- }
- average[col] = sum / scores[col].length;
- }
- return average;
- }
- }
Explanation:
The solution code is written in Java.
Firstly, create a static method that take one input parameter, double type two dimensional arrays and this method will return one array of average as required by question (Line 11).
In the method, declare a double type array and initialize its size with the length of the column of the input scores array (Line 12).
Create a double layer of for loop with outer loop to iterate through the column of the input score array and the inner loop to iterate through the row (Line 13-15). In the outer loop, create a sum variable to hold the value of summation for each column (Line 14). In the inner loop, increment the sum variable with the score in each row (Line 16). After finishing one inner loop cycle, the sum is divided by the length of column and assign it to an item of average array in outer loop before proceed to the next round of outer loop to repeat the same process (Line 18).
At last return the average array (Line 20).
We can test the method using a sample data (Line 4-8) and print out the output returned by the method (Line 9).