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.

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

Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Programming with Recursion
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
1 Executing Method Calls This lecture tells you precisely how method calls are executed (a few details will have to wait until we get to classes and objects).
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
1 Algorithmic analysis Introduction. This handout tells you what you are responsible for concerning the analysis of algorithms You are responsible for:
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
RECURSION Lecture 7 CS2110 – Spring Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
19-Aug-15 Simple Recursive Algorithms. 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
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:
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
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.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
RECURSION Lecture 7 CS2110 – Fall Overview references to sections in text 2  Note: We’ve covered everything in JavaSummary.pptx!  What is recursion?
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
RECURSION, CONTINUED Lecture 8 CS2110 – Fall 2015.
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.
CS100A, Fall Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Program Development and Design Using C++, Third Edition
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
1 Recursion and induction We teach these early, instead of new object- oriented ideas, so that those who are new to Java can have a chance to catch up.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
RECURSION (CONTINUED) Lecture 9 CS2110 – Spring 2016.
CS 100Lecture 201 Announcements P5 due Thursday Final exam: Tuesday August 10th, 8AM, Olin 155, 2 hours Tomorrow, Wednesday: C, pointers Thursday, Friday:
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Programming with Recursion
Recursion (Continued)
Chapter Topics Chapter 16 discusses the following main topics:
Recursion Version 1.0.
Topic 6 Recursion.
Chapter 15 Recursion.
Correctness issues and Loop invariants
Computer Science 4 Mr. Gerb
Start reading Sec and chapter 7 on loops
Chapter 15 Recursion.
Recursion (Continued)
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Recursion (Continued)
Programming with Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
CS October 2008 The while loop and assertions
Testing change to a linked list
Building Java Programs
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Recursion (Continued)
Recursion (Continued)
Yan Shi CS/SE 2630 Lecture Notes
Dr. Sampath Jayarathna Cal Poly Pomona
Recursion Method calling itself (circular definition)
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
CS100A Sections Dec Loop Invariant Review C Review and Example
Presentation transcript:

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. A short quiz concerning the development of a loop from a invariant will be given --you will have to develop a loop with initialization given the invariant. We will raise your grade on Question 2 of Prelim 3 to your grade on this quiz (but we won’t lower anyone’s grade on Prelim 3) IF: (0) Your grade on the quiz is larger than your grade on Question 2 of Prelim3 and (1) You turn in your Prelim3 with the Quiz. Also, we will hand out course evaluations for you to complete and hand in at the end of the lecture. CS100A, Lecture 25, 1 December 1998

(1) Create initialization that makes P true. Memorize this slide 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 ) and keep P true { S } CS100A, Lecture 25, 1 December 1998

Recursion A method is recursive if it calls itself. Many algorithms are most easily written using recursive methods. To see that recursive method calls (calls of a method from within the body of a method) work, you have only to execute a method call of a recursive method yourself, using the rules that you already know. We’ll show that for a simple case in this lecture. However, to understand that a particular recursive method is correct, you should not think about executing it. Rather, do two things: (0) Understand that each recursive method call is correct in terms of the specification of the method. (2) Make sure that the recursion “terminates” (much like making sure that a loop terminates). That is, see to it that in some sense the arguments of the recursive call are “smaller” than the parameters of the method and that when the parameters are “as small as possible”, the method terminates without calling itself recursively. We’ll see this in the examples. CS100A, Lecture 25, 1 December 1998

Return the reverse of a string // Return the reverse of string s. For example, if s is // “abcd”, return the string “dcba”. public static String rev(String s) { // If the string is empty or contains one character, return it if (s.length( ) <= 1) return s; // The string has the form C c, where C is a character; // c is a string. C is s.charAt(0). c is s.substring(1). // Return the reverse of c catenated with C return rev(s.substring(1)) + charAt(0); } Note that the argument to the recursive call rev(s.substring(1)) has one less character than parameter s. Therefore, the “depth” of recursion --the maximum number of recursive calls with frames that will exist at any time-- is the number of characters in s. CS100A, Lecture 25, 1 December 1998

