Final answer:
To find the sum and average of all values in a 2D array, initialize variables for sum and count to 0, iterate over each element with nested for loops, add elements to sum, increment count, and then divide sum by count for the average.
Step-by-step explanation:
To calculate the sum and the average of all values in a 2D array, first, we need to initialize two variables sum and count, which will hold the total sum of all the numbers in the array and the count of those numbers, respectively. We then use nested for loops to iterate over each element of the array, adding each element to sum and incrementing count by 1 for each element. After iterating over all elements in the array, we calculate the average by dividing the sum by the count. Finally, we output both the sum and the average to complete the task.
Here's a pseudocode example:
- Initialize the variable sum to 0.
- Initialize the variable count to 0.
- Use a for loop to iterate over each row of the array.
- Nest another for loop inside to iterate over each element of the row.
- Add each element to sum and increment count.
- After all iterations, divide sum by count to get the average.
- Print out the sum and the average.
Here's a sample output based on the given values:
Sum of all values: 949
Average of all values: 37.96