Download presentation
Presentation is loading. Please wait.
Published byBathsheba Gregory Modified over 9 years ago
2
Humorous Asides
3
“A journey begins with single step”
6
Large problems hard to solve Thinking about & solving small problems easier Splitting problems into smaller ones often helps Before you start coding, plan each assignment Break up large functions with many if s and loops Move repeated action into small functions Solving Problems
7
Smaller is Better
8
CENSORED
9
Smaller is Better (At least for programming) CENSORED
10
Should be boring, easy, understandable drone Given its parameters, perform the expected action Only perform action defined for its parameters Should not cure cancer Do not worry about the larger problem Solving entire problem is not this function’s issue Split into tasks since solving whole problem hard Goal of a Function
11
re-cur-sion re-cur-sion: Method of solving problem by combining solutions to identical, smaller problems Recursion
12
re-cur-sion re-cur-sion: Method of solving problem by combining solutions to identical, smaller problems Recursion
13
Recursive stepbase case(s) Recursive step simplifies problem to base case(s) Recast using slightly easier version in recursive step 4! = 4 * 3! See Recursion Work
14
Recursive stepbase case(s) Recursive step simplifies problem to base case(s) Recast using slightly easier version in recursive step 4! = 4 * 3! = 4 * (3 * 2!) See Recursion Work
15
Recursive stepbase case(s) Recursive step simplifies problem to base case(s) Recast using slightly easier version in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!)) See Recursion Work
16
Recursive stepbase case(s) Recursive step simplifies problem to base case(s) Recast using slightly easier version in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!)) Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1)) See Recursion Work
17
Recursive stepbase case(s) Recursive step simplifies problem to base case(s) Recast using slightly easier version in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!)) Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1)) After base case, combine solutions in recursive steps = 4 * (3 * 2) See Recursion Work
18
Recursive stepbase case(s) Recursive step simplifies problem to base case(s) Recast using slightly easier version in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!)) Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1)) After base case, combine solutions in recursive steps = 4 * (3 * 2) = 4 * 6 See Recursion Work
19
Recursive stepbase case(s) Recursive step simplifies problem to base case(s) Recast using slightly easier version in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!)) Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1)) After base case, combine solutions in recursive steps = 4 * (3 * 2) = 4 * 6 = 24 See Recursion Work
20
Very easy to create solution that does not work Infinite recursion occurs if base case never reached Frame-by-frame stack grows from function calls Program crashes without warning or explanation For Recursion To Work
21
Very easy to create solution that does not work Recursive step must advance toward a base case If there are multiple, which base case is unimportant one Get one step closer to base case at each recursive call Must check if algorithm works for all possible inputs For Recursion To Work
22
recursive A function is recursive if it calls itself: int factorial(int i) { if (i <= 1) { return 1; } else { return i * factorial(i - 1); } } Recursion in C++
23
recursive A function is recursive if it calls itself: int factorial(int i) { if (i <= 1) { return 1; } else { int nextI = i – 1; int result = factorial(nextI); return i * result; } } Recursion in C++ Base case: Solution is simple
24
recursive A function is recursive if it calls itself: int factorial(int i) { if (i <= 1) { return 1; } else { int nextI = i – 1; int result = factorial(nextI); return i * result; } } Recursion in C++ Recursive Step: 1 step Take 1 step to solution
25
recursive A function is recursive if it calls itself: int factorial(int i) { if (i <= 1) { return 1; } else { int nextI = i – 1; int result = factorial(nextI); return i * result; } } Recursion in C++ Recursive Step: 1 step Take 1 step to solution Make 1 or more recursive calls
26
recursive A function is recursive if it calls itself: int factorial(int i) { if (i <= 1) { return 1; } else { int nextI = i – 1; int result = factorial(nextI); return i * result; } } Recursion in C++ Recursive Step: 1 step Take 1 step to solution Make 1 or more recursive calls Simple process computes result
27
Start with check for base case(s) These cases must return blatantly obvious answer 1+ recursive calls found within recursive step(s) Write these assuming recursive call works 1 Take 1 step toward base case (not 2, 3, or 10482) Recursive Function Basics
28
Start with check for base case(s) These cases must return blatantly obvious answer 1+ recursive calls found within recursive step(s) Write these assuming recursive call works 1 Take 1 step toward base case (not 2, 3, or 10482) Recursive Function Basics
29
No different than usual tracing we were doing When function called, we add frame for the call Local variables & parameters shown in frame (Processors also include line being executed) Tracing Recursion
30
No different than usual tracing we were doing When function called, we add frame for the call Local variables & parameters shown in frame (Processors also include line being executed) NOT Tracing Recursion
31
int findMin(int[] a, int len, int j) { if (j == len - 1) { return a[j]; } else { int minFollowing = findMin(a, len, j+1); if (a[j] < minFollowing) { return a[j]; } return minFollowing; } } int[] example1 = { 0 }; findMin(example1, 1,0); Trace This, Buddy!
32
int findMin(int[] a, int len, int j) { if (j == len - 1) { return a[j]; } else { int minFollowing = findMin(a,len,j+1); if (a[j] < minFollowing) { return a[j]; } return minFollowing; } } int[] example2 = { 2, 3, 0, 1 }; findMin(example2, 4, 0); Trace This, Buddy!
33
Your Turn Get into your groups and complete activity
34
For Next Lecture Programming Project #3 Programming Project #3 due Friday
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.