Question

For this pseudocode present a loop invariant and prove it, n >= 0. i <= 0;...

For this pseudocode present a loop invariant and prove it, n >= 0.

i <= 0;

s <=2;

while i < n do

i <= i + 1

s <= s * s

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The best loop invariant for this while loop is n-i > 0. Because the loop executes when the value of i is less than n. It terminates once when the i value becomes equal to n.

For example, if n = 5. i = 0 starts with 0.

Iteration 1 : n > i , i = 0, 5 - 0 > 0

Iteration 2 : n > i , i = 1, 5 - 1 > 0

Iteration 3 : n > i , i = 2, 5 - 2 > 0

Iteration 4 : n > i , i = 3, 5 - 3 > 0

Iteration 5 : n > i , i = 4, 5 - 4 > 0

Iteration 6 : n > i , i = 5, 5 - 5 = 0 --> Loop terminates.

From iteration 1 to 5 the loop invariant n - i > 0 holds.

**Comment for any further queries.

Add a comment
Know the answer?
Add Answer to:
For this pseudocode present a loop invariant and prove it, n >= 0. i <= 0;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT