3,130 views
20 votes
20 votes

Consider the following psuedocode fragment, where $y$ is an integer that has been initialized.

int i=1
int j=1
while (i<10):
    j=j*i
    i=i+1
    if (i==y):
        break
    end if
end while

Consider the following statements:

  1. $(i==10)$ or $(i==y)$
  2. If $y > 10$, then $i==10$
  3. If $j=6$, then $y==4$

Which of the above statements is/are TRUE at the end of the while loop? Choose from the following options.

  1. i only
  2. iii only
  3. ii and iii only
  4. i, ii, and iii
  5. None of the above

5 Answers

0 votes
0 votes

The loop invariant we are getting is

 

j=(i-1)! or Gamma function of i

as Gamma(i) = (i-1)!

 

Now try to see yourself

All three are correct as all three are coming out of the loop

 

  1. j=6 means it is 3! which is (4-1)! and this means i=4 or y=4 , break happened, hence we came out of loop
  1. when i=10 or y=10, we come out of loop
  2. i==10 for all y>10 this condition is also true for coming out of loop

So all three are loop invariants.

 

 

Answer:

Related questions

12 votes
12 votes
2 answers
1
36 votes
36 votes
6 answers
4