Evaluate:
int fn(int v)
{
if(v==1 || v==0)
return 1;
if(v%2==0)
return fn(v/2)+2;
else
return fn(v-1)+3;
}
for fn(7);

a) 10
b) 11
c) 1

Answer Posted / jagadish

The answer would be
b.

11 Its recursive function.so track for the returns
1st return:fn(v-1)+3;::v=7
2nd return fn(v/2)+2;::v=6
3rd return fn(v-1)+3;::v=3
4th return fn(v/2)+2;::v=2
5th return 1;::v=1

finally its 11=1+2+3+2+3

Is This Answer Correct ?    6 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the use of lambda in c++?

571


Why do we need pointers?

577


What is do..while loops structure?

613


If you want to share several functions or variables in several files maitaining the consistency how would you share it?

548


How do you add an element to a set in c++?

547






What is private public protected in c++?

547


What is a dynamic binding in c++?

524


Is c++ the hardest language?

555


How can you differentiate between inheritance and implementation in c++?

637


Write a program to find the reverse Fibonacci series starting from N.

600


Will a recursive function without an end condition every quit, in practice a) Compiler-Specific (Some can convert to an infinite loop) b) No c) Yes

577


What language is a dll written in?

540


Explain the use of this pointer?

632


What are function prototypes?

640


What is virtual table?

608