Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS100A, Fall 1998. Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants.

Similar presentations


Presentation on theme: "CS100A, Fall 1998. Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants."— Presentation transcript:

1 CS100A, Fall 1998. Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants and developing a loop when the desired result and loop invariant are given. This handout is aimed at removing that trouble. Read it carefully and do the exer- cises in this handout. If there is anything you don’t un- derstand, see an instructor or TA or consultant immedi- ately. Important. Remember: a loop invariant is nothing more than a definition of the variables that are used in the loop, which shows the relationship between them. This defin- ition holds before and after each iteration of the loop. Checklist: We give a checklist for understanding a loop. Memorize it. It shows you how to understand loops, and it is also used to develop loops. The nice thing about the loop invariant is that it allows us to develop the initi- alization, the loop condition, and the loop body basically independently of each other. For the following loop with precondition Q and postcondition R to be correct, // Precondition: Q initialization; // invariant: P while (B) { S } // Postcondition R we need: 1. The initialization should truthify P. 2. If B is false, then with the help of P we see that result R is true. 3. Each iteration makes progress toward termination (gets closer to the point where B is false). 4. Each iteration keeps P true. Example: Linear search looks for x in b[0..m-1]. The result postcondition R is P: 0 <= h <= m and x is not in b[0..h-1] and (h = m or x = b[h] Use the loop invariant Initialization: To make P true, store in h to make the first section empty: h= 0; Loop condition: The loop should continue as long as the second array section is not empty and x doesn’t equal b[h]: h != m && x != b[h] Get closer to termination: h= h+1; Keep P true: nothing to do. Final loop: h= 0; while (h != m && x != b[h]) S{h= h+1;} Important hint! Memorize: * Array section b[h..k] has one element if h = k. * Array section b[h..k] is empty if h = k+1. Important point. Practise!! Some of you have difficulty determining when some of the four array sections given below are empty: In order to determine whether a section is empty, first put it in the form b[a..b] and then use the important hint given above. Example: The right most section is b[n+1..m-1]. It is empty when n+1 = m-1+1, i.e. when n= m-1. Practice determining when a section is empty and when it has one element until your can do it in your sleep. How to study this material on loops. 1. Study the previous page and memorize everything that it says to memorize. 2. Memorize the following definitions (you have to know them for the final, anyway) that concern a loop while ( C ) { S }. x is not here b 0 h m 0 h k n m b

2 CS100A, Fall 1998. Review of loops 2 Loop condition: C Loop body: S Iteration of a loop: One execution of loop body S (when C is true). Loop invariant: An assertion (true-false statement) that is maintained by each iteration --if the invariant is true before an iteration, it is true after iteration. 3. Look through the lecture notes for the development of other loops from loop invariants. Practice developing these, too. 4. Exercises. Answers are after the exercises. (a) Write an algorithm to store the sum of the elements of b[h..k-1] in x. Use the loop invariant P: P: h <= j <= k+1 and x is the sum of b[h..j-1], i.e. (b) Write an algorithm to store the sum of the elements of b[h..k] in x. Use the loop invariant P: P: h <= j <= k and x is the sum of b[h..j-1], i.e. (c) Write an algorithm to store the sum of the elements of b[h..k-1] in x. Use the loop invariant P: P: h <= j <= k and x is the sum of b[j..k-1], i.e. (d) Write an algorithm to store the sum of the elements of b[h..k-1] in x. Use the loop invariant P: P: h-1 <= j <= k and x is the sum of b[j+1..k-1], i.e. (e) Write an algorithm that, given n >= 1, stores in j the smallest power of 2 that is greater than n. (Note: the pow- ers of 2 are 1, 2, 4, 8, 16, … ). Use the loop invariant P: P: j is a power of 2 and the powers of 2 that are less than j are at most n (f) Write an algorithm that, given n >= 0, stores in j the smallest square that is greater than n. (Note: the squares are 0, 1, 4, 9, 16, 25, … ). Use the loop invariant P: P: 1 <= j and (j-1)*(j-1) <= n (g) It is known that x is in b[h..k]. Write an algorithm to store in j the index of the last occurrence of x in b[h..k]. Use the loop invariant: P: h <= j <= k and x is not in b[j+1..k], i.e. (h) Write an algorithm that, given a<b, stores the maxi- mum value of m[a..b-1] in x. Use the loop invariant P: P: x is the maximum of m[a..k-1] An empty segment has no maximum, so it makes sense to initialize so that m[a..k-1] has one value in it. x is sum of these b h j k x is sum of these b h j k x is sum of these b h j k x is sum of these b h j k x is not in this section b h j k

