Consider the following C function. float f(float x, int y) { float p, s; int…

2003

Consider the following C function. 

float f(float x, int y)
{
  float p, s; int i;
  for (s=1, p=1, i=1; i < y; i ++)
  {
    p*= x/i;
    s+=p;
  }
  return s;
}  

For large values of y, the return value of the function f best approximates

  1. A.

    x^y

  2. B.

    e^x

  3. C.

    ln(1 + x)

  4. D.

    x^x

Attempted by 53 students.

Show answer & explanation

Correct answer: B

Answer: e^x

Explanation: The function builds a sum of terms that match the exponential series.

  • Initialization: s = 1 and p = 1 represent the k = 0 term (1).

  • Loop effect: each iteration does p *= x/i. After the iteration with index i, p = x^i / i!.

  • Accumulation: s adds each p, so after the loop s = sum_{k=0}^{y-1} x^k / k!.

Conclusion: As y becomes large, the finite sum approaches the infinite series sum_{k=0}^∞ x^k / k! = e^x, so the function best approximates e^x.

Explore the full course: Gate Guidance By Sanchit Sir