ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int calls = 0;
int A(int m, int n) {
calls++;
if (m == 0) return n + 1;
if (n == 0) return A(m - 1, 1);
return A(m - 1, A(m, n - 1));
}
int main(void) {
int r = A(2, 2);
printf("%d %d", r, calls);
}- A. 7 27
- B. 5 14
- C. 7 19
- D. 6 27
Correct answer: A. 7 27
This is the Ackermann function, with A(1, n) = n + 2 and A(2, n) = 2n + 3, so A(2, 2) = 7. Counting calls: A(1, n) takes 2n + 2 calls, A(2, 0) = A(1, 1) takes 1 + 4 = 5, A(2, 1) = A(1, A(2, 0)) = A(1, 3) takes 1 + 5 + 8 = 14, and A(2, 2) = A(1, A(2, 1)) = A(1, 5) takes 1 + 14 + 12 = 27 calls. 5 and 14 are the value and call count of A(2, 1).
More C Programming questions
- What is printed? c int a[7] = {1, 2, 3, 4, 5, 6, 7}, n = 7, i = 0; while (n > 1) { i = (i + 2) % n; printf("%d ", a[i]); for (int j = i; j…
- What is printed? c / LP64: 4-byte int, 8-byte pointers / void f(int m[][4], int n) { printf("%zu %zu %td ", sizeof m, sizeof m[0], (char…
- What is printed? c int a = 1, b = 2, c = 3; a += b -= c = 2; int d = (a, b) > c ? a-- : c++; printf("%d %d %d %d", a, b, c, d);
- What is printed? c int a[] = {1, 2, 3, 4, 5}; int p = a + 1, q = &a[4]; printf("%td %d %d", q - p, p + q, p[2]);
- With 4-byte int, what is printed? c int a[3][4] = {0}; int (p)[4] = a; int q = &a[0][0]; printf("%td %td %td", (p + 2) - p, (char )(p + 1)…
- What is printed? c int calls = 0; int f(int n) { calls++; return n < 2 ? n : f(n / 2) + f(n - 1); } int main(void) { int r = f(6)…