Matrix Sums
Completed
Read an r×c matrix (max 10×10) and print each row sum on one line and each column sum on the next.
Input
r c, then r rows.
Output
Two lines.
Example
Input
2 3 1 2 3 4 5 6
Output
6 15 5 7 9
Editor
C
Hints
m[i][j] is row i, column j.
Solution explanation
Swapping the loop order switches between row-wise and column-wise processing.
Show solution
#include <stdio.h>
int main() {
int r, c, m[10][10];
scanf("%d %d", &r, &c);
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
scanf("%d", &m[i][j]);
for (int i = 0; i < r; i++) {
int s = 0;
for (int j = 0; j < c; j++) s += m[i][j];
printf("%d%s", s, i < r - 1 ? " " : "\n");
}
for (int j = 0; j < c; j++) {
int s = 0;
for (int i = 0; i < r; i++) s += m[i][j];
printf("%d%s", s, j < c - 1 ? " " : "\n");
}
return 0;
}