{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

COP3502_10_Recursion2

# COP3502_10_Recursion2 - More Recursion Computer Science...

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

Computer Science Department University of Central Florida More Recursion COP 3502 – Computer Science I

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

View Full Document
More Recursion page 2 Announcements Quiz 1 due tonight by 11:55 PM (9/15/10) It is available starting at 11:00 AM Program 2 due tonight by 11:55 PM Program 3 is now assigned Uses Recursion MUST use recursion or a SIZEABLE deduction will come off the grade Good News: Assignments WILL slow down (not as often) 1 st three assigned and end up being done in first 6 weeks Remaining three are spread out over last 9 weeks
More Recursion page 3 Recursion What is Recursion? (reminder from last time) From the programming perspective: Recursion solves large problems by reducing them to smaller problems of the same form Recursion is a function that invokes itself Basically splits a problem into one or more SIMPLER versions of itself And we must have a way of stopping the recursion So the function must have some sort of calls or conditional statements that can actually terminate the function

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

View Full Document
More Recursion page 4 Recursion - Factorial Example: Compute Factorial of a Number What is a factorial? 4! = 4 * 3 * 2 * 1 = 24 In general, we can say: n! = n * (n-1) * (n-2) * … * 2 * 1 Also, 0! = 1 (just accept it!)
More Recursion page 5 Recursion - Factorial Example: Compute Factorial of a Number Recursive Solution Mathematically, factorial is already defined recursively Note that each factorial is related to a factorial of the next smaller integer 4! = 4*3*2*1 Right? Another example: 10! = 10*9*8*7*6*5*4*3*2*1 10! = 10* = 4 * (4-1)! = 4 * (3!) (9!) This is clear right? Since 9! clearly is equal to 9*8*7*6*5*4*3*2*1

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

View Full Document
More Recursion page 6 Recursion - Factorial Example: Compute Factorial of a Number Recursive Solution Mathematically, factorial is already defined recursively Note that each factorial is related to a factorial of the next smaller integer Now we can say, in general, that: n! = n * (n-1)! But we need something else We need a stopping case, or this will just go on and on and on NOT good! We let 0! = 1 So in “math terms”, we say n! = 1 if n = 0 n! = n * (n-1)! if n > 0
More Recursion page 7 Recursion - Factorial How do we do this recursively? We need a function that we will call And this function will then call itself (recursively) until the stopping case (n = 0) This program prints the result of 10*9*8*7*6*5*4*3*2*1: 3628800 #include <stdio.h> void Fact(int n); int main(void) { int factorial = Fact(10); printf(“%d\n”, factorial); return 0; } Here’s the Fact Function int Fact (int n) { if (n = 0) return 1; else return (n * fact(n-1)); }

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

View Full Document
More Recursion page 8 Recursion - Factorial Here’s what’s going on…in pictures #include <stdio.h> void Fact(int n); int main(void) { int factorial = Fact(10); printf(“%d\n”, factorial); return 0; } Fact(10) Returns (10*Fact(9)) Fact(9) Returns (9*Fact(8)) Fact(8) Returns (8*Fact(7)) Fact(7) Returns (7*Fact(6)) Fact(6) Returns (6*Fact(5)) Fact(5) Returns (5*Fact(4)) Fact(4) Returns (4*Fact(3)) Fact(3) Returns (3*Fact(2)) Fact(2) Returns (2*Fact(1)) Fact(1) Returns (1*Fact(0)) Fact(0) Returns 1
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}