Evaluate the following:
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);




1) 10


2) 11


3) 1

Answer Posted / anju nair

the answer is : 11

solution :

since v=7

initially f(6)+ 3 is executed ( being a recurrsive
function) fn is again called with v=6 and the return now
is f(3) + 2 and again fn is called with v=3 and the return
now is f(2) +3 and again fn is called with v=0.now
recurrsion ends since f(0) is 1

f(0)=1
f(0)+2=3

f(2)=f(0)+2=1+2
f(2)+3=6

f(3)=f(2)+3=6
f(3)+2=6+2=8

f(6)=f(3)+2=8
f(6)+3=8+3=11

f(7)=f(6)+3
f(7)=11

now v hav to back track and keep adding
1+2+3+2+3 = 11.

Is This Answer Correct ?    14 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Describe dynamic data structure in c programming language?

591


What is the difference between a string copy (strcpy) and a memory copy (memcpy)? When should each be used?

610


What is call by reference in functions?

541


Create a structure to specify data on students as given below: Roll number, Name, Department, Course, and Year of joining. Assume that there are not more than 450 students in the collage. (a) Write a function to print the names of all students who joined in the last 3 years. (b) Write a function to print the data of a student whose roll numbers are divisible by 4.

585


What is the default value of local and global variables in c?

547






Define circular linked list.

558


What is meant by errors and debugging?

635


I have seen function declarations that look like this

585


Why is this loop always executing once?

605


What are examples of structures?

579


If the size of int data type is two bytes, what is the range of signed int data type?

573


What are pointers?

619


What is use of bit field?

755


Explain two-dimensional array.

609


What is #include in c?

583