The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
124 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 by (31 points)
edited by | 124 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

+2 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
by (83 points)
0 votes
It is the recursion solution
Caption

 

ago by (303 points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,984 questions
55,135 answers
190,487 comments
85,107 users