ProgrammingHard

int m[4][4]; for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) m[i][j] = i + j; int d = 0; for (int i = 0; i < 4; i++) d += m[i][i]; printf("%d", d); What is printed?

  1. A. 12
  2. B. 6
  3. C. 16
  4. D. 24

Correct answer: A. 12

m[i][i] = i + i = 2i, so the diagonal is 0, 2, 4, 6 and sums to 12. 6 adds only the indices 0 + 1 + 2 + 3, and 24 doubles the correct total again.

More C Programming questions

Exams that ask this topic