CS1110 Lec Oct 2010 More on Recursion

Slides:



Advertisements
Similar presentations
1 CS1110 Lec Oct 2010 More on Recursion We develop recursive functions and look at execution of recursive functions Study Sect 15.1, p Watch.
Advertisements

0 CS100J September 2007 CS100J: 12 weeks programming using Java, 2 using Matlab. David Gries. CS100M: 7 weeks of Matlab and 7 of Java. Daisy Fan. CS100H:
Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 –HW3 is posted Questions?
CS1101: Programming Methodology Aaron Tan.
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 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
1 CS100J 09 March, 2006 More on Loops Reading: Secs 7.1–7.4 A Billion. The next time you hear someone in government rather casually use a number that includes.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Binary01.ppt Decimal Decimal: Base 10 means 10 Unique numerical digits ,00010,000 Weight Positions 3,
1 CS100J 26 Feb 2008 More on Recursion My first job was working in an orange juice factory, but I got canned: couldn't concentrate. Then I worked in the.
while there is room A draws or ; B draws or ; A wants to get a solid closed curve. B wants to stop A from getting a solid closed curve. Who.
Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
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.
1 CS September 2008 Discussion of Methods: Executing method calls.If-statements. The return statement in a function. Local variables. For this and.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
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.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
Week 3 Bell Works Political Science. Political Science Monday’s Bell Work Today’s bell work: Write down at least two pieces of information you learned.
CS 100Lecture 201 Announcements P5 due Thursday Final exam: Tuesday August 10th, 8AM, Olin 155, 2 hours Tomorrow, Wednesday: C, pointers Thursday, Friday:
Recursion (Continued)
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
UMBC CMSC 104 – Section 01, Fall 2016
CS1110 Spring Instructor: David Gries
Correctness issues and Loop invariants
ECE Application Programming
Announcements Homework #5 due Monday 1:30pm
Introduction to programming in java
CS1110 Classes, stepwise refinement 23 Sep 2009
COSC 220 Computer Science II
Variables and Arithmetic Operators in JavaScript
Recursion (Continued)
Week 11 - Friday CS221.
Recursion (Continued)
Trees Lecture 12 CS2110 – Spring 2018.
Formatting Output.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Lecture 13 Input/Output Files.
Time management School of Rock.
CS October 2008 The while loop and assertions
Sit next to someone. Today, we do some work in pairs.
Building Java Programs
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Recursion (Continued)
Sit next to someone. Today, we do some work in pairs.
Locations for CS 115 Activities
CISC124 Labs start this week in JEFF 155. Fall 2018
Searching, Sorting, and Asymptotic Complexity
COMPUTER 2430 Object Oriented Programming and Data Structures I
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
EECE.2160 ECE Application Programming
CS 336/536: Computer Network Security Fall 2014 Nitesh Saxena
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Searching.
Asymptotic complexity
EECE.2160 ECE Application Programming
Searching and Sorting Hint at Asymptotic Complexity
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
EECE.2160 ECE Application Programming
Homework Homework Questions? Continue Reading K&R Chapter 2
EECE.2160 ECE Application Programming
2011年 5月 2011年 6月 2011年 7月 2011年 8月 Sunday Monday Tuesday Wednesday
CSE 206 Course Review.
Searching and Sorting Hint at Asymptotic Complexity
CS 250, Discrete Structures, Fall 2015 Nitesh Saxena
Week 7 - Monday CS 121.
Presentation transcript:

CS1110 Lec. 11 5 Oct 2010 More on Recursion Thursday 7:30pm prelim: A-K Olin 155 L-Z Olin 255 Study Sect 15.1, p. 415. Watch activity 15-2.1 on the CD. In DrJava, write and test as many of the self-review exercises as you can (disregard those that deal with arrays). Wednesday 3:35 lab is less crowded; go there instead? No Labs the week of fall break (next week) No office hours Friday, Monday, Tuesday, 8-12 October More lunch dates scheduled on the CMS. You are invited. A3 times mean 4.2 median 3.5 7..9 hours: 17 10..14 hours: 7 15..15 hours: 1

A game . . . . . . . . . . . A and B alternate moves . . . . . . . . . . . while there is room A draws or ; B draws or ; A wants to get a solid closed curve. B wants to stop A from getting a solid closed curve. Who can win? What strategy to use? A and B alternate moves Board can be any size: m by n dots, with m > 0, n > 0 . . . . . . . . . . A won the game to the right because there is a solid closed curve.

What does private mean? Look it up! Index says p 155. 155-156 says: a component declared with modifier private in class C is accessible only in class C. a1 Person fbf a2 mutual() { … } /** = “the female best-friend relation is mutual” —this person’s best friend thinks this person is their best friend. */ public boolean mutual() { return fbf!= null && this == fbf.fbf; } Can’t reference fbf in here SClass

/** = if y is even then 2*y otherwise y*/ public static boolean d(int y) { if (y%2 == 0) { int k= 2 * y; return k; } else return y; } Consider the call d(5). When is local variable k created (or drawn) during evaluation of the call? A: Never, since the argument is odd B: Just before k= 2*y is executed C: Just the method body is executed D: During step 1 of execution of the call

/. = non-negative n, with commas every 3 digits e. g /** = non-negative n, with commas every 3 digits e.g. commafy(5341267) = “5,341,267” */ public static String commafy(int n) { } What is the base case? A: 0..1 B: 0..9 C: 0..99 D: 0..999 E: 0..9999

Executing recursive function calls. /** = non-negative n, with commas every 3 digits e.g. commafy(5341267) = “5,341,267” */ public static String commafy(int n) { 1: if (n < 1000) 2: return “” + n; // n >= 1000 3: return commafy(n/1000) + “,” + to3(n%1000); } /** = p with at least 3 chars — 0’s prepended if necessary */ public static String to3(int p) { if (p < 10) return “00” + p; if (p < 100) return “0” + p; return “” + p; } commafy(5341266 + 1) n commafy: 1 Demo

b c = (b*b) c/2 Recursive functions Properties: b c = b * b c-1 For c even b c = (b*b) c/2 e.g 3*3*3*3*3*3*3*3 = (3*3)*(3*3)*(3*3)*(3*3) /** = b c. Precondition: c ≥ 0*/ public static int exp(double b, int c)

Recursive functions c number of calls 0 1 1 2 2 2 4 3 8 4 5 32 6 2n n + 1 /** = b c. Precondition: c ≥ 0*/ public static int exp(double b, int c) { if (c == 0) return 1.0; if (c is odd) return b * exp(b, c–1); // c is even and > 0 return exp(b*b, c / 2); } 32768 is 215 so b32768 needs only 16 calls!

Binary arithmetic Decimal Binary Octal Binary 00 00 00 20 = 1 1 00 00 00 20 = 1 1 01 01 01 21 = 2 10 02 10 02 22 = 4 100 03 11 03 23 = 8 1000 04 100 04 24 = 16 10000 05 101 05 25 = 32 100000 06 110 06 26 = 64 1000000 07 111 07 215 = 32768 1000000000000000 08 1000 10 09 1001 11 10 1010 12 Test c odd: Test last bit = 1 Divide c by 2: Delete the last bit Subtract 1 when odd: Change last bit from 1 to 0. Exponentiation algorithm processes binary rep. of the exponent.

Hilbert’s space-filling curve As the size of each line gets smaller and smaller, in the limit, this algorithm fills every point in space. Lines never overlap. Hilbert(2): H(n-1) dwn H(n-1) dwn Hilbert(n): All methods used in today’s lecture will be on course website H(n-1) left H(n-1) right

Hilbert’s space-filling curve