Download presentation

Presentation is loading. Please wait.

1
CSSE221: Software Dev. Honors Day 16 Announcements Announcements Markov due Wednesday 11:59 pm Markov due Wednesday 11:59 pm Homework 6 due next Tuesday, since I’m so late in posting it. Homework 6 due next Tuesday, since I’m so late in posting it. Exam 1 passed back at end of class Exam 1 passed back at end of class

2
This week: Markov Monday: Monday: Eclipse’s Debugger Eclipse’s Debugger Recursion Recursion Recursion exercise Recursion exercise Tuesday: Tuesday: Wrap up recursion Wrap up recursion Capsule round 3: choose groups, discuss expectations Capsule round 3: choose groups, discuss expectations Intro capstone project, choose teams. Intro capstone project, choose teams. Some time for Markov Some time for Markov Thursday: Thursday: Fall Break! Fall Break!

3
Debugging Just scratching the surface… Just scratching the surface… “Debug as” “Debug as” Press the bug icon to debug the same program again (like with running) Press the bug icon to debug the same program again (like with running) Debug perspective Debug perspective Breakpoints Breakpoints Stepping Stepping Demo together: use SimpleFileIO Demo together: use SimpleFileIO

4
Recursion What is a recursive method? What is a recursive method? A method that calls itself, but on a simpler problem A method that calls itself, but on a simpler problem Used for any situation where parts of a whole look like mini versions of the whole: Used for any situation where parts of a whole look like mini versions of the whole: Folders within folders on computers Folders within folders on computers Some computer languages (Scheme) Some computer languages (Scheme) Trees in general Trees in general Cons: Takes more space (but time can be roughly equal) Cons: Takes more space (but time can be roughly equal) Pros: Can gives code that’s very easy to understand Pros: Can gives code that’s very easy to understand

5
Recursion template For a method that calculates a value: For a method that calculates a value: int foo(int n) { if (n <=1) { //Base case return (some easy expression); } else { return (some expr. with foo(n-1); //not just foo(n)) so progress } Of course, you can write void recursive methods, and ones that recurse on values other than n-1 Of course, you can write void recursive methods, and ones that recurse on values other than n-1

6
Weiss’ Four Rules of Recursion 1. Base case You need at least 1 base case that can be solved without recursing You need at least 1 base case that can be solved without recursing 2. Progress You can only recurse on a simpler problem You can only recurse on a simpler problem 3. “You gotta believe” Otherwise, you’ll try to solve the problem both recursively and non-recursively. This is bad. Otherwise, you’ll try to solve the problem both recursively and non-recursively. This is bad. 4. Compound interest rule Efficiency: Don’t duplicate work by solving the same instance of the problem in separate recursive calls Efficiency: Don’t duplicate work by solving the same instance of the problem in separate recursive calls Later Later

7
Demo Factorial!

8
Let’s watch in the debugger

9
What else can we do recursively? gcd(a,b): //assumes a > b gcd(a,b): //assumes a > b if a is a multiple of b, return b if a is a multiple of b, return b Otherwise, return gcd(b, a % b) (guaranteed to be smaller) Otherwise, return gcd(b, a % b) (guaranteed to be smaller) myPow(x, a) myPow(x, a) Program this now Program this now Contest: Which table can write a version with the shallowest call stack? Contest: Which table can write a version with the shallowest call stack? Write x a as … Write x a as … (x a/2 ) (x a/2 ) if a is even, (x a/2 ) (x a/2 ) if a is even, x (x (a-1)/2 ) (x (a-1)/2 ) otherwise x (x (a-1)/2 ) (x (a-1)/2 ) otherwise Particularly helpful in cryptography when you are doing modular arithmetic Particularly helpful in cryptography when you are doing modular arithmetic

10
If you don't have a base case for your recursion, it can become a nightmare! Break

11
Break

12
Next Programming Project Starts day after break Starts day after break Teams of 3 Teams of 3 2+ Weeks to do 2+ Weeks to do An educational simulation or animation of some process An educational simulation or animation of some process Constraints: Constraints: Must include non-trivial use of 2 non-array data structures Must include non-trivial use of 2 non-array data structures Must include a GUI Must include a GUI

13
Why tell you this now? Tomorrow you are going to pick round 3 capsule groups (which start the Tuesday after break) Tomorrow you are going to pick round 3 capsule groups (which start the Tuesday after break) One interesting idea would be to demo your simulation to help teach your capsule topic One interesting idea would be to demo your simulation to help teach your capsule topic You’d have the same team for both capsule and project You’d have the same team for both capsule and project Some last year recommended working with fewer groups at a time Some last year recommended working with fewer groups at a time I’ll give you a spot on the Angel survey to indicate if you would like to do that: I’ll give you a spot on the Angel survey to indicate if you would like to do that: Which topic Which topic Which teammates Which teammates Talk it up with others today/tonight! Talk it up with others today/tonight!

14
A graphical exercise on recursion Sierpinski’s Gasket… Sierpinski’s Gasket… http://www.pha.jhu.edu/~ldb/seminar/fractals.ht ml http://www.pha.jhu.edu/~ldb/seminar/fractals.ht ml http://www.pha.jhu.edu/~ldb/seminar/fractals.ht ml http://www.pha.jhu.edu/~ldb/seminar/fractals.ht ml See starting code in the repository. See starting code in the repository. How can you use recursion to solve this problem? How can you use recursion to solve this problem? Discuss with a partner Discuss with a partner You may pair-program this if you want You may pair-program this if you want Fun extensions: Fun extensions: Add color Add color Play with non-equilateral triangles Play with non-equilateral triangles Write the chaos version (search on web for it) Write the chaos version (search on web for it)

15
The gem cannot be polished without friction, nor man perfected without trials. -- Chinese Proverb

16
Exam 1 Statistics 90+8 90+8 80-906 80-906 70-8012 70-8012 60-703 60-703 0-603 0-603 Average: 79.5% Average: 79.5% Compare to midterm averages: 90+12 80-9010 (4 B+) 70-809 (4 C+) 60-700 0-601 Midterm grades submitted

Similar presentations

© 2024 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google