Question
Consider the following C function.
int fun(int n){
int x=1, k;
if (n==1) return x;
for (k=1; k<n; ++k)
x = x + fun(k) * fun(n-k);
return x;
}
The return value of fun(5) is _____.
Official answer and solution
- Official answer
- 51
- From
- Official GATE 2015 answer key
Solution
Unroll the recursive recurrence relation computed by the C function fun.
The function definition is:
- Base case:
fun(1) = 1. - For :
Calculate sequentially:
- : .
- :
- :
- :
- :
Final answer
51
Common trap
Forgetting the initial int x = 1; that adds 1 to the summation for each recursive call.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2015 Computer Science and Information Technology (CS) Session 2
- Question
- 21 of 65, printed as CS Q.11
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.