ProgrammingModerateAccenture · Capgemini pattern

What is printed?

What is printed?
int m[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int s = 0;
for (int i = 0; i < 3; i++)
    s += m[i][2 - i];
printf("%d %d", s, m[2][0] * m[0][2]);
  1. A. 15 21
  2. B. 15 9
  3. C. 12 21
  4. D. 45 21

Correct answer: A. 15 21

A flat initializer fills the 2-D array row by row: {1,2,3}, {4,5,6}, {7,8,9}. m[i][2 − i] walks the anti-diagonal: m[0][2] = 3, m[1][1] = 5, m[2][0] = 7, sum 15. m[2][0] × m[0][2] = 7 × 3 = 21. 9 would be the product of the main-diagonal corners 1 × 9.

More C Programming questions

Exams that ask this topic