{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

This preview shows pages 1–17. Sign up to view the full content.

Problem Solving with Computers-II CS 24 February 16 & 18, 2010 1

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

View Full Document
Announcements Wednesday’s Lab section Recursive programming No office hours on Feb 17 th Please speak to one of the TAs 2

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

View Full Document
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
5 What Is Recursion? Recursion like a set of Russian dolls.

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

View Full Document
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
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.

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

View Full Document
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
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

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

View Full Document
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
11 Computing Factorial Recursive definition A definition in which something is defined in terms of a smaller version of itself What is 3 factorial?

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

View Full Document
12
13

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

View Full Document
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;
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).

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

View Full Document
16 Recursive Factorial int Factorial ( int number ) // Pre: number >= 0.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 49

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

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

View Full Document
Ask a homework question - tutors are online