ProgrammingVery hardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
void gen(int d, int v) {
    if (d == 0) {
        printf("%d ", v);
        return;
    }
    gen(d - 1, v * 2 + 1);
    gen(d - 1, v * 2);
}
int main(void) {
    gen(3, 0);
}
  1. A. 0 1 2 3 4 5 6 7
  2. B. 7 6 5 4 3 2 1 0
  3. C. 3 2 1 0 7 6 5 4
  4. D. 7 5 3 1 6 4 2 0

Correct answer: B. 7 6 5 4 3 2 1 0

Each call visits the odd child before the even child, so the leaves come out in decreasing order from 7 down to 0. Swapping the two calls would print the values in increasing order.

More C Programming questions

Exams that ask this topic