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

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

View Full Document Right Arrow Icon
Announcements Assignment 4 Designs due RIGHT NOW Due Thursday before midnight Topics for today Recursion
Background image of page 1

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

View Full DocumentRight Arrow Icon
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 arameter(s) parameter(s) An inductive or recursive case The function's value for current parameter(s) is defined in f il df i d ft i l d/ terms of previously defined function values and/or parameter(s)
Background image of page 2
classic example is computing the factorial of a non N! A classic example is computing the factorial of a non negative integer. The mathematical formulation of n! is: 0! = 1 base case, by definition n! = n * (n 1)! Recursive case epresents the number of permutations of n mbols Represents the number of permutations of n symbols, e.g. the three symbols a,c,r ! = 6: car, rac, arc, acr, rca, cra 3! 6: car, rac, arc, acr, rca, cra 0! = 1 1! = 1 2! = 2 3! = 6 ! = 24 4! = 24 5! = 120
Background image of page 3

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

View Full DocumentRight Arrow Icon
N Factorial Program Ct fti l f i it */ /* Compute factorial of a given integer */ /* usual stuff in here */ int factorial (int); t i ( ) { int main ( ) { printf ("Please enter a nonnegative integer\n”); int num; 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 */ t factorial (int n) { int factorial (int n) { if (n == 0) return 1; /* base or terminating case */ else turn n*factorial (n ); /* recursive call for n case*/ return n factorial (n 1); / recursive call for n 1 case / }
Background image of page 4
Recursive Descent and Ascent Every recursive call is like a call to a new copy of the function Every recursive call must simplify the computation in some way here must be base cases to handle the simplest There must be base cases to handle the simplest computations (terminal cases) For example; to compute factorial (4) p; p () 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 factorial(2) returns 1 * 2 = 2 factorial(3) returns 2 * 3 = 6 factorial(4) returns 6 * 4 = 24
Background image of page 5

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

View Full DocumentRight Arrow Icon
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; lse else return x * power (x, n 1); } What is the base (anchor) case ?
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/22/2008 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas at Austin.

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 Right Arrow Icon
Ask a homework question - tutors are online