Read the question, write C on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
Read two integers r and c, then r × c integers in row-major order.
Print the sum of every entry. A nested loop over a 2D array is the point of this lab.
Input. r c, then r*c integers.
Output. One integer: the total.
Constraints
- 1 ≤ r, c ≤ 20
Examples
Input
2 3 1 2 3 4 5 6
Output
21
Hint
- int a[20][20]; nested for i, j; scanf into a[i][j]; add to sum.
Show correct code
Peek only after you have tried. You can still Check your own version.
#include <stdio.h>
int main(void) {
int r, c;
if (scanf("%d %d", &r, &c) != 2) return 1;
long sum = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
int x;
scanf("%d", &x);
sum += x;
}
}
printf("%ld\n", sum);
return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.