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
What is function overriding in c++?
write a porgram in c++ that reads an integer and print the biggest digit in the number
Why c++ is created?
Which of the following is not a valid declaration for main() a) int main() b) int main(int argc, char *argv[]) c) They both work
What do you mean by translation unit?
Explain the difference between using macro and inline functions?
Is string data type in c++?
What is malloc in c++?
You want to link a c++ program to c functions. How would you do it?
Why do we use structure in c++?
What is static class data?
If a header file is included twice by mistake in the program, will it give any error?
What are friend classes? What are advantages of using friend classes?
What is scope operator in c++?
What is this weird colon-member (" : ") syntax in the constructor?