Vous êtes sur la page 1sur 2

CSC 165 H1Y Worth: 3%

Homework Exercise # 6 Sample Solutions

Summer 2012

Due: By 3 pm on Tuesday July 24.

1. (a) Write a detailed argument that shows that the loop invariant holds just before the loop condition is evaluated for the rst time, under the assumption that the precondition is true. The code that we talk about in this sub-question is lines 1 3. It does not change x and y, and a, b and z dont have values before the code is executed. So, we can use the variable names to talk about the variables after line 3. (Of course, we could also dene v to be the value of a variable v just after line 3, and v to be the value just before line 1. Then x = x, y = y, a = x, b = y and z = 1, and a, b and z are undened). Assume x N+ y N+ Then, z (ab ) = 1(ab ) =x (ab )
b y

# z = 1 by line 3

# a = x by line 1

= x # b = y by line 2 Then, z = xy Then, (x N+ y N+ ) (z (ab ) = xy ) (b) Assuming that the loop invariant is correct, write a detailed argument that shows that the postcondition will be satised once the loop terminates. The code between the end of the loop and the return statement is empty, so again we dont need any special notation. (Of course, we could still say that v = v for any variable v). The loop invariant is z (ab ) = xy , and if the loop terminates, then b = 0 Assume z (ab ) = xy b = 0 Then, xy = z (ab ) # Loop Invariant = z(a0 ) =z Then, z = xy Then, z (ab ) = xy b = 0 z = xy (c) Write a detailed argument that shows that the loop invariant is correct. That is, show that the loop invariant is true each time the program evaluates the loop condition. Here we need to show that for an arbitrary loop iteration, if the loop invariant was true at the beginning of the iteration, it is true at the end of that iteration. Let i be an arbitrary iteration. Let us say that the variables a, b and z will refer to the values of these variables when the loop condition is evaluated at the beginning of the it h iteration. Let us say that the variables a , b and z will refer to the values of these variables when the loop condition is evaluated just after the ith iteration. The code we are considering is lines 59. The variables x and y are unchanged, so we dont need separate variables to refer to them. We want to prove that if the loop invariant holds at the beginning of the ith iteration, it will hold at its end. So, we want to prove: z (ab ) = xy z (a b ) = xy Assume z (ab ) = xy Then, either b is even or odd. Case 1. b is even. Then, b % 0 == 0, so lines 6 and 7 will execute. Then, a = a a # line 6 Then, b = b/2 = b/2 # line 7, b is even Then, z = z a # z is unchanged Dept. of Computer Science, University of Toronto, St. George Campus Page 1 of 2 #b=0

CSC 165 H1Y

Homework Exercise # 6 Sample Solutions Then, z (a ) = (z)(a)b/2 = z(a2 )b/2 = za2b/2 = zab = xy # Loop Invariant Case 1. b is odd. Then, b % 0 = 0, so lines 8 and 9 will execute. Then, a = a # a is unchanged Then, b = b 1 # line 8 Then, z = z a # line 9 b Then, z (a ) = (z a)ab1 = z(a ab1 ) = z(ab1+1 ) = zab = xy Then, in both cases, z (a ) = xy
b b

Summer 2012

# Loop Invariant

Then, z (ab ) = xy z (a b ) = xy (d) In order to prove that the algorithm is correct, we need to show that the loop eventually terminates. Provide an (informal) argument that shows that the loop condition is eventually false. We need two observations: that b is never negative, and that it strictly decreases after each iteration of the loop. There are only two operations ever performed on b: it is either divided 2 on line 7, or decremented by 1 on line 8. If the loop is still executing, then b = 0 at the beginning of the loop. Then, b > 0, and either of these operations would produce a new value of b which is non-negative, and strictly smaller (note that division by 2 is integer division, which rounds down alternatively, we can note that its only done only when b is even). So, b must eventually reach 0, at which point the loop will terminate. The question does not ask for a formal proof, but it is possible to formally prove that for any iteration of the loop, b 0 b = 0 b 0 b < b.

Dept. of Computer Science, University of Toronto, St. George Campus

Page 2 of 2

Vous aimerez peut-être aussi