522 views
2 votes
2 votes
The value returned by the following function for foo(10) is ____

int foo(int x)
{
    if(x < 1)
        return 1;
    int sum = 0;
    for(i = 1; i <= x; i++)
    {
        sum += foo(x-i);
    }
    return sum;
}

1 Answer

Best answer
2 votes
2 votes
With the help of recursion tree we can easily find the function value for some small numbers.

$\begin{align*} &\text{foo}(0) = 1 \\ &\text{foo}(1) = 1 \\ &\text{foo}(2) = 2 \\ &\text{foo}(3) = 4 \\ &\text{foo}(4) = 8 \\ &\text{foo}(5) = 16 \\ &\text{foo}(6) = 32 \\ \end{align*}$

We can see that, for $n > 0$ $ \;\;\;\; \text{foo}(n) = 2^{n-1} $

Therefore $\text{foo}(10) = 512$
selected by

Related questions

0 votes
0 votes
2 answers
1
Debargha Mitra Roy asked Apr 10
103 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
1 votes
1 votes
0 answers
2
RahulVerma3 asked Mar 22
142 views
My question is that can we use command line arguments without use of main function's parameters argc and *argv?