search
Log In
1 vote
309 views
int  zap(int n)
{
if (n<=1) then zap =1;
else zap = zap(n-3)+zap(n-1);
}
then the call zap(6) gives the values of zap

Give the proper explanation
in Programming
edited by
309 views
0
Answer  is 9
0
it will lead to compile time error.
0
how.explanation required
0
They asked  what  is  value ...
0
but its zap = zap(n-3)+zap(n-1) : lvalue will be needed
0

@arvin

zap is undeclared, therefore it will lead to compiler error, but that is not complete program, therefore we can assume zap is declared...

moreover there is no problem having variable same as function name with in that function, so the answer is 9

0
how did you solve it to get the answer 9 m seeking for the complete stepwise solution.
0
the function does not return any value ... so do we have to assume that also
0

@Shiv Gaur

yes we have to assume that, function return the value also...

otherwise how we will compute n>2 ?

1
@shaikh i dont know but i think it wont execute as how can zap be a variable and function at the same time..

.

dont u think zap(n)= zap(n-3)+zap(n-1) should be the right way.
0
indeed I also  know that this is not going to execute that's why I am demanding for the correct code you can modify it to make it workable
0

@shivam sharma 5

is it your self doubt? or somewhere you got this question?

if somewhere you got this question, what is the answer provided by them.

2 Answers

3 votes
#include<stdio.h>
int zap(int n)
{
    int p;
    if(n<=1)
    p=1;
    else
    p=zap(n-3)+zap(n-1);
    return p;
}
int main()
{
    int p=zap(6);
    printf("%d",p);
}

 

p will be

p=zap(0) + zap(-1) +zap(1) +zap(-1) +zap(1) +zap(1) +zap(0) +zap(-1) +zap(1)

p=1+1+1+1+1+1+1+1+1=9;

i think this should be solution of this question...

plz tell me if there is wrong any thing in this explanation
0 votes
It is the recursion solution
Caption

 

Related questions

0 votes
2 answers
1
800 views
Output of this program - int rec(int x) { static int f; if(x == 1) return(1); else __Y__ ; return f ; } What is the value returned by rec(5) &#8203;a) when Y is f = f * 1 + rec(x-1) ; b) when Y is f = f * x + rec(x-1) ; What is proper way to solve this ?
asked Jan 16, 2016 in Programming Himanshu1 800 views
0 votes
1 answer
2
461 views
#include<iostream> using namespace std; int i=0; void a() { i+=1; cout<<i<< ".hello"<<endl; a(); } int main() { a(); } For this above code the output is only upto → 64891.Hello Does this mean that that the stack can hold only 64891 recursive calls? (I am using dev c++)
asked Jun 2, 2019 in Programming Hirak 461 views
3 votes
3 answers
3
478 views
Consider the following function: void madeeasy (int n) { if (n < 0) return; else { printf(n); madeeasy (- -n); madeeasy (n - -); printf(n); } } The sum of all values printed by madeeasy (5)_______ (I am getting -12 but given answer is 52)
asked Jan 30, 2019 in Programming newdreamz a1-z0 478 views
2 votes
2 answers
4
302 views
int f (int n){ if (n==0) return 0; if(n==1) return 1; else return f(n-1)+f(n-2); } Find the upper bound and lower bound to the number of function calls for input size 'n'?
asked Jul 8, 2018 in Programming parasghai28 302 views
...