Ask Question
15 January, 08:35

Given an array as follows

long [] [] nums = {{1,2,3,4,5}, {6,7,8,9}, {10,11,12,13,14,15}, {16,17,18,19,20}, {21,22,23,24,25}};

Build the following methods:

1) calcTotal: The method should accept a two-dimensional array as an argument and return the total of all the values in the array.

2) calcAverage: The method should accept a two-dimensional array as an argument and return the average of all the values in the array.

3) calcRowAverage: The method should accept two parameters: 1) a two-dimensional array as an argument and 2) an integer that represents a row number for calculations. Return the average of all the values in that row in the array.

+1
Answers (1)
  1. 15 January, 11:15
    0
    1) Method calcTotal:

    public static long calcTotal (long [][] arr2D) { long total = 0; for (int i = 0; i < arr2D. length; i++) { for (int j = 0; j < arr2D[i]. length; j++) { total = total + arr2D[i][j]; } } return total; }

    Explanation:

    Line 1: Define a public method calcTotal and this method accept a two-dimensional array

    Line 2: Declare a variable, total, and initialize it with zero.

    Line 4: An outer for-loop to iterate through every rows of the two-dimensional array

    Line 6: An inner for-loop to iterate though every columns within a particular row.

    Line 8: Within the inner for-loop, use current row and column index, i and j, to repeatedly extract the value of each element in the array and add it to the variable total.

    Line 12: Return the final total of all the element values as an output

    Answer:

    2) Method calcAverage:

    public static double calcAverage (long [][] arr2D) { double total = 0; int count = 0; for (int i = 0; i < arr2D. length; i++) { for (int j = 0; j < arr2D[i]. length; j++) { total = total + arr2D[i][j]; count++; } } double average = total / count; return average; }

    Explanation:

    The code in method calcAverage is quite similar to method calcTotal. We just need to add a counter and use that counter as a divisor of total values to obtain an average.

    Line 4: Declare a variable, count, as an counter and initialize it to zero.

    Line 11: Whenever an element of the 2D array is added to the total, the count is incremented by one. By doing so, we can get the total number of elements that exist in the array.

    Line 16: Use the count as a divisor to the total to get average

    Line 18: Return the average of all the values in the array as an output.

    Answer:

    3) calcRowAverage:

    public static double calcRowAverage (long [][] arr2D, int row) { double total = 0; int count = 0; for (int i = 0; i < arr2D. length; i++) { if (i = = row) { for (int j = 0; j < arr2D[i]. length; j++) { total = total + arr2D[i][j]; count++; } } } double average = total / count; return average; }

    Explanation:

    By using method calcAverage as a foundation, add one more parameter, row, in the method calcRowAverage. The row number is used as an conditional checking criteria to ensure only that particular row of elements will be summed up and divided by the counter to get an average of that row.

    Line 1: Add one more parameter, row,

    Line 8-15: Check if current row index, i, is equal to the target row number, proceed to sum up the array element in that particular row and increment the counter.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Given an array as follows long [] [] nums = {{1,2,3,4,5}, {6,7,8,9}, {10,11,12,13,14,15}, {16,17,18,19,20}, {21,22,23,24,25}}; Build the ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers