COP3502_10_Recursion2 - More Recursion Computer Science...

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

View Full Document Right Arrow Icon
Computer Science Department University of Central Florida More Recursion COP 3502 – Computer Science I
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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!)
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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)); }
Background image of page 7

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

View Full DocumentRight Arrow Icon
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)
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 09/21/2011.

Page1 / 49

COP3502_10_Recursion2 - More Recursion Computer Science...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online