{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

P.Lecture - 3.4.08

# P.Lecture - 3.4.08 - Announcements Assignment 4 D i Designs...

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

Announcements Assignment 4 D i d RIGHT NOW Designs due Due Thursday before midnight Topics for today Recursion

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

View Full Document
Recursion Recursion in C is simply when a function calls itself It is also an important problem solving approach A function is defined recursively if it has the following two parts An anchor or base case The function is defined for one or more specific values of the parameter(s) An inductive or recursive case The function's value for current parameter(s) is defined in t f i l d fi d f ti l d/ terms of previously defined function values and/or parameter(s)
A classic example is computing the factorial of a non N! negative integer. The mathematical formulation of n! is: 0! = 1 base case, by definition n! = n * (n 1)! Recursive case Represents the number of permutations of n symbols symbols, e.g. the three symbols a,c,r 3! = 6: car, rac, arc, acr, rca, cra 3! 6: car, rac, arc, acr, rca, cra 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120

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

View Full Document
N Factorial Program /* C t f t i l f i i t */ /* Compute factorial of a given integer */ /* usual stuff in here */ int factorial (int); i t i ( ) { int main ( ) { printf ("Please enter a nonnegative integer\n”); int num; f (“ %d” ) scanf (“ %d”, num); printf (“The factorial of %d is %d ”, num, factorial(num)); return 0; } /* Recursive function for computing the factorial of an int n */ int factorial (int n) { if (n == 0) return 1; /* base or terminating case */ else return n*factorial (n 1); /* recursive call for n 1 case*/ return n factorial (n 1); / recursive call for n 1 case / }
Recursive Descent and Ascent Every recursive call is like a call to a new copy of the function i ll i lif h i i Every recursive call must simplify the computation in some way There must be base cases to handle the simplest computations (terminal cases) For example; to compute factorial (4) factorial(4) calls factorial(3) factorial(3) calls factorial(2) factorial(2) calls factorial(1) factorial(1) calls factorial(0) factorial(0) returns 1 factorial(1) returns 1 * 1 = 1 f i l(2) 1 * 2 2 factorial(2) returns 1 * 2 = 2 factorial(3) returns 2 * 3 = 6 factorial(4) returns 6 * 4 = 24

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

View Full Document
Recursive Example X n Consider it as a recursive power function call it as power (x, n); /* x is a real #, n an int*/ double power (double x, int n) { if ( n = = 0 ) return 1.0; else return x * power (x, n 1); } What is the base (anchor) case ? What is the recursive (inductive) case ? How does the base keep it from going forever?
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 24

P.Lecture - 3.4.08 - Announcements Assignment 4 D i Designs...

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

View Full Document
Ask a homework question - tutors are online