CS1110 22 October 2009 Read: Sec. 2.3.8 and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.

Slides:



Advertisements
Similar presentations
© Trainer Bubble. Cryptic anagram puzzles (the clues are in the questions): dirty room here come dots lost.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
1 CS100J February 28, 2006 Loops, iterative statements, or repetitive statements A bit about graphics From news.com, on 23 February 2006, about browser.
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.
CS100J October 07, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
1 CS100J October 06, 2005 For Loops Reading: Secs Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself.
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.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
The If/Else Statement, Boolean Flags, and Menus Page 180
1 Assignment 6. Developing Loops Due on Tuesday, 30 October, by midnight (submitted electronically). Note that each question will be graded on the following.
1 CS1110, 20 Oct. 2009, Lec 14 Elementary graphics; intro to loops and for-loops Reading: Sec and chapter 7 on loops. The lectures on the ProgramLive.
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:
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
1 CS1110, 8 March 2009 Two topics: elementary graphics (for A5); loops Reading: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
1 CS March 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
1 CS100J 4 March 2008 Two topics: Turtles; loops Start reading Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
Chapter 3 Part II Describing Syntax and Semantics.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
1 CS100J 18 October 2005 Arrays Reading: You are responsible for: Secs 8.1, 8.2, 8.3, 8.4 A decimal point I'm a dot in placeSlot machines Cash lost in.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
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 29 March 2005 Arrays Reading: You are responsible for: Secs 8.1, 8.2, 8.3, 8.4 A decimal point I'm a dot in placeSlot machines Cash lost in 'em.
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.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
CS100A, Fall Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
Repetition Statements
Chapter 3 of Programming Languages by Ravi Sethi
Correctness issues and Loop invariants
Start reading Sec and chapter 7 on loops
Introduction To Repetition The for loop
Pamela Moore & Zenia Bahorski
Reasoning About Code.
Specifications What? Not how!.
Engineering Innovation Center
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Developing Loops from Invariants
Looping and Repetition
CS October 2008 The while loop and assertions
Testing change to a linked list
CS 220: Discrete Structures and their Applications
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
Chapter 6: Repetition Statements
Binary Search and Loop invariants
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.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Data Types and Maths Programming Guides.
CS100A Sections Dec Loop Invariant Review C Review and Example
Developing Loops from Invariants
CS100J 16 Oct, 2007 Assignment A5: loops
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:

CS1110 22 October 2009 Read: Sec. 2.3.8 and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm a dot in place Animosity Is no amity Debit card Bad credit Desperation A rope ends it Dormitory Dirty room Funeral Real fun Schoolmaster The classroom Slot machines Cash lost in 'em Statue of liberty Built to stay free Snooze alarms Alas! No more Z's The Morse code Here come dots Vacation times I’m not as active Western Union No wire unsent George Bush He bugs Gore Parishioners I hire parsons The earthquakes That queen shake Circumstantial evidence Can ruin a selected victim Victoria, England’s queen Governs a nice quiet land Eleven plus two Twelve plus one (and they have 13 letters!)

Week of 7 Dec: National Computer Science Education Week U.S. House of Representatives resolution H. RES. 558 to raise profile of CS as a transforming industry that drives technology innovation and bolsters economic productivity. Week of 7 Dec: National Computer Science Education Week  Resolution cites influence of computing technology as a significant contributor to U.S. economic output, calls on educators and policy makers to improve CS learning at all educational levels and to motivate increased participation in computer science. ACM Computing Research Association (CRA) CS Teachers Association (CSTA) National Center for Women & Information Technology (NCWIT) Microsoft, Google, Intel

Assertion: true-false statement (comment) asserting a belief about (the current state of) your program. // x is the sum of 1..n <- asserts a specific relationship between x and n Assertions help prevent bugs by helping you keep track of what you’re doing … … and they help track down bugs by making it easier to check belief/code mismatches. Assertions can help with bugs in loops: initialization errors, termination errors, and processing errors. x ? n 1 x ? n x ? n 3

