GATE 1993
January 31, 2024GATE 1996
February 1, 2024GATE 2003
Question 1
|
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
Xy
|
|
ex
|
|
ln(1+x)
|
|
Xx
|
Question 1 Explanation:
P = P * (x/i)
S = S+P
Iteration 1:
P=1; i=1; S=1
P=x
S = 1+x
Iteration 2:
P=x; S = 1+x; i=2
P = x * x/2 = x2/2
Iteration 3:
P = x2/2; S = 1+x+x2/2; i=3
P = (x2/2)(x/3) = x3/6
S = 1 + x + x2/2 + x3/6
Continue upto n
Then f( ) returns:
S = 1 + x/1 + x2/2⋅1 + x3/3⋅2 + …
= 1 + x/1! + x2/2! + x3/3! + … + xn/n!
= ex
S = S+P
Iteration 1:
P=1; i=1; S=1
P=x
S = 1+x
Iteration 2:
P=x; S = 1+x; i=2
P = x * x/2 = x2/2
Iteration 3:
P = x2/2; S = 1+x+x2/2; i=3
P = (x2/2)(x/3) = x3/6
S = 1 + x + x2/2 + x3/6
Continue upto n
Then f( ) returns:
S = 1 + x/1 + x2/2⋅1 + x3/3⋅2 + …
= 1 + x/1! + x2/2! + x3/3! + … + xn/n!
= ex
Correct Answer: B
Question 1 Explanation:
P = P * (x/i)
S = S+P
Iteration 1:
P=1; i=1; S=1
P=x
S = 1+x
Iteration 2:
P=x; S = 1+x; i=2
P = x * x/2 = x2/2
Iteration 3:
P = x2/2; S = 1+x+x2/2; i=3
P = (x2/2)(x/3) = x3/6
S = 1 + x + x2/2 + x3/6
Continue upto n
Then f( ) returns:
S = 1 + x/1 + x2/2⋅1 + x3/3⋅2 + …
= 1 + x/1! + x2/2! + x3/3! + … + xn/n!
= ex
S = S+P
Iteration 1:
P=1; i=1; S=1
P=x
S = 1+x
Iteration 2:
P=x; S = 1+x; i=2
P = x * x/2 = x2/2
Iteration 3:
P = x2/2; S = 1+x+x2/2; i=3
P = (x2/2)(x/3) = x3/6
S = 1 + x + x2/2 + x3/6
Continue upto n
Then f( ) returns:
S = 1 + x/1 + x2/2⋅1 + x3/3⋅2 + …
= 1 + x/1! + x2/2! + x3/3! + … + xn/n!
= ex
Subscribe
Login
0 Comments