Consider the following function:
int SimpleFunction(int Y[], int n, int x)
{
int total = Y[0], loopIndex;
for (loopIndex=1; loopIndex<=n-1; loopIndex++)
total=x*total +Y[loopIndex];
return total;
}
Let be an array of elements with , for all such that . The value returned by is __________
View worked solution
Answer: 1023 : 1023
The given C function implements Horner's method for evaluating a polynomial of degree at a point , where the coefficients of the polynomial are given by the array :
Let's trace the execution of SimpleFunction(Z, 10, 2) where:
- for all
Step-by-Step Trace:
Initially, total = Y[0] = 1.
The loop runs for loopIndex from to :
- Iteration 1 (
loopIndex = 1):
- Iteration 2 (
loopIndex = 2):
- Iteration 3 (
loopIndex = 3):
In general, after iterations, the value of total is given by the recurrence:
Solving this recurrence, we get:
The loop terminates after iterations (when loopIndex = 9). Substituting into our formula:
Thus, the value returned by the function is 1023.
Free practice · Your answers on this page are not saved.