Precondition: assertion placed before a segment Postcondition: assertion placed after a segment 1 2 3 4 5 6 7 8 x contains the sum of these (6) n precondition // x = sum of 1..n-1 x= x + n; n= n + 1; // x = sum of 1..n-1 Pre/post definition: it should be that if the pre-condition is true, then execution of the segment implies that the postcondtion holds. So, if before we had x=6, n=4, then after we add n to x (i.e., add 4 to x to get x==10) and add 1 to to n to get n==5, the postcondition is stil true. n 1 2 3 4 5 6 7 8 x contains the sum of these (10) postcondition

Solving a problem A. x= x + 1; B. x= x + n; C. x= x + n+1; // x = sum of 1..n n= n + 1; // x = sum of 1..n precondition postcondition What statement do you put here so that segment is correct? (if precondition is true, execution of segment should make postcondition true.) A. x= x + 1; B. x= x + n; C. x= x + n+1; D. None of A, B, C E. I can’t figure it out

Solving a problem A. x= x + 1; B. x= x + n; C. x= x + n+1; // x = sum of 1..n-1 n= n + 1; // x = sum of 1..n-1 precondition postcondition What statement do you put here so that segment is correct? (if precondition is true, execution of segment should make postcondition true.) A. x= x + 1; B. x= x + n; C. x= x + n+1; D. None of A, B, C E. I can’t figure it out

Invariants: another type of assertion An invariant is an assertion about the variables that is true before and after each iteration (execution of the repetend). x= 0; for (int i= 2; i <= 4; i= i +1) { x= x + i*i; } i= 2; i <= 4 i= i +1; true false x= x + i*i; Invariant: x = sum of squares of 2..i-1

for (int k= a; k <= b; k= k + 1) { Process integer k; } // Process integers in a..b Command to do something equivalent post-condition // inv: the integers in a..k-1 have been processed for (int k= a; k <= b; k= k + 1) { Process integer k; } // post: the integers in a..b have been processed

Methodology for developing a for-loop Recognize that a range of integers b..c has to be processed Write the command and equivalent postcondition. 3. Write the basic part of the for-loop. 4. Write loop invariant. Figure out any initialization. Initialize variables (if necessary) to make invariant true. 6. Implement the repetend (Process k). // Process b..c // Postcondition: range b..c has been processed // Invariant: range b..k-1 has been processed for (int k= b; k <= c; k= k+1) { // Process k }

// Store in double variable v the sum Finding an invariant // Store in double variable v the sum // 1/1 + 1/2 + 1/3 + 1/4 + 1/5 + … + 1/n for (int k= 1; k <= n; k= k +1) { Process k; } // v =1/1 + 1/2 + … + 1/n Command to do something and equivalent postcondition v= 0; // invariant: v = sum of 1/i for i in 1..k-1 What is the invariant? 1 2 3 … k-1 k k+1 … n

// set x to no. of adjacent equal pairs in s[0..s.length()-1] Finding an invariant // set x to no. of adjacent equal pairs in s[0..s.length()-1] Command to do something and equivalent post-condition for s = ‘ebeee’, x = 2. // invariant: for (int k= 0; k < s.length(); k= k +1) { Process k; } // x = no. of adjacent equal pairs in s[0..s.length()-1] k: next integer to process. Which ones have been processed? 0..k C. a..k 0..k–1 D. a..k–1 What is the invariant? x = no. adj. equal pairs in s[1..k] x = no. adj. equal pairs in s[0..k] x = no. adj. equal pairs in s[1..k–1] x = no. adj. equal pairs in s[0..k–1]

// { String s has at least 1 char } 1. What is the invariant? Being careful Command postcondition // { String s has at least 1 char } // Set c to largest char in String s // inv: for (int k= ; k < s.length(); k= k + 1) { // Process k; } // c = largest char in s[0..s.length()–1] 2. How do we initialize c and k? k= 0; c= s.charAt[0]; k= 1; c= s.charAt[0]; k= 1; c= s.charAt[1]; k= 0; c= s.charAt[1]; None of the above c is largest char in s[0..k–1] (note that initializing with 0 turns out to just mean you’re maintaining a different invariant, as opposed to an actual error occurring (because the case of k==0 will cause everything to become initialized correctly.) An empty set of characters or integers has no maximum. Therefore, be sure that 0..k–1 is not empty. Therefore, start with k = 1.