c What the function fcn do where n is a positive integer Hi
c) What the function f_c(n) do ?
where n is a positive integer.
Hint: don not interprete this question as explaining each line of the code, without understanding what the piece of code does as a whole. (explain at a higher level of abstraction).
int f_c1(a, b, n);
int f_c(int n) {
return f_c1(1, n, n);
}
int f_c1(a, b, n) {
int c = (a + b) / 2;
if ( a == b || a == b-1 )
return a;
if ( c*c*c > n)
return f_c1(a, c, n);
return f_c1(c, b, n);
}
--------------------------------------------------------------------------------------------------------------------------
and then draw the stack activation records when function is executed for ret = f_c(100);
for the rst three calls to f_c1. Since you are to submit your answer as a-text file, use
so-called ASCII graphics, which should look like the following
Lc1(c-4,p=7,n=100) -cco I lt_c1(a-LCx7,n=100) I f_c1(a=1.c=13,n=100) I f_c1(a-LC-25,n=100) l t_c1(a-LC-50,n=100) If_c1(a=1,p=100,n=100) l fc(n-100) | main I f_c1(a=1c=7,n=100) cacc uacauuca I cacocouacauucaSolution
#include <iostream>
 using namespace std;
int f_c1(int,int,int);
int f_c(int n)
 {
return f_c1(1, n, n);
}
int f_c1(int a,int b,int n)
 {
int c = (a + b) / 2;
if ( a == b || a == b-1 )
return a;
if ( c*c*c > n)
return f_c1(a, c, n);
return f_c1(c, b, n);
}
int main()
 {
 int ret = f_c(100);
 cout<<\"ret =\"<<ret;
 return 0;
 }
output:
ret =4
In f_c1() , the value of argument b is halved until it becomes equal to a or a+1 .
| return 4 | 
| f_c1(a=4,c=5,n=100) | 
| f_c1(c=4,b=7,n=100) | 
| f_c1(a=1,c=7,n=100) | 
| f_c1(a=1,c=13,n=100) | 
| f_c1(a=1,c=25,n=100) | 
| f_c1(a=1,c=50,n=100) | 
| f_c1(a=1,b=100,n=100) | 
| f_c(n=100) | 
| main | 


