21.6k views
0 votes
Write sum() function prototype and definition that returns the sum of a 2D array scores with 3 rows and 4 columns. The function receives the 2D array and also the number of rows in the array. Separately, show how to use the function to print the total from the array (assuming some initialization with numbers 0-100) to the screen.

1 Answer

3 votes

Final answer:

To declare and define the sum() function that returns the sum of a 2D array with 3 rows and 4 columns, follow the provided code. Use the function to print the total from the array by declaring an array, initializing it, and calling the sum() function.

Step-by-step explanation:

To declare and define the sum() function that returns the sum of a 2D array with 3 rows and 4 columns, you can use the following code:

int sum(int array[][4], int numRows){ int total = 0; for(int i=0; i<numRows; i++){ for(int j=0; j<4; j++){ total += array[i][j]; } } return total; }

To use this function to print the total from the array, you can declare an array and initialize it with numbers ranging from 0 to 100. Then, you can call the sum() function and pass the array and the number of rows as arguments:

int main(){ int scores[3][4] = {{10, 20, 30, 40}, {50, 60, 70, 80}, {90, 100, 0, 20}}; int numRows = 3; int total = sum(scores, numRows); printf("Total: %d\\", total); return 0; }
User Happier
by
7.6k points