# HW03Sol - Homework 3 Solutions CS 336 Spring 2008 1. The...

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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Homework 3 Solutions CS 336 Spring 2008 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 ) } 1 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: 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....
View Full Document

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

### Page1 / 7

HW03Sol - Homework 3 Solutions CS 336 Spring 2008 1. The...

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

View Full Document
Ask a homework question - tutors are online