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

How can I disable the "echo" feature?

696


What function initalizes variables in a class: a) Destructor b) Constitutor c) Constructor

784


Is vector a class in c++?

691


What is size_type?

634


what are the iterator and generic algorithms.

1560






What is microsoft c++ redistributable?

699


List the features of oops in c++?

664


What are manipulators in c++ with example?

678


How const int *ourpointer differs from int const *ourpointer?

693


What is the use of "new" operator?

761


Is swift better than c++?

630


What is Destructor in C++?

767


What is the use of string in c++?

635


What is main function in c++ with example?

651


Why iomanip is used in c++?

738