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 are the differences between malloc() and calloc()?

619


What is the basic difference between C and C++?

636


What is prototype in c++ with example?

571


explain the reference variable in c++?

590


Which operator cannot overload?

551






Evaluate !(1&&1||1&&0) a) Error b) False c) True

713


What do you mean by translation unit in c++?

689


What is atoi in c++?

576


What is an inclusion guard?

630


What is the two main roles of operating system?

523


What does the following code do: int c=0; cout< a) Undefined *Updated* b) 01 c) 00

642


What is conditions when using boolean operators?

607


How do you compile the source code with your compiler?

625


Write a single instruction that will store an EVEN random integer between 54 and 212 inclusive in the variable myran. (NOTE only generate EVEN random numbers)

1498


What is function prototyping?

626