Download presentation
Presentation is loading. Please wait.
Published byKarin Alexander Modified over 9 years ago
1
CS 108 Computing Fundamentals March 31, 2015
2
Grades not as good as I hoped I drop the lowest of the first 3 exams Exam #3 and #4 will cover much of the same ground We will use today's review of Exam #2 as a means to clear-up some lack of understanding as well as exam strategy Review Exam #2
3
Pronunciation: ree-kur-zhun Function: noun Etymology: Late Latin recursion, recursio, from recurrere 1 : RETURN 2 : the determination of a succession of elements (as numbers or functions) by operation on one or more preceding elements according to a rule or formula involving a finite number of steps 3 : a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself in a step having a termination condition so that successive repetitions are processed up to the critical step until the condition is met at which time the rest of each repetition is processed from the last one called to the first. Compare with ITERATION Recursion
4
A recursive function is a function that calls itself. Anything that can be solved iteratively can be also be solved recursively and vice versa. Recursion is sometimes useful because the recursive solution to a problem can sometimes be expressed more simply and succinctly than an iterative solution. Let’s look at a classic example Recursion
5
Main Entry: factorial Function: noun 1 : the product of all the positive integers from 1 to n ; symbol n! 2 : the quantity 0! arbitrarily defined as equal to 1 http://en.wikipedia.org/wiki/Factorial Example: Factorial
6
factorial(0) = 1 (by definition… see previous slide) factorial(1) = 1 * 1 factorial(2) = 2 * 1 *1 factorial(3) = 3 * 2 * 1 *1 factorial(4) = 4 * 3 * 2 *1 *1 factorial(5) = 5 * 4 * 3 * 2 *1 *1 factorial(6) = 6 * 5 * 4 * 3 * 2 *1 *1 factorial(7) = 7 * 6 * 5 * 4 * 3 * 2 *1 *1 factorial(8) = 8 *7 * 6 * 5 * 4 * 3 * 2 *1 *1
7
First: Factorial Via Iteration #include // 1.c int factorial ( int ) ; int main (void) { int input = 0, answer = 0; printf("\n\n The program computes the factorial of an entered integer. ") ; printf("\n Enter an postive integer: ") ; scanf("%d", &input ) ; answer = factorial ( input ) ; printf("\n\n %d factorial equals: %d \n\n", input, answer ) ; return (0) ; } // continued on next slide
8
First: Factorial Via Iteration // continued from previous slide int factorial ( int passed_input ) { int index = 1, result = 1; for ( index = 1 ; index <= passed_input ; index++ ) { result = result * index ; } return (result) ; }
9
Factorial Via Recursion factorial(0) = 1 (by definition) factorial(1) = 1 * 1 is the same as 1 * factorial(0) factorial(2) = 2 * 1 * 1 is the same as 2 * factorial(1) factorial(3) = 3 * 2 * 1 * 1 is the same as 3 * factorial(2) factorial(4) = 4 * 3 * 2 * 1 * 1 is the same as 4 * factorial(3) factorial(5) = 5 * 4 * 3 * 2 * 1 * 1 is the same as 5 * factorial(4) factorial(6) = 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 6 * factorial(5) factorial(7) = 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 7 * factorial(6) factorial(8) = 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 8 * factorial(7) Is there a general way to state our observation of factorials (above) into rules for an algorithm using factorial(n) as a starting point?
10
Factorial Via Recursion factorial(0) = 1 (by definition) factorial(1) = 1 * 1 is the same as 1 * factorial(0) factorial(2) = 2 * 1 * 1 is the same as 2 * factorial(1) factorial(3) = 3 * 2 * 1 * 1 is the same as 3 * factorial(2) factorial(4) = 4 * 3 * 2 * 1 * 1 is the same as 4 * factorial(3) factorial(5) = 5 * 4 * 3 * 2 * 1 * 1 is the same as 5 * factorial(4) factorial(6) = 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 6 * factorial(5) factorial(7) = 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 7 * factorial(6) factorial(8) = 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 8 * factorial(7) If n = = 0 then answer = 1 … factorial(0) = 1 For all other cases, what is the relationship between factorial(n) on the left side of the assignment operator and the formula on the right side? The next slide might be more helpful.
11
Factorial Via Recursion factorial(0) = 1 (by definition) factorial(1) = 1 * factorial(0) factorial(2) = 2 * factorial(1) factorial(3) = 3 * factorial(2) factorial(4) = 4 * factorial(3) factorial(5) = 5 * factorial(4) factorial(6) = 6 * factorial(5) factorial(7) = 7 * factorial(6) factorial(8) = 8 * factorial(7) For all cases other than n = = 0, what is the relationship between factorial(n) on the left side of the assignment operator and the formula on the right side? factorial ( n ) = ??
12
Factorial Via Recursion First: answer = 1 if n = = 0 … factorial(0) = 1 Second: otherwise: factorial(n) = n * factorial( n - 1 ) Notice that the identifier "factorial" appears on both sides of the assignment operator How would we write a factorial function to implement these two rules into a C algorithm ?
13
Factorial Via Recursion First: 1 if n = 0 Second: factorial( n ) = n * factorial( n-1 )if n > 0 int factorial ( int n ) { if n = = 0 return 1 ; else return ( n * factorial ( n – 1 ) ); }
14
Understanding Recursion You can think of a recursive function call as if it were calling a completely separate function. Understand this about recursively called functions: the operations that can be performed by recursively called functions are the same, but the data that is input to each recursively called function is different The next slide might help to visualize the sameness of operations and the differences in data through the use of different (but very, very similar) functions... we’ll pass factorial_a the value 3.
15
Understanding Recursion int factorial_b ( int n ) { if ( n = = 0 ) return 1; else return n * factorial_c ( n - 1) ; } int factorial_c ( int q ) { if( q = = 0 ) return 1; else return q * factorial_d ( q - 1) ; } int factorial_a ( int m ) { if ( m = = 0 ) return 1; else return m * factorial_b ( m - 1 ) ; } int factorial_d ( int r ) { if( r = = 0 ) return 1; else return r * factorial_e ( r - 1) ; }
16
Let's develop a symbol table for the previous slide which is incorporated in this program (this program can handle 0 – 3 as inputs, which is enough to demonstrate the concepts) : http://web.cs.sunyit.edu/~urbanc/cs_108_mar_31a.txt Let's look at the addresses and values of the variables/parameters http://web.cs.sunyit.edu/~urbanc/cs_108_mar_31b.txt Notice that each version of the factorial functions does the same thing Therefore, we can condense this into something more general
17
Recursion Example: Factorial #include // 2.c int factorial ( int ) ; int main (void) { int input = 0, answer = 0; printf( " \n\n The program computes the factorial of an entered integer. " ) ; printf( " \n Enter an postive integer: " ) ; scanf( " %d ", &input ) ; answer = factorial ( input ) ; printf( " \n\n %d factorial equals: %d \n\n ", input, answer ) ; return 0 ; } int factorial ( int passed_input ) { if ( passed_input = = 0) // if cutting, remove the extra space between = and = return 1 ; else return ( passed_input * factorial ( passed_input - 1 ) ) ; }
18
Let's develop a symbol table for the previous slide which is incorporated in this program: http://web.cs.sunyit.edu/~urbanc/cs_108_mar_31c.txt
19
Questions to Guide The Use of Recursion Can you define the problem in terms of smaller problem(s) of the same type? Think about the factorial problem: factorial(n) = n * factorial ( n-1 ) if n > 0 Does each recursive call reduce the size of the problem? As the problem "shrinks", will you eventually reach a point where an obvious solution presents itself? Again, think about the factorial problem: factorial (0) = 1
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.