lecture8

lecture8 - Recursion Recursion Recursion Wednesday,April26...

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

View Full Document Right Arrow Icon
Wednesday, April 26 th Recursion Recursion Recursion One day A mad poet with little to say Gave a poem away That started: One day A mad poet with little to say Gave a poem away That started: One day … etc -Martin Gardner (famous mathematician)
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! Before we talk about recursion, its important to understand how parameters  are passed to functions in C++. void bar(int b) { cout << b << endl; } void foo(int a) { cout << a << endl; bar(a*2); } void main(void) { int x = 1; foo(x); foo(3); }   1 x a 1 b 2 Output: 1 2 a 3 3 b 6 6 Watch our  variable diagrams  very  carefully! What  data structure  does this remind  you of?
Background image of page 2
Recursion! When you  pass variables to a function , C++  pushes  those variables onto a  stack  in the computers memory.  When your  function returns , the variables  are  popped  off the stack and go away. Every time you  declare a local variable  or  call a function , your program  uses the PC’s stack automatically!  Recursion , as we’ll learn, relies heavily on the stack.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Recursion! Recursion  is a programming technique where a  function calls itself recursive function  works by applying itself on a simpler sub-problem over  and over until the most basic subproblem is reached and handled trivially.  Recursive algorithm to a sort a deck of index cards:  1. Compute the  median number  in the deck  2. Put cards with values  less than the median  in a pile on the       left  3. Put cards with values  greater than the median  in a pile on       the right  4. Repeat steps 1-4 on each sub-pile
Background image of page 4
Recursion! Recursive sort: 1. Compute the  median number   in the pile 1. Put cards with values  less  than the median  in a pile on  the  left 3. Put cards with values  greater  than or equal to the median   in a pile on the  right 4. Repeat steps 1-4 on each sub- pile 9 3 2 7 4 1 5 Median! 5 9 3 2 7 4 1 Median! 3 2 1 Median! 5 9 4 1 Median! 3 2 4 5 7 7 9 Median! By applying the same algorithm to simpler and  simpler subproblems, we can quickly  solve  complex problems !
Background image of page 5

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

View Full DocumentRight Arrow Icon
The Two Rules of Recursion RULE ONE:  The Stopping Condition or Base Case : We take the simplest, most basic  problem and handle this directly and return a result  without using  recursion Every recursive function must have a “ stopping condition !” Remember: A recursive function  calls itself . Therefore, every recursive function  must have some mechanism to allow the function to stop calling itself.  
Background image of page 6
The Stopping Condition int print_nums(int num) { cout << num << endl; if (num == 0) return; print_nums(num-1); } Can you identify the  stopping  condition  in this function?  Here’s our first recursive function. This 
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 30

lecture8 - Recursion Recursion Recursion Wednesday,April26...

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

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