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 ::(scope resolution operator)?

814


What is the use of endl?

725


What are the different types of polymorphism in c++?

805


Explain what you mean by a pointer.

832


What data structure is fastest, on average, for retrieving data: a) Binary Tree b) Hash Table c) Stack

797






Why is c++ called oops?

771


Define whitespace in C++.

939


List the issue that the auto_ptr object handles?

789


What is pure virtual function?

798


Show the declaration for a static function pointer.

773


Is c++ a good first language to learn?

747


what kind of projects are suitable for c and c++

801


What is stack unwinding?

783


What is vector pair in c++?

917


Explain the use of virtual destructor?

793