3 CS100A, Fall 1998. Review of loops 3 (i) Write an algorithm that, given a<=b, stores the maximum value of m[a..b] in x. Use the loop invariant P: x is the maximum of m[k..b] (j) Write an algorithm that, given a<=b, stores the index of the maximum value of m[a..b] in h. Use the loop invariant P: m[h] is the maximum of m[k..b] (k) (A version of selection sort). Write an algorithm that, given a <= b+1, sorts m[a..b]. Use the invariant P given below. Do not write a loop within the body of the main loop; instead, write commands in the body of the loop as high-level statements, in English. P: (l) Write an algorithm that stores into ev the number of even elements in b[0..n] and the number of odd elements into od. Use the invariant P: P: ev is the number of even values in b[j..n] and od is the number of odd values in b[j..n] (m) The rows of two-dimensional integer array b[ ] [ ] is completely allocated. Its rows need not be the same size. Store in h the number of the row with fewest elements. Use the following invariant P: P: row h has the fewest elements of rows 0..j (n) Each element b[m] (where 1<=m<=12) of array b[0..12] contains the number of days in month m. (January is month 1, etc.) Given a date (day, month) in two variables day and month, store in n the day of the year on which (day, month) occurs. For example, if January has 30 days, then (5,2), i.e. day 5 of February, occurs on day 35 of the year, and if February has 28 days, then (10,3) occurs on day 68 of the year. Your algorithm will need a loop plus some other statements. For the loop, use the loop invariant P: P: 1 <= m <= month and n is the total number of days in months 1..m-1 (o) The Fibonacci numbers F(0), F(1), F(2), … are 0, 1, 2, 3, 5, 8, 13, …. Each, except the first two, is the sum of the preceding two numbers in the sequence. For example, 3+5 = 8. Write an algorithm that, given n>=1, stores Fibonacci number F(n) in variable x. Use the loop invariant P: P: 1 <= h <= n and x = F(h) and y = F(h-1) (p) Remember that mod(n,m) is the integer r that satisfies 0 <= r < m and n = q* m + r (for some integer q) From this definition, we can infer: mod(x,m) = mod(x-m,m) and if 0 <= x < m, then mod(x,m) = x Write an algorithm that, given n>=0 and m>1, stores mod(n,m) in variable r. The algorithm should not use the remainder operator %; instead, it should consist of a single loop (with initialization) that relies on the loop invariant: P: 0 <= r <= n and mod(r,m) = mod(n,m) Hint: you can trutify the invariant with r= n. Answers to exercises (a) j= h; x= 0; // Invariant P (see the problem description) while (j != k+1) /* or j < k+1 */ {x= x + b[j]; j= j+1;} (b) j= h; x= 0; // Invariant P (see the problem description) while (j != k) /* or j < k */ {x= x+ b[j]; j= j+1;} (c) j= k; x= 0; // Invariant P (see the problem description) while (h != j) /* or h < j */ {j= j-1; x= x+b[j]; } = b a j b

4 CS100A, Fall 1998. Review of loops 4 (d) j= k-1; x= 0; // Invariant P (see the problem description) while (h <= j) {x= x+b[j]; j= j-1; } (e) j= 1; // Invariant P (see the problem description) while (j <= n) j= 2*j; (f) j= 1; // Invariant P (see the problem description) while ( j*j <= n) j= j+1; j= j*j; (g) j= k; // Invariant P (see the problem description) while (x != b[j]) j= j-1; (h) k= a+1; x= m[a]; // Invariant P (see the problem description) while (k != b) { if (m[k] > x) x= m[k]; k= k+1; } (i) k= b; x= m[b]; // Invariant P (see the problem description) while (k != a) { k= k-1; if (m[k] > x) x= m[k]; } (j) k= b; h= b; // Invariant P (see the problem description) while (a != k) { k= k-1; if (m[h] < m[k]) h= k; } (k) j= b+1; // Invariant P (see the problem description) while ( a < j ) { j= j-1; Store in h the index of the maximum value of m[a..j]; Swap m[h] and m[j]; } (l) j= n+1; ev= 0; od= 0; // Invariant P (see the problem description) while (j > 0) { j= j-1; if (b[j] % 2 = 0) ev= ev+1; else od= od+1; } (m)h= 0; j= 0; // Invariant P (see the problem description) while (j < b.length-1) { j= j+1; if ( b[h].length > b[j] ) h= j; } (n) m= 1; n= 0; // Invariant P (see the problem description) while (m != month) { n= n + b[m]; m= m+1; } n= n + day; (o)h= 1; y= 0, x= 1; // Invariant P (see the problem description) while ( h != n) { h= h+1; int t= x; x= x+y; y= t; } (p) r= n; // Invariant P (see the problem description) while (r >= m) {r= r-m;}


Download ppt "CS100A, Fall 1998. Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants."

Similar presentations


Ads by Google