DOC PREVIEW
UVa-Wise COSC 181 - Study Notes

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 181COSC 181 – Foundations of Computer ProgrammingClass 2926.19 RecursionRecursive function•A function that calls itself, either directly, or indirectly (through another function)Recursion•Base case(s)•The simplest case(s), which the function knows how to handle•For all other cases, the function typically divides the problem into two conceptual pieces•A piece that the function knows how to do •A piece that it does not know how to do•Slightly simpler or smaller version of the original problem36.19 Recursion (Cont.)Recursion (Cont.)•Recursive call (also called the recursion step) •The function launches (calls) a fresh copy of itself to work on the smaller problem•Can result in many more recursive calls, as the function keeps dividing each new problem into two conceptual pieces•This sequence of smaller and smaller problems must eventually converge on the base case•Otherwise the recursion will continue forever46.19 Recursion (Cont.)Factorial•The factorial of a nonnegative integer n, written n! (and pronounced “n factorial”), is the product•n · (n – 1) · (n – 2) · … · 1•Recursive definition of the factorial function•n! = n · (n – 1)! •Example•5! = 5 · 4 · 3 · 2 · 15! = 5 · ( 4 · 3 · 2 · 1)5! = 5 · ( 4! )5Fig. 6.28 | Recursive evaluation of 5!.61 // Fig. 6.29: f ig06_29.cpp2 / / Test ing the recurs ive factor ia l funct ion .3 #inc lude <iostream>4 us ing std::cout;5 us ing std::endl;67 #inc lude <iomanip>8 us ing std::setw;910 uns igned long factorial( unsigned long ); // function prototype1112 int main()13 {14 // calculate the factorials of 0 through 1015 for ( int counter = 0; counter <= 10; counter++ )16 cout << setw( 2 ) << counter << "! = " << factorial( counter ) 17 << endl;1819 return 0; // indicates successful termination20 } // end mainOutlinefig06_29.cpp (1 of 2)First call to factorial function72122 / / recurs ive def in it io n of funct ion factor ia l 23 uns igned long factorial( unsigned long number ) 24 { 25 if ( number <= 1 ) // test for base case 26 return 1; // base cases: 0! = 1 and 1! = 127 else // recursion step 28 return number * factorial( number - 1 ); 29 } // end function factoria l 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 36288010! = 3628800Outlinefig06_29.cpp (2 of 2)Base cases simply return 1Recursive call to factorial function with a slightly smaller problem8Common Programming Error 6.24 Either omitting the base case, or writing the recursion step incorrectly so that it does not converge on the base case, causes “infinite” recursion, eventually exhausting memory. This is analogous to the problem of an infinite loop in an iterative (nonrecursive) solution.96.20 Example Using Recursion: Fibonacci Series The Fibonacci series•0, 1, 1, 2, 3, 5, 8, 13, 21, …•Begins with 0 and 1•Each subsequent Fibonacci number is the sum of the previous two Fibonacci numbers•can be defined recursively as follows:•fibonacci(0) = 0•fibonacci(1) = 1•fibonacci(n) = fibonacci(n – 1) + fibonacci(n – 2)101 // Fig. 6.30: f ig06_30.cpp2 / / Test ing the recurs ive f ibonacc i funct ion .3 #inc lude <iostream>4 us ing std::cout;5 us ing std::cin;6 us ing std::endl;78 unsigned long fibonacci( unsigned long ); // function prototype910 int main()11 {12 // calculate the fibonacci values of 0 through 1013 for ( int counter = 0; counter <= 10; counter++ )14 cout << "fibonacci( " << counter << " ) = "15 << fibonacci( counter ) << endl;1617 // display higher fibonacci values18 cout << "fibonacci( 20 ) = " << fibonacci( 20 ) << endl;19 cout << "fibonacci( 30 ) = " << fibonacci( 30 ) << endl;20 cout << "fibonacci( 35 ) = " << fibonacci( 35 ) << endl;21 return 0; // indicates successful termination22 } // end main23Outlinefig06_30.cpp (1 of 2)1124 / / recurs ive method f ibonacc i 25 unsigned long fibonacci( unsigned long number ) 26 { 27 if ( ( number == 0 ) || ( number == 1 ) ) // base cases 28 return number; 29 else // recursion step 30 return fibonacci( number - 1 ) + fibonacci( number - 2 );31 } // end function f ibonacci fibonacci( 0 ) = 0fibonacci( 1 ) = 1fibonacci( 2 ) = 1fibonacci( 3 ) = 2fibonacci( 4 ) = 3fibonacci( 5 ) = 5fibonacci( 6 ) = 8fibonacci( 7 ) = 13fibonacci( 8 ) = 21fibonacci( 9 ) = 34fibonacci( 10 ) = 55fibonacci( 20 ) = 6765fibonacci( 30 ) = 832040fibonacci( 35 ) = 9227465Outlinefig06_30.cpp (2 of 2)Recursive calls to fibonacci functionBase cases12Fig. 6.31 | Set of recursive calls to function fibonacci.13Common Programming Error 6.25 Writing programs that depend on the order of evaluation of the operands of operators other than &&, ||, ?: and the comma (,) operator can lead to logic errors.14Portability Tip 6.3 Programs that depend on the order of evaluation of the operands of operators other than &&, ||, ?: and the comma (,) operator can function differently on systems with different compilers.156.20 Example Using Recursion: Fibonacci Series (Cont.)Caution about recursive programs•Each level of recursion in function fibona cci has a doubling effect on the number of function calls•i.e., the number of recursive calls that are required to calculate the nth Fibonacci number is on the order of 2n•20th Fibonacci number would require on the order of 220 or about a million calls•30th Fibonacci number would require on the order of 230 or about a billion calls.•Exponential complexity•Can humble even the world’s most powerful computers16Performance Tip 6.8 Avoid Fibonacci-style recursive programs that result in an exponential “explosion” of calls.17In-class ExerciseWrite a program that asks the user for a number n. The program then calls a recursive function that finds the sum of the first n integers. It then prints out that sum.2. What is the recursive relationship?3. What is the base case?18The two parts 1. Recursive Relationship•n + IntSum(n-1)2. Base Case•n=1 (the sum of the 1st integer is 1)There is an even better solution •Any ideas?•(n(n+1)) /


View Full Document

UVa-Wise COSC 181 - Study Notes

Download Study Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?