C Program to Find Sum of Matrix Diagonals
C Program to Find Sum of Matrix Diagonals
A small school table has two rows and two columns. The sum matrix diagonals operation should preserve what each row and column means, rather than treating the table as four unrelated values.
Sum the union of the main and secondary diagonal positions, counting a shared center only once.
Before this exercise, review array indexes and bounds and nested loops. The worked program below applies those ideas to this specific problem.
Explain
Explain why sum matrix diagonals uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Use size three and show why the center five is counted once.
Sum Matrix Diagonals: follow the data
Given data
A small school table has two rows and two columns. The sum matrix diagonals operation should preserve what each row and column means, rather than treating the table as four unrelated values.
Rule
Sum the union of the main and secondary diagonal positions, counting a shared center only once.
Observed result
Diagonal union: 10
Understand the operation
The main diagonal satisfies row==col. The other satisfies row+col==N−1. A single or-condition counts each visited position at most once.
The example fixes both dimensions at two so every index can be checked directly. A general matrix interface must carry dimensions, require compatible shapes for its operation and validate storage for each referenced element.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
int matrix[2][2] = {{1, 2}, {3, 4}};
int sum = 0;
for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) if (row == col || row + col == 1) sum += matrix[row][col];
printf("Diagonal union: %d\n", sum);
return 0;
}
Expected output
Diagonal union: 10
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int matrix[2][2] = {{1, 2}, {3, 4}};
int sum = 0;
for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) if (row == col || row + col == 1) sum += matrix[row][col];
printf("Diagonal union: %d\n", sum);
return 0;
}
In size two the diagonals contain all four entries, summing to ten.
Read the resulting rows in order; moving a value to another row or column is part of the operation’s meaning, not merely an output-format choice.
Follow the worked calculation
Start with the sample
In size two the diagonals contain all four entries, summing to ten.
Apply the rule
Read the resulting rows in order; moving a value to another row or column is part of the operation’s meaning, not merely an output-format choice.
Check the result
Diagonal union: 10
Diagonal union: 10
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int matrix[2][2] = {{1, 2}, {3, 4}};
int sum = 0;
for (int row = 0; row < 2; row++) for (int col = 0; col < 2; col++) if (row == col || row + col == 1) sum += matrix[row][col];
printf("Diagonal union: %d\n", sum);
return 0;
}
Use size three and show why the center five is counted once.
Worked answer
The complete worked answer uses the same valid two-by-two input. Trace its index rule independently; compare each printed result with the source element or accumulation that produced it.
C17 · complete program
#include <stdio.h>
int main(void) {
int larger[3][3] = {{1,2,3},{4,5,6},{7,8,9}}, sum = 0;
for (int row = 0; row < 3; row++) for (int col = 0; col < 3; col++) if (row == col || row + col == 2) sum += larger[row][col];
printf("Diagonal union: %d\n", sum);
return 0;
}
Expected output
Diagonal union: 25
Run in C compiler →Recognize the important boundaries
For an odd square matrix, two separately summed diagonals double-count the center unless corrected. Define whether you intend a union or two sums.
Larger or untrusted dimensions require checked storage-size calculations. Integer products and accumulated sums also need a representable-range contract.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Sum the union of the main and secondary diagonal positions, counting a shared center only once.
Continue with array boundaries.
Continue with function bounds contracts.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.