2D Array Sum in C
Level 3146 keystrokes
Calculates the sum of all elements in a 2D array using nested loops.
C code
// 2 rows × 3 columns1int m[2][3] = {{1, 2, 3}, {4, 5, 6}};// Running total2int sum = 0;// Outer loop over the rows3for (int i = 0; i < 2; i++) // Inner loop over the columns of row i; no braces are needed for a single statement4 for (int j = 0; j < 3; j++) // Runs 2 × 3 = 6 times, once per element5 sum += m[i][j];// 1+2+3+4+5+6 gives Sum: 216printf("Sum: %d", sum);Output
Sum: 21
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this C code and how often they appear.
;× 8,× 6+× 5=× 5[× 4]× 4(× 3)× 3{× 3}× 3