The Gateway to Computer Science Excellence

First time here? Checkout the FAQ!

x

+10 votes

Consider the following $C$ function.

For large values of $y$, the return value of the function $f$ 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; }

- $x^y$
- $e^x$
- $\text{ln} (1+x)$
- $x^x$

+14 votes

Best answer

The simplified version of above program can be rewritten as float f(float x, int y) { float p=1, s=1; int i; for (i=1; i<y; i++) { p = p * (x/i); s = s + p; } return s; }

we can take p=1,s=1 intialization outside of for loop cz there is no condition checking in for loop involving p,s .

i | p= p * (x/i) | s= s + p |

1 | x | 1+x |

2 | $\frac{x^{2}}{2}$ | 1+x+$\frac{x^{2}}{2}$ |

3 | $\frac{x^{3}}{6}$ | 1+x+$\frac{x^{2}}{2}$+$\frac{x^{3}}{6}$ |

4 | $\frac{x^{4}}{24}$ | 1+x+$\frac{x^{2}}{2}$+$\frac{x^{3}}{6}$+$\frac{x^{4}}{24}$ |

n | $\frac{x^{n}}{n!}$ | e^{x} |

e^{x }= x^{n} / n!
= 1+x+$\frac{x^{2}}{2}$+$\frac{x^{3}}{6}$ +$\frac{x^{4}}{24}$+ ...+$\frac{x^{n}}{n!}$ |

Hence Option **B** is Ans.

+23 votes

$$\begin{array}{l}

i = 1 &\rm then& p = x &\&& s = 1 + x\\[1em]

i = 2 &\rm then& p = \frac{x^2}{2} &\&& s = 1+x+\frac{x^2}{2}

\end{array}$$

As $y$ tends to infinity, $s$ tends to $e^x$.

**Hence, the correct answer is option B.**

@Pooja Thanks for compact answers.I have been following your answers. :) just a minute correction needed in the above answer when i=1 p=x.

@Pooja Palod good answer but just small improvement, Please put factorial symbol in denominator.

- All categories
- General Aptitude 1.2k
- Engineering Mathematics 4.7k
- Digital Logic 1.9k
- Programming & DS 3.5k
- Algorithms 3k
- Theory of Computation 3.7k
- Compiler Design 1.5k
- Databases 2.8k
- CO & Architecture 2.5k
- Computer Networks 2.9k
- Non GATE 837
- Others 1.2k
- Admissions 284
- Exam Queries 397
- Tier 1 Placement Questions 17
- Job Queries 51
- Projects 7

33,705 questions

40,253 answers

114,351 comments

38,862 users