2-16 - Problem Solving with Computers-II CS 24 February 16...

Info iconThis preview shows pages 1–18. Sign up to view the full content.

View Full Document Right Arrow Icon
Problem Solving with Computers-II CS 24 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Announcements Wednesday’s Lab section Recursive programming No office hours on Feb 17 th Please speak to one of the TAs 2
Background image of page 2
Recap Stack ADT List ADT 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture Plan Recursion General structure of recursive solutions Why do recursive solutions terminate? How do recursive programs manage the stack? Tail recursion When to use recursion? 4
Background image of page 4
5 What Is Recursion? Recursion like a set of Russian dolls.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 What Is Recursion? Recursive call A method call in which the method being called is the same as the one making the call Direct recursion Recursion in which a method directly calls itself example Indirect recursion Recursion in which a chain of two or more method calls returns to the method that originated the chain example
Background image of page 6
7 Recursion You must be careful when using recursion. Recursive solutions can be less efficient than iterative solutions. Still, many problems lend themselves to simple, elegant, recursive solutions.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Some Definitions Base case The case for which the solution can be stated non-recursively General (recursive) case The case for which the solution is expressed in terms of a smaller version of itself Recursive algorithm A solution that is expressed in terms of (a) smaller instances of itself and (b) a base case
Background image of page 8
9 Finding a Recursive Solution Each successive recursive call should bring you closer to a situation in which the answer is known. A case for which the answer is known (and can be expressed without recursion) is called a base case. Each recursive algorithm must have at least one base case, as well as the general (recursive) case
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 General format for many recursive functions if (some condition for which answer is known) // base case solution statement else // general case recursive function call
Background image of page 10
11 Computing Factorial Recursive definition A definition in which something is defined in terms of a smaller version of itself What is 3 factorial?
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12
Background image of page 12
13
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
14 Factorial Program The function call Factorial(4) should have value 24, because that is 4 * 3 * 2 * 1 . For a situation in which the answer is known, the value of 0! is 1. So our base case could be along the lines of if ( number == 0 ) return 1;
Background image of page 14
15 Factorial Program Now for the general case . . . The value of Factorial(n) can be written as n * the product of the numbers from (n - 1) to 1, that is, n * (n - 1) * . . . * 1 or, n * Factorial(n - 1) And notice that the recursive call Factorial(n - 1) gets us “closer” to the base case of Factorial(0).
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
16 Recursive Factorial int Factorial ( int number ) // Pre: number >= 0. { if ( number == 0) // base case return 1 ; else // general case return number * Factorial ( number - 1 ) ; } Why is this correct?
Background image of page 16
17 Three-Questions for Verifying Recursive Functions Base-Case Question: Is there a non-recursive way out of the function?
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 18
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/26/2010 for the course CS cs24 taught by Professor Singh during the Winter '10 term at UCSB.

Page1 / 49

2-16 - Problem Solving with Computers-II CS 24 February 16...

This preview shows document pages 1 - 18. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online