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.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

One Dimensional Arrays
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
1 CS March 2009 While loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim in two days: Th 7:30-9pm Uris Aud.
1 CS100J 13 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
1 CS100J 27 March 2007 Algorithms on arrays Reading: 8.3–8.5 Please punctuate this: Dear John, I want a man who knows what love is all about you are generous.
1 CS100J 15 March, 2006 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal.
CS 1110 Prelim III: Review Session 1. Info My name: Bruno Abrahao – We have two other TA’s in the room to help you individually Beibei Zhu Suyong Zhao.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Algorithm Efficiency and Sorting
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
1 Chapter 7 Recursion. 2 What Is Recursion? l Recursive call A method call in which the method being called is the same as the one making the call l Direct.
1 Assignment 6. Developing Loops Due on Tuesday, 30 October, by midnight (submitted electronically). Note that each question will be graded on the following.
Floyd Hoare Logic. Semantics A programming language specification consists of a syntactic description and a semantic description. Syntactic description:symbols.
1 Algorithmic analysis Introduction. This handout tells you what you are responsible for concerning the analysis of algorithms You are responsible for:
1 CS100J 25 October 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
1 CS100J 29 March 2007 Arrays: searching & sorting. Reading: 8.5 Why did I get a Christmas Card on Halloween? Searching and sorting algorithms are on the.
1 CS October 2008 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Quotes for the Day:
Reading and Writing Mathematical Proofs
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
1 CS100J 31 October 2006 Arrays: searching & sorting. Reading: 8.5 Merry Christmas!! On Halloween? Searching and sorting algorithms are on the course website.
CS 100Lecture 131 Announcements Exam stats P3 due on Thursday.
1 On (computational) simplicity We are trying to teach not just Java, but how to think about problem solving. Computer science has its field called computational.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
ALGORITHMS.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 26.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
1 CS100J September 27, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
1 CS100J 06 March 2008 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 27.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
1 CS April 2010 insertion sort, selection sort, quick sort Do exercises on pp to get familiar with concepts and develop skill. Practice.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
1 CS Oct 2008 Arrays. Reading: Secs 8.1, 8.2, 8.3 Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long, and.
List Algorithms Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Correctness issues and Loop invariants
CS100J 30 October 2007 Algorithms on arrays Reading: 8.3–8.5
Developing Loops from Invariants
CS October 2008 The while loop and assertions
List Algorithms Taken from notes by Dr. Neil Moore
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Asymptotic complexity Searching/sorting
Output Variables {true} S {i = j} i := j; or j := i;
Binary Search and Loop invariants
Asymptotic complexity
CS November 2010 Developing array algorithms. Reading:
Searching and Sorting Hint at Asymptotic Complexity
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Searching and Sorting Hint at Asymptotic Complexity
Developing loops from invariants
CS100A Lecture 25 1 December 1998 Chance to replace grade on Prelim 3, Question 2. Everyone is expected to be in lecture on Thursday, 3 December.
Linear and Binary Search
CS100J Nov 04, 2003 Arrays. Reading: 8
CS100A Sections Dec Loop Invariant Review C Review and Example
Developing Loops from Invariants
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
CS October 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
Presentation transcript:

CS100A, Fall 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

CS100A, Fall 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

CS100A, Fall 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

CS100A, Fall 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;}