// matrix_sum_debug.c // // Written by YOUR-NAME (YOUR-ZID) // on TODAYS-DATE // // Prints the sum of all elements in the matrix and the // sum of elements along the top-left to bottom-right diagonal. #include #define SIZE 5 // Function Prototypes int full_sum(int matrix[SIZE][SIZE]); int main(void) { int matrix[SIZE][SIZE] = { { 1, 2, 3, 4, 5 }, { 8, 3, 2, 77, 1 }, { 0, 0, 0, 0, 0 }, { 1, 2, 23, 4, 5 }, { 1, 44, 3, 0, 1 }, }; printf("Full sum: %d\n", full_sum(matrix)); printf( "Sum of top-left to bottom-right diagonal: %d\n", diagonal_sum(matrix) ); return 0; } // Calculates the sum of all elements in a 2D matrix int full_sum(int matrix[SIZE][SIZE]) { int sum; for (int i = 1; i < SIZE; i++) { for (int j = 1; j < SIZE; j++) { sum = sum + matrix[j][j]; } } return sum; } // Calculates the sum of elements along the top-left to bottom-right diagonal // in a 2D matrix int diagonal_sum(int matrix[SIZE][SIZE]) { int sum = 0; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (i != j) { sum++; } } } return sum; }