C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture13_RecursionArrays

# C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture13_RecursionArrays

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

1 ee312 Lecture 13 1 Announcements Lecture 13 • Topics for today – Recursion – More Arrays • Read for Next Week – Structures, Enums and Unions - chapter 16 – C Data Structures, Pointers 101 - chapter 11 ee312 Lecture 13 2 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 terms of previously defined function values and/or parameter(s)

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

View Full Document
2 ee312 Lecture 13 3 Recursive Example Consider a recursive power function double power (double x, int n) { if ( n = = 0 ) return 1.0; else return x * power (x, n-1); } Which is the base case ? Which is the recursive (inductive) case ? How does the base keep it from going forever? What happens when we call it, e.g. double z = power (3.0, 5); X n ee312 Lecture 13 4 Recursive Example • Note the results of a call – Recursive calls – Resolution of the calls
3 ee312 Lecture 13 5 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 Represents the number of permutations of n symbols, e.g. the three symbols a,c,r 3! = 6: car, rac, arc, acr, rca, cra 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 N! ee312 Lecture 13 6 N-Factorial Program /* Compute factorial of a given integer usual stuff in here */ int factorial (int); 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 */ 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 */ }

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

View Full Document
4 ee312 Lecture 13 7 • Every recursive call is like a call to a new copy of the function • Every recursive call must simplify the computation in some way • There must be base cases to handle the simplest computations (terminal cases) 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
This is the end of the preview. Sign up to access the rest of the document.

## C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture13_RecursionArrays

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

View Full Document
Ask a homework question - tutors are online