ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int m[3][3];
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            m[i][j] = i * 3 + j + 1;
    int *row = &m[0][0];
    int s = 0;
    for (int i = 0; i < 3; i++)
        s += m[i][2 - i];
    printf("%d", s);
}
  1. A. 12
  2. B. 15
  3. C. 9
  4. D. 18

Correct answer: B. 15

The anti-diagonal entries are m[0][2] = 3, m[1][1] = 5 and m[2][0] = 7, summing to 15. The pointer row is unused and does not affect the result.

More C Programming questions

Exams that ask this topic