# CHAP17 - Chapter 17 Recursion Dale/Weems/Headington 1...

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

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

View Full Document
2 Chapter 17 Topics Meaning of Recursion Base Case and General Case in Recursive Function Definitions Writing Recursive Functions with Simple Type Parameters Writing Recursive Functions with Array Parameters Writing Recursive Functions with Pointer Parameters Understanding How Recursion Works
3 Recursive Function Call a recursive call is a function call in which the called function is the same as the one making the call in other words, recursion occurs when a function calls itself! but we need to avoid making an infinite sequence of function calls (infinite recursion)

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

View Full Document
4 Finding a Recursive Solution a recursive solution to a problem must be written carefully the idea is for each successive recursive call to bring you one step closer to a situation in which the problem can easily be solved this easily solved situation is called the base case each recursive algorithm must have at least one base case, as well as a general (recursive) case
5 General format for Many Recursive Functions if (some easily-solved condition) // base case solution statement else // general case recursive function call SOME EXAMPLES . . .

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

View Full Document
6 Writing a Recursive Function to Find the Sum of the Numbers from 1 to n DISCUSSION The function call Summation(4) should have value 10, because that is 1 + 2 + 3 + 4 . For an easily-solved situation, the sum of the numbers from 1 to 1 is certainly just 1. So our base case could be along the lines of if ( n == 1 ) return 1;
7 Writing a Recursive Function to Find the Sum of the Numbers from 1 to n Now for the general case. . . The sum of the numbers from 1 to n, that is, 1 + 2 + . . . + n can be written as n + the sum of the numbers from 1 to (n - 1), that is, n + 1 + 2 + . . . + (n - 1) or, n + Summation(n - 1) And notice that the recursive call Summation(n - 1) gets us “closer” to the base case of Summation(1)

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

View Full Document
8 Finding the Sum of the Numbers from 1 to n int Summation ( /* in */ int n ) // Computes the sum of the numbers from 1 to n by // adding n to the sum of the numbers from 1 to (n-1) // Precondition: n is assigned && n > 0 // Postcondition: // Function value == sum of numbers from 1 to n { if ( n == 1) // base case return 1 ; else // general case return ( n + Summation ( n - 1 ) ) ; }
9 Returns 4 + Summation(3) = 4 + 6 = 10 Summation(4) Returns 3 + Summation(2) = 3 + 3 = 6 Summation(3) Returns 2 + Summation(1) = 2 + 1 = 3 Summation(2) n==1 Returns 1 Summation(1) Summation(4) Trace of Call n 4 n 3 n 2 n 1

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

View Full Document
10 Writing a Recursive Function to Find n Factorial DISCUSSION 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;
11 Writing a Recursive Function to Find Factorial(n) Now for the general case . . .

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 06/13/2011 for the course CSC 240 taught by Professor Lebre during the Spring '04 term at Moraine Valley Community College.

### Page1 / 56

CHAP17 - Chapter 17 Recursion Dale/Weems/Headington 1...

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

View Full Document
Ask a homework question - tutors are online