CS100A Sections Dec Loop Invariant Review C Review and Example

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to Computers and Programming Lecture 9: For Loops New York University.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
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 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.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
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.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
 The initialization, loop-continuation condition and increment portions of a for statement can contain arithmetic expressions.  For example,
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
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.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
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.
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.
Programming with Recursion
Recursion.
UMBC CMSC 104 – Section 01, Fall 2016
Correctness issues and Loop invariants
An Introduction to Programming with C++ Sixth Edition
Lecture 7: Repeating a Known Number of Times
Arrays 2/4 By Pius Nyaanga.
Selenium WebDriver Web Test Tool Training
Data Structures in Java with JUnit ©Rick Mercer
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
9. FUNCTIONS.
Algorithm Analysis CSE 2011 Winter September 2018.
Counted Loops.
CS 213: Data Structures and Algorithms
Programming with Recursion
CMSC 202 Static Methods.
User-defined Functions
Applied Algorithms (Lecture 17) Recursion Fall-23
CSS161: Fundamentals of Computing
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
CS October 2008 The while loop and assertions
Outline Altering flow of control Boolean expressions
Testing change to a linked list
Ch 7: Quicksort Ming-Te Chi
User-defined Functions
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Chapter 6 Control Statements: Part 2
Lecture 5: For Loops Building Java Programs: A Back to Basics Approach
Object Oriented Programming
The structure of programming
Classes and Objects Static Methods
Binary Search and Loop invariants
CS100J Lecture 14 Previous Lecture
Chapter 18 Recursion.
Searching and Sorting Hint at Asymptotic Complexity
The structure of programming
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Searching and Sorting Hint at Asymptotic Complexity
Question 1a) What is printed by the following Java program? int s;
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.
CSC1401 Manipulating Pictures 2
Linear and Binary Search
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Thinking procedurally
Recursion.
Developing Loops from Invariants
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Presentation transcript:

CS100A Sections Dec 1-2 1998 Loop Invariant Review C Review and Example Recursion

Loop Invariant Review A loop invariant is a true-false statement (about the vari-ables used in a loop) that is true before and after each iteration. Given an invariant P, we can develop a loop with initialization in four steps: (1) Create initialization that makes P true. (2) Determine the condition B so that from !B and P you can see that the desired result is true. (Or, equivalently, determine the condition B such that you know the desired result is not true.) (3) Figure out what statement(s) to put in the loop body to make progress toward termination of the loop. (4) Figure out what else the loop body should do to en-sure that after execution of the loop body invariant P is still true. init; // invariant P: … S must make progress while ( B ) toward termination { S } and keep P true

Example of a loop developed from an invariant Problem. Sorted array b[0..n-1] may contain duplicates. Rearrange b[0..n-1] and store in k so that b[0..k-1] con-tains the non-duplicates of b[0..n-1]. Invariant P: (1) Initialization: Make first section empty item using k= 0; make second section contain 1 item using i= 0; (2) Stop when last section is empty, i.e. when i=n. So, continue as long as i != n. (3) Make progress using i= i+1. (4) Before incrementing i: if b[i] is not a duplicate, place it in section b[0..k-1]. k= 0; i= 0; while ( i != n) { if (k == 0 || b[k-1] != b[i] ) {b[k]= b[i]; k= k+1;} i= i+1; } Sorted. Contains all dis- tinct values of b[0..i-1], and has no duplicates these values haven’t been looked at 0 k i n b looked at, but not changed

a j b m <= sorted, >= (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: 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]; } a j b m <= sorted, >=

Each element b[m] (where 1<=m<=12) of array b[0 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, a 0 is in b[0], 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 31 days, then (5,2), i.e. day 5 of February, occurs on day 36 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 m= 1; n= 0; // Invariant P (see the problem description) while (m != month) { n= n + b[m]; m= m+1; } n= n + day;

Pointers and References in C By default, C methods are call_by_value for integer args void f (int x, int y) { int tmp; tmp = x; x=y; y=tmp; } void main (void) { int j=10; int k=20; f(j,k); No change to j and k as a result of the call to f.

10 20 Pointers and References in C What if you WANT f to modify j and k… j k &j is a pointer to j; j must be a variable type of j: int type of &j: pointer to int, int* So if you see int *p1; p1 = &j; int* p2 = &k; then *p1 is 10 (follow the pointer to the value) and *p2 is 20. 10 20

Pointers and References in C Rewrite f so that it modifies the arguments… void f (int* x, int* y) { int tmp; tmp = *x; *x=*y; *y=tmp; } void main (void) { int j=10; int k=20; f(&j, &k); Now j and k change as a result of the call to f.

C Example

Calls to One (a) One (a, c, b, d); (b) One (&(a+b), &c, b, d); (c) One (&a, &c, b, d); (d) One (&a, &d, b, d); (e) One (&b, &b, b, d); (a) Illegal. First and second arguments must be pointers to integer variables, not integer values. (b) Illegal. The & operator can only be applied to a variable, not an expression. (c) 20 2 1 10.0 1 2 20 8.0 (d) Second parameter must be a pointer to an integer. &d is a pointer to a double. (e) 20 2 20 10.0 1 20 7 8.0

Recursion Example Write a function prod that computes the product of integers between lo and hi inclusive. (Assume that lo <= hi.) Header: public static int product (int lo, int hi) Example: lo = 3 hi = 5 3 x 4 x 5 = 60 Algorithm: base case: if lo= hi, return lo otherwise: (go from lo to hi or vice versa) return ( lo * product (lo+1, hi)) Code: //computes the product of integers between lo and hi //inclusive. (Assume that lo <= hi.) public static int prod (int lo, int hi) { if (lo == hi) return lo; else return (lo * product (lo+1, hi)); }