day14 - Recursion Introduction Recursion is a function...

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

View Full Document Right Arrow Icon
Introduction Recursion is a function invoking itself, either directly or indirectly. Many algorithms and mathematical properties are naturally expressed in a recursive manner. It can be used as an alternative to iteration. Recursion is an important and powerful tool in problem solving and programming. It is a programming technique that naturally implements the divide-and-conquer problem solving methodology. Some programming languages are inherently recursive (Lisp, Scheme). In its simplest form the idea of recursion is straightforward: Example 1: Simple countdown timer Recursion - 1 Recursion void count_down(int n) { if (n <= 0) printf(“\nBlast off.\n”); else{ printf(“%d! “, n); count_down(n-1); } } int main () { count_down(10); }
Background image of page 1

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

View Full DocumentRight Arrow Icon
Example 2: Multiplication of natural numbers. a * b = a added to itself b times. (iterative definition) a * b = a if b=1 (recursive definition) a* b = a * (b-1) + a if b > 1 e.g. - 6 * 3 = 6 * 2 + 6 = 6 * 1 + 6 + 6 = 6 + 6 + 6 = 18 Example 3: Factorial Function n! = n * (n-1) * (n-2) * … * 2 * 1 5! = 5 * 4 * 3* 2 * 1 0! = 1 Recursion - 2 int multiply(int a, int b) { if (b == 1) /* stopping case */ return a; else /* recursive step */ return ( a + multiply(a, b-1)); }
Background image of page 2
Mathematical definition n! = 1 if n = 0 n! = n . (n-1) . (n-2). … . 2 . 1 if n > 0 Iterative Definition: Recursive Definition: e.g. - 4! = 4 * 3 * 2 * 1 = 4 * 3! Coded in C: Recursion - 3 p = 1; for (x=n; x>=1; x--) p = p* x; n! = 1 if n = 0 n! = n*(n-1)! if n > 0 int factorial(int n) { if (n ==0) return 1; else return (n * factorial(n- 1)); }
Background image of page 3

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

View Full DocumentRight Arrow Icon
The Nature of Recursion One or more simple cases of the problem (called the stopping cases ) have a simple non-recursive solution. The other cases of the problem can be reduced ( using recursion ) to problems that are closer to stopping cases. Eventually the problem can be reduced to stopping cases only, which are relatively easy to solve. In general: if (stopping case) solve it else reduce the problem using recursion Tracing a Recursive Function Computer uses a stack to keep track of function calls. Whenever a new function is called, all its parameters and local variables are pushed onto the stack along with the memory address of the calling statement (this gives the computer the return point after execution of the function). Recursion - 4
Background image of page 4
Example 1: Tracing the function multiply x = multiply(6,3): Recursion - 5 a = 6 b = 3 3 <= 1? false return (6 + multiply(6,2)) a = 6 b = 2 2 <= 1? false return(6 + multiply(6,1)) a = 6 b = 1 1 <= 1? true return(6)
Background image of page 5

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

View Full DocumentRight Arrow Icon
Example: Tracing a Recursive Function A palindrome is a string of characters that reads the same backwards and forwards (e.g. level, deed, mom) palindrome(5) reads 5 characters and prints them in reverse order. Recursion -
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.

Page1 / 20

day14 - Recursion Introduction Recursion is a function...

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