The output of the below program is 24. But I could not understand the behaviour of the function f1(parameters).
There is a recursive call of f1 in m1 nad m2. Considering m1 and m2 holding the function f1's stack. m1 stack will contain:
1]0,12,a 2]0,6,a 3]0,3,a 4]0,1,a 5]0,0,a
And m2 stack will contain:
1]13,12,a 2]20,6,a 3]24,3,a 4]26,1,a 5]27,0,a
What m1 and m2 values hold? please explain this behaviour of the recursive function.
#include <stdio.h>
main()
{
int i, n, m, b, x[25];
int f1(int, int, int j[25]);
for(i=0;i<25;i++) x[i] = i;
i=0; m = 24;
b=f1(i, m, x);
printf("res %d\n",b);
}
int f1( int p, int q, int a[25])
{
int m1,m2;
if (q==0)
return(a[p]);
else
{
m1 = f1 (p, q/2, a);
m2 = f1(p+q/2+1,q/2,a);
if(m1<m2)
return (m2);
else
return(m1);
}
}
f1unless it's a quick example)