Consider the following algorithm assume N to be a positive i
Consider the following algorithm; assume N to be a positive integer. 1. X 0 2. Y 1 3. WHILE (X < N) a. X X + 1 b. Y Y + 2 X 4. Y Y / N Calculate what value of Y the algorithm will compute for the following values of N. Explain your solution. a) N = 3 b) N = 5
Solution
1. X 0
2. Y 1
3. WHILE (X < N)
a. X X + 1
b. Y Y + 2 X
4. Y Y / N
Solution: for N =3
initially x0 = 0 and y0 = 1
in while loop steps the values changes as shown below
step1 : X1 = 1 and Y1 = Y0 + 2*X1 = 1+2*1 = 3
step2 : X2= 2 and Y2 = Y1+ 2*X2= 3+2*2 = 7
step3 : X3= 3 and Y3 = Y2+ 2*X3= 7+2*3 = 13
when the loop ends Y4 = Y3 /3 = 13/3 = 4.3
finall value of Y becomes 4.3
similarlly for N = 5 we can see that finall value of Y will be Y6 = 31/5 = 6.2
