ProgrammingHardGATE CS · Infosys

What is printed?

What is printed?
#include <stdio.h>
int sum(int (*m)[3], int rows) {
    int s = 0;
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < 3; j++)
            s += m[i][j] * (i + 1);
    return s;
}
int main(void) {
    int g[2][3] = {{1, 2, 3}, {4, 5, 6}};
    printf("%d", sum(g, 2));
}
  1. A. 42
  2. B. 36
  3. C. 45
  4. D. 21

Correct answer: B. 36

Row 1 contributes 6 * 1 and row 2 contributes 15 * 2, so the total is 6 + 30 = 36. Forgetting the row weight gives 21.

More C Programming questions

Exams that ask this topic