in Programming
764 views
5 votes
5 votes

Consider the following function:

void f(int a)
{
        if(a <= 0)
                return ;
        else{
                printf("%d ",a);
                f(a-2);
                printf("%d ",a);
                f(a-3);
        }
}

The sum of all values printed by f(6) is _____ .

in Programming
764 views

2 Comments

dont know sir,they gave aa value in solution,but my doubte is as per qs i calculated madeeasy(a-2) ,and whatever the final value came ,then i used that  value of a in the next madeeasy(a-3) function,but they calculate both fn parallely,like madeeasy(4) madeeasy(3)  parallely,my qs is that until and unless i calculate first function how did i move into the second function..and why did they use the value of a is 6 un both functions

0
0
sorry, I got confused with their printf usage :O
0
0

1 Answer

2 votes
2 votes
Best answer
f(0) does not print anything.

f(1) prints 1 and 1.

f(n) prints n, f(n-2), again n and then f(n-3).

So, we can have the recurrence relation for the sum of values being printed as

$P(n) = 2n + P(n-2) + P(n-3), P(n\leq0) = 0.$

$P(1) = 2, P(2) = 4, P(3) = 8, P(4) = 14$

$P(6) = 2\times 6 + P(4) + P(3) \\=12 + 14 + 8 = 34.$
selected by
by

4 Comments

This is the output- just don't care their explanation.

$ ./a.out
6 4 2 2 4 1 1 6 3 1 1 3
1
1
@Arjun Sir, Why did you calculate sum in your answer??
0
0
The question asks for sum of all values.
0
0