Consider the following function.
For large values of , the return value of the function best approximates
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;
}
View worked solution
Answer: B
To find what the function approximates for large values of , let's trace the values of the variables and inside the loop.
Initially, before the loop starts:
The loop runs for :
- For :
- For :
- For :
Continuing this process up to , the final value of returned by the function is:
For large values of (i.e., as ), this infinite series is the standard Taylor series expansion of the exponential function:
Therefore, the return value of the function best approximates .
Free practice · Your answers on this page are not saved.
