ProgrammingVery hardCompetitive coding (CP)
What is the maximum value of depth printed?
What is the maximum value of depth printed?
#include <stdio.h>
int depth = 0, maxd = 0;
void r(int n) {
depth++;
if (depth > maxd) maxd = depth;
if (n > 0) {
r(n - 1);
r(n - 2);
}
depth--;
}
int main(void) {
r(4);
printf("%d", maxd);
}- A. 4
- B. 6
- C. 5
- D. 3
Correct answer: C. 5
The deepest chain goes r(4), r(3), r(2), r(1), r(0), which has five active calls. Counting only the recursive calls gives 4, and the other branches are never deeper.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 2, 3, 4, 5}; int p = a + 2; printf("%d %d", p[-1], p[2]); }
- What is printed? Assume uint32t is 32 bits. c #include <stdint.h> #include <stdio.h> uint32t rotl(uint32t x, unsigned k) { return (x << k)…
- What is printed? c #include <stdio.h> int main(void) { unsigned u = 1; int s = -2; printf("%d %d", s < u, s + (int)u > -2); }
- What is printed? Assume int is 4 bytes. c #include <stdio.h> int main(void) { int i = 1; sizet n = sizeof(i++); printf("%d %zu", i, n); }
- How many factors of 2 does 100! contain? Compute with this program. c #include <stdio.h> int main(void) { int c = 0; for (int i = 2; i <=…
- What is printed? c #include <stdio.h> #include <string.h> int main(void) { char s[] = "stressed"; char a = s, b = s + 7; while (a < b) {…