5 pts What is the ouput of the following function F for the
(5 pts) What is the ouput of the following function F, for the call int i = F(3)?
int F(int n)
{
int result;
if (n > 20)
return 1;
else
{ result = F(2*n) * 2;
cout << result << \" \";
return result;
}
}
Solution
#include <iostream>
using namespace std;
int F(int n) //n=3
{
int result;
if (n > 20)
return 1;
else
{
result = F(2*n) * 2; //recursive calls, F(6)*2,F(12)*2*2,F(24)*2*2*2,1*2*2*2=8
cout << result << \" \"; // f(24)*2 = 1*2=2 ,F(12)*2 = 2*2 =4, F(6)*2 = 4*2 =8
return result;
}
}
int main()
{
int i = F(3);
cout<<\"\ i=\"<<i;
return 0;
}
output:
