SolHW3f07 - HW3F07 CS336 1. The following program computes...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: HW3F07 CS336 1. The following program computes the quotient q and remainder r of the division of x by y . { x < y } q,r := 0 ,x ; do r y r,q := r- y,q + 1 od { r < y q * y + r = x } What would you have to prove to show that 0 < y r q * y + r = x is an invariant of the loop? Set up a fully instantiated version of the predicate to be proved true (but dont prove). How would you show that if the loop terminates the postcondition is established? Again, set up a fully instantiated version of the predicate to be proved, but dont prove it. Solution: Let P be 0 < y r q * y + r = x . To show that P is an invariant, we need to show the following two things: P holds before loop execution begins. Fully instantiated version: x < y wp ( q,r := 0 ,x , < y r q * y + r = x ) P holds after each loop iteration. Fully instantiated version: < y r q * y + r = x r y wp ( r,q := r- y,q +1 , < y r q * y + r = x ) To show that the postcondition is established if the loop terminates, we need to show that P BB R . The fully instantiated version is < y r q * y + r = x r < y r < y q * y + r = x 2. Prove or disprove the correctness of the following algorithm. { Q : 0 < n } i := 1; { P : 1 i n ( p | : i = 2 p ) } do 2 * i n i := 2 * i od { R : 1 i n 2 * i ( p | : i = 2 p ) } Solution: We shall show that the algorithm is correct. The purpose of the algorithm is to find the largest power of 2 that is at most n . We need to show the following three things: 1 a. P holds before loop execution begins: { Q } i := 1 { P } . b. P holds after each loop iteration: { P 2 * i n } i := 2 * i { P } . c. P holds after the loop: P 2 * i > n R . a. { Q } i := 1 { P } < wp, instantiation > < n wp ( i := 1 , 1 i n ( p | : i = 2 p )) < wp > < n 1 1 n ( p | : 1 = 2 p ) < arith: p=0 > < n 1 1 n T < arith ,...
View Full Document

This note was uploaded on 09/20/2008 for the course CS 336 taught by Professor Myers during the Spring '08 term at University of Texas at Austin.

Page1 / 6

SolHW3f07 - HW3F07 CS336 1. The following program computes...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online