ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int c = 0;
int g(int n) {
c++;
if (n <= 2) return 1;
return g(n - 1) + g(n - 2) + g(n - 3);
}
int main(void) {
int r = g(6);
printf("%d %d", r, c);
}- A. 13 25
- B. 17 25
- C. 17 23
- D. 24 25
Correct answer: B. 17 25
Values: g(0)=g(1)=g(2)=1, g(3)=3, g(4)=5, g(5)=9, g(6)=9+5+3=17. Calls: C(n)=1 for n ≤ 2, else 1 + C(n−1) + C(n−2) + C(n−3), giving C(3)=4, C(4)=7, C(5)=13, C(6)=25.
More C Programming questions
- Assume 32-bit unsigned int. What is printed? c unsigned f(unsigned x) { return (x & 0x55555555u) << 1 | (x >> 1) & 0x55555555u; }…
- What is printed? c #define X 3 #define Y X 2 #if Y == 6 #define Z 1 #else #define Z 2 #endif printf("%d %d", Z, Y + 1 2);
- What is printed? c #define SHOW(x) printf(#x " = %d", x) int a = 3; SHOW(a 2 + 1);
- file1.c contains static int count = 5; at file scope. file2.c contains extern int count; and uses count in main. Nothing else defines…
- What is printed? c int a[] = {10, 20, 30, 40, 50}; int p = (int )(&a + 1); int q = &a[1] + 1; printf("%d %d %td", (p - 1), (q - 1), p - q);
- What is printed? c int a = 0, b = 1, c = 2; if (a++ || b-- && ++c) c += 10; printf("%d %d %d", a, b, c);