Skip to main content

GATE CSE 2021Algorithms › Program & Function TracingVerified

GATE CSE 2021 PYQ: Consider the following ANSI C \text{ANSI C} ANSI C function: int SimpleFunction ( int Y [], int n , int x )…

Consider the following ANSI C\text{ANSI C} function:

  1. int SimpleFunction(int Y[], int n, int x)
  2. {
  3. int total = Y[0], loopIndex;
  4. for (loopIndex=1; loopIndex<=n-1; loopIndex++)
  5. total=x*total +Y[loopIndex];
  6. return total;
  7. }

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

    Answer

    1023

    Approach

    Evaluate the recurrence implemented by the loop. With all array elements equal to 1 and x = 2, each iteration transforms total from t to 2t + 1, starting from t = 1.

    The function evaluates a polynomial-style expression with coefficients all equal to 1 and base x = 2. Since the loop performs 9 updates and starts from total = 1, the returned value is $2^{10} - 1 = 1023$.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.