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]);
}- A. 11 13
- B. 11 12
- C. 10 12
- 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
- A qsort comparator must order the uint16t values by high byte descending and, for equal high bytes, by low byte ascending. What is…
- What is printed? c #include <stdio.h> unsigned sumd(unsigned n) { return n == 0 ? 0 : n % 10 + sumd(n / 10); } int main(void) {…
- How many subsets of {1, 2, 3, 4, 5, 6} (the empty set included) have a sum divisible by 3? Compute with this program. c #include <stdio.h>…
- What is printed by the first two calls? c #include <stdio.h> unsigned next(void) { static unsigned s = 1; s = s 1103515245u + 12345u…
- What is printed? c #include <stdio.h> int main(void) { int total = 0; for (unsigned x = 1; x <= 100; x++) { unsigned y = x; while (y) {…
- What is printed? c #include <stdio.h> #include <string.h> int main(void) { const char names[] = {"ann", "bob", "cat", "dan", "eve"}; const…