8/5/19 Welcome to CSCI 134! CSCI 134 Intro to Computer Science How might we draw a Scribble? Objects, Events, Graphics (A collection of lines drawn when the user Iris Howley onMouseDrag s) iris@cs.williams.edu Assistant Professor of Computer Science Take a moment to write down your response, individually. 17 1 2 Discuss with a partner! How might we draw a Scribble? Is there a more concise way to (A collection of lines drawn program this [repetitive!] algorithm? when the user onMouseDrag s) 3 4 • Know when to stop. • Decide how to take one step. DEMO • Break the journey down into that step plus a smaller journey. RECURSION ColorScribbleController 5 6 4 1
8/5/19 CONGRATS! DEMO SpiralsScribblesRecursive 7 8 Administrative Details • Lab: Monday and Tuesday this week, as usual • Midterm: 6-7:45p OR 8-9:45pm, 10/24 TPL 203 • Sample midterm up on course Lectures page • How to study? – Sample midterm – Class demos – Understand comments on your labs – Practice hand-writing codes: • Textbook problem sets (answers on course website/Resources) • We will give you Java Swing & ObjectDraw reference sheets 9 10 Dragon, I need to know if any of the numbers in this Sorry, I can only tell you if list are odd: the first number of the list is odd. (3142, 5798, 6550, 8914) 11 12 5 2
8/5/19 But I need to know if any I’ll only look at the first number in the list is odd, number, but I’ll look at as not just the first! many lists as you like. 13 14 ? (3142, 5798, 6550, 8914) The first number is not odd. What should Sam do? 15 16 (3142, 5798, 6550, 8914) (3142, 5798, 6550, 8914) The first number is not odd. The first number is not odd. 17 18 6 3
8/5/19 (3142, 5798, 6550, 8914) (3142, 5798, 6550, 8914) The first number is not odd. That’s an empty list! It can’t be odd. 19 20 None of the numbers the How can you know that? I Sorcerer gave me were odd, only told you about the first thank you! number! 21 22 The lists I gave you were: The lists I gave you were: (3142, 5798, 6550, 8914) (3142, 5798, 6550, 8914) (5798, 6550, 8914) (5798, 6550, 8914) Tricky. Looks like (6550, 8914) (6550, 8914) (8914) (8914) you’ve discovered () () recursion. Why did this work? 23 24 7 4
8/5/19 Steps for Recursion The lists I gave you were: Recursive Function (3142, 5798, 6550, 8914) (5798, 6550, 8914) 1.Know when to stop. (6550, 8914) (8914) 2.Decide how to take one step. () 3.Break the journey down into Base Case that step plus a smaller journey. Function Call, List Mover 25 26 Steps for Recursion Pseudocode • When to stop? Sam: The list is {3142, 5798, 6550, 8914} Dragon: Is list empty? – When list is empty Dragon: No? Is first number odd? • What is the one step? Dragon: Yes? Print. – Check the first list item Sam: Drop first num from list! • How to break the journey down? Dragon: Is list empty? – Progress through each of first list items Dragon: No? Is first number odd? Dragon: Yes? Print. 27 28 Pseudocode array = {3142, 5798, 6550, 8914} Let’s implement this printFirstOdd(array) recursive algorithm in function printFirstOdd(a[]) { Java is a[] not empty? { is a[0] odd? à Print a[0] } printFirstOdd(a[1,a.length]) 29 30 } 8 5
8/5/19 Discuss with a partner How might we draw a Scribble recursively ? When should we choose (A collection of lines drawn when loops (iteration) over the user onMouseDrag s) recursion? 31 32 Learning Goals By the end of this class, students should be able to: 1. Describe the 3-step process for recursion 2. Explain why recursion is a useful approach for some problems We’ll be discussing recursion again! 33 9 6
Recommend
More recommend