Skip to content
GATEiit

GATE CSE 2021 Set 1 Q58: Value returned by a Horner-style loop on an all-ones array

Question

Consider the following ANSI C function:

Let Z be an array of 10 elements with Z[ii] = 1, for all ii such that 0≤i≤90 \le i \le 9. The value returned by SimpleFunction(Z, 10, 2) is ________.

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;
}

Try it: numerical answer: type the value

Numerical answer. No negative marking.
Show answer and explanation

Answer: 1023 (official GATE 2021 final answer key)

Concept used: The loop is Horner's rule: it evaluates the polynomial with coefficients Y[0..n−1] at x.

First step: With all coefficients 1 and x=2x = 2, the result is 29+28+⋯+12^9 + 2^8 + \dots + 1.

Full step-by-step solution

Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.

Share this question

WhatsAppDownload image

Source: official GATE 2021 Set 1 CS question paper and final answer key, organised by IIT Bombay. Question paper · Answer key