Consider the following C function:
int f(int n) { static int r = 0; if (n <= 0) return1; if (n > 3) { r = n; return f(n-2) + 2; } return f(n-1) + r; }
What is the value of $f(5)$?
Can anyone explain why f(5) will not execute return f(n-1) + r;?
I think there should be an else in lile 9.
int f(int n) { static int r = 0; if (n <= 0) return1; if (n > 3) { r = n; return f(n-2) + 2; } else return f(n-1) + r; }
The answer is D. $f(5) = 18.$ $f(3) + 2 = 16 + 2 = 18$ $f(2) + 5 = 11 + 5 = 16$ $f(1) + 5 = 6 + 5 = 11$ $f(0) + 5 = 1+5 = 6$ Consider from last to first. Since it is recursive function.
No, it's correct to add 5.
Reason :
Gatecse
It's a test series conducted by Kiran sir i.e...