Consider the following C functions:
int f1 (int n)
{
if(n == 0 || n == 1)
return n;
else
return (2 * f1(n-1) + 3 * f1(n-2));
}
int f2(int n)
{
int i;
int X[N], Y[N], Z[N];
X[0] = Y[0] = Z[0] = 0;
X[1] = 1; Y[1] = 2; Z[1] = 3;
for(i = 2; i <= n; i++){
X[i] = Y[i-1] + Z[i-2];
Y[i] = 2 * X[i];
Z[i] = 3 * X[i];
}
return X[n];
}
and return the values
View worked solution
Answer: C
We can find the values returned by both functions by tracing them step-by-step.
1. Evaluating :
The recurrence relation for is:
Let's compute the values sequentially:
2. Evaluating :
The iterative function maintains three arrays where and . Substituting these into the recurrence for gives:
Since the base cases are and , the sequence generated by is identical to the sequence generated by .
Thus, .
Therefore, both and return .
Free practice · Your answers on this page are not saved.