// Return the reverse of string s. Execution of rev // Return the reverse of string s. public static String rev(String s) { if (s.length( ) <= 1) return s; return rev(/*L2:*/s.substring(1)) + charAt(0); } ------------------ in main method:--------------- String t= /*L1:*/ rev(“abc”); M0: frame for main. t ____ Called from system F0: first frame for rev. s “abc” Called from main.L1, frame M0 F2: second frame for rev. s “bc” Called from rev.L2, frame F1 F3: third frame for rev. s “c” Called from rev.L2, frame F2 CS100A, Lecture 25, 1 December 1998

Method to remove blanks Method to duplicate each character // Return a copy of s, but with blanks removed public static String removeBlanks (String s) { if (s.length() = 0) return s; if (s.charAt(0) = ‘ ‘) return s.substring(1); // first character of s is not a blank. // Return first character followed by rest of s // but with blanks removed return s.charAt(0) + removeBlanks(s.substring(1)); } Method to duplicate each character // Return a copy of s but with each character repeated public static String dup(String s) { if s.length() == 0) return s; return s.charAt(0) + s.charAt(0) + dup(s.substring(1)); CS100A, Lecture 25, 1 December 1998

Recursive method to lower-case a String // Return a copy of s with all capital letters lower-cased public static lowerCase(String s) { if (s.length()= 0) return s; // Store into c the first character, lower-cased // if necessary char c= s.charAt(0); if (‘A’ <=s.charAt(0) && s.charAt(0) <=‘Z’) c= (char) ((int)s.charAt(0) -26); return c + lowerCase(s.substring(1)); } Recursive method for logarithmic exponentiation // Given b>= 0, return ab public static int exp(int a, int b) { if (b == 0) return 1; if (b is odd) return a * exp ( b-1); // b is even, so ab = (a*a)b/2 return exp(a*a, b/2); CS100A, Lecture 25, 1 December 1998

possible locations for Tiling a kitchen with L-shaped squares Our house has a kitchen which is 2n x 2n feet square (for some integer n). A small, 1 x 1 refrigerator sits on one square of the room. How can we tile the floor with L-shaped tiles that look like this (2 x 2 feet with a 1 x 1 section missing)? Kitchen, showing two possible locations for the refrigerator 2n CS100A, Lecture 25, 1 December 1998

// Given n >= 0 and a 2n x 2n feet square room with one // square filled, tile room with L-shaped squares (see // previous slide) public static tile(Floor p, int n) { if (n=0) return; Draw horizontal and vertical lines in middle of room p, as shown to the right. One of the four quadrants has a a 1 x 1 square filled (example shown). Place an L-shaped tile as shown to the right, so that each of the other quad- rants has a square filled. Now call tile(p1,n-1) four times, where each time p1 is one of the four quadrants, to tile the four quadrants. } . 2n-1 CS100A, Lecture 25, 1 December 1998

// Given n>=0, return !n = 1*2*3*…*n public static factorial(int n) { if (n<=1) return 1; return n * factorial(n-1); } // Binary search --Given is b[0..h] <= x < b[k..n-1] and // h < k and b is sorted (in ascending order). // Return an integer j satisfying b[0..j] <= x < b[j+1]. Public static bsearch(int[ ] b, int h, int k) { if (h+1 = k) return h; int e= (h+k)/2; if (b[e] <= x) return bsearch(b, e, k); else return bsearch(b, h, e); CS100A, Lecture 25, 1 December 1998

Fibonacci numbers Fn given by: F0 = 0 F1 = 1 Fn = Fn-1 + f n-2 for n>1 // Given n>= 0, return Fn --inefficient version public static fib(int n) { if (n<=1) return n; return fib(n-1) + fib(n-2); } ------------------------------------------------------------------------ // Given n>= 0, return Fn --efficient version public static fibe(int n) { if (n=0) return n; return fibh(n,0,1,1) // Given: 0<m<=n and Fm-1 = a and Fm=b, return Fn public static fibh(int n, int a, int b, int m) { if (m=n) return b; return fibh(n, b, a+b, m+1); CS100A, Lecture 25, 1 December 1998