Question
Consider the following ANSI C 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 10 elements with , for all such that . The value returned by SimpleFunction(Z, 10, 2) is __________
Official answer and solution
- Official answer
- 1023
- From
- Official GATE 2021 answer key
Solution
Recognize the Horner's rule accumulator pattern and compute the resulting geometric series.
The function SimpleFunction(Y, n, x) initializes total = Y[0] and then iteratively updates:
Expanding this recurrence yields Horner's rule representation for the polynomial:
For the call SimpleFunction(Z, 10, 2), we have , , and for all :
1023
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2021 Computer Science and Information Technology (CS, Set-1)
- Question
- 58 of 65, printed as CS Q.48
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.