ProgrammingModerateCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int a[] = {4, 1, 7, 3, 9}, p[6] = {0};
    for (int i = 0; i < 5; i++) p[i + 1] = p[i] + a[i];
    printf("%d %d", p[4] - p[1], p[5] - p[3]);
}
  1. A. 11 13
  2. B. 11 12
  3. C. 10 12
  4. D. 12 11

Correct answer: B. 11 12

p[i] holds a[0] + ... + a[i-1], so p[4] - p[1] = a[1] + a[2] + a[3] = 11 and p[5] - p[3] = a[3] + a[4] = 12. Using p[i] for a range that starts at index i is the usual off-by-one.

More C Programming questions

Exams that ask this topic