Example 1 — Print a matrix and its row sums
Nested loops over rows and columns.
#include <stdio.h>
int main(void) {
int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int r = 0; r < 2; r++) {
int sum = 0;
for (int col = 0; col < 3; col++) {
printf("%d ", m[r][col]);
sum += m[r][col];
}
printf("| sum=%d\n", sum);
}
return 0;
} How It Works
- The outer loop picks the row, the inner loop walks its columns.
sumis reset for each row.- Row sums: 1+2+3 = 6 and 4+5+6 = 15.
