1 CS1110 10 March 2010 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 pointI'm.

Slides:



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

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.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Discrete Structures Chapter 2 Part B Mathematical Induction
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.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
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:
Reading and Writing Mathematical Proofs
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
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.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
CSIS 113A Lecture 5 Random Numbers, while, do-while.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
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.
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 CMSC 250 Chapter 2, Predicate Logic. 2 CMSC 250 Definitions l Subject / predicate John / went to the store. The sky / is blue. l Propositional logic-
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Basic Java Syntax Comments Basic data types Operators and assignment.
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.
Midterm preview. double int = 2.0; True / FalseThe following is a syntactically correct variable declaration and assignment statement:
COMP Loop Statements Yi Hong May 21, 2015.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
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.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
Introduction to Computing Systems and Programming Programming.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
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.
CS100J October 21, 2003 For Loops Reading: Secs Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
Correctness issues and Loop invariants
Start reading Sec and chapter 7 on loops
Bill Tucker Austin Community College COSC 1315
CS October 2009 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.
Specifications What? Not how!.
LOOPS.
Developing Loops from Invariants
Using variables, for..loop and functions to help organize your code
CS October 2008 The while loop and assertions
Testing change to a linked list
Asymptotic complexity Searching/sorting
Loops.
Recursion (Continued)
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.
Chapter 3 Debugging Section 3.4
Class code for pythonroom.com cchsp2cs
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.
Chapter 4: Loops and Iteration
Presentation transcript:

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 a dot in placeAnimosity Is no amity Debit cardBad creditDesperation A rope ends it DormitoryDirty roomFuneral Real fun Schoolmaster The classroomSlot machines Cash lost in 'em Statue of libertyBuilt to stay free Snooze alarms Alas! No more Z's The Morse code Here come dotsVacation times I’m not as active Western Union No wire unsentGeorge BushHe bugs Gore ParishionersI hire parsonsThe earthquakes That queen shake Circumstantial evidence Can ruin a selected victim Victoria, England’s queenGoverns a nice quiet land Eleven plus twoTwelve plus one (and they have 13 letters!)

2 A4: mean: 96.7 median: 98 std dev: 6.2 A4: mean: 94.7 median: 98 std dev: students’ time: min 1.5 med 7 mean 7.2 max 15 (4 people) Today’s terminology: assertion: true-false statement, sometimes placed in a program to assert that it is true at that point. precondition: assertion placed before a statement postcondition: assertion placed after a statement loop invariant: assertion supposed to be true before and after each iteration of the loop iteration of a loop: one execution of its repetend And we give you a methodology for developing for-loops.  Last sem This sem 

3 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 x ? n 3 x ? n 0 x ? n 1 assert ; Java assert statement. To execute: if the bool exp is false, stop with an error message

4 Precondition: assertion placed before a segment Postcondition: assertion placed after a segment // x = sum of 1..n-1 x= x + n; n= n + 1; // x = sum of 1..n-1 precondition postcondition x contains the sum of these (6) n n x contains the sum of these (10) Meaning: if precondition is true, then after executing the segment the postcondition will be true

5 Solving a problem // 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

6 Solving a problem // 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

7 Invariants: another type of assertion 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 An invariant is an assertion about the variables that is true before and after each iteration (execution of the repetend). Invariant: x = sum of squares of 2..i-1 x= 0; The loop processes the range 2..4 in terms of the range of integers that have been processed so far

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

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

10 Finding an invariant // Store in b the value of: “no int in 2..n-1 divides n // b = “no int in 2..n-1 divides n” // invariant: b = no int in 2..k-1 divides n Command to do something and equivalent postcondition What is the invariant? … k-1 k k+1 … n for (int k= 2; k < n; k= k +1) { Process k; } b= true; // if (n%k == 0) b= false;

11 Finding an 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] What is the invariant? A.x = no. adj. equal pairs in s[1..k] B.x = no. adj. equal pairs in s[0..k] C.x = no. adj. equal pairs in s[1..k–1] D.x = no. adj. equal pairs in s[0..k–1] // invariant: for s = ‘ebeee’, x = 2. k: next integer to process. Which ones have been processed? A. 0..k C. a..k B. 0..k–1 D. a..k–1 // set x to no. of adjacent equal pairs in s[0..s.length()-1] Command to do something and equivalent post- condition

12 Being careful // { 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] c is largest char in s[0..k–1] 1. What is the invariant? 2. How do we initialize c and k? A.k= 0; c= s.charAt[0]; B.k= 1; c= s.charAt[0]; C.k= 1; c= s.charAt[1]; D.k= 0; c= s.charAt[1]; E.None of the above 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. Command postcondition