2 2 1 1 1 0 0 1 base case 1 1 1 2 2 6 6 24 we can

This preview shows 27 out of 35 pages.

2! = 2 *  1! 1! = 1 *  0! 0! =  1 ( base case ) 1 1 1 2 2 6 6 24 We can write a more formal definition: fact(N) = 1 if N = 0 N * fact(N-1) otherwise   Let’s see how to do it in C++ // stopping condition // simplifying
Image of page 27

Subscribe to view the full document.

Recursion: Taking a Factorial int fact(int num) { if (num == 0) return (1); return( num * fact(num-1)); } What if we didn’t have a  stopping condition /base case?  stopping condition simplifying code What if we didn’t have  simplifying   code
Image of page 28
Working Through Recursion int fact(int n) { if (n == 0) return (1); return(n * fact(n-1)); } int fact(int n) { if (n == 0) return (1); return(n * fact(n-1)); } int fact(int n) { if (n == 0) return (1); return(n * fact(n-1)); } int fact(int n) { if (n == 0) return (1); return(n * fact(n-1)); } void main(void) { int result; result = fact(3); cout << result; } result 3 == 0  2       n        3   3 2 == 0  1       n        2   1 == 0  0       n        1   0 == 0  1       n        0   1  1 2  2 6  6 6
Image of page 29

Subscribe to view the full document.

More Recursion Let’s examine a recursive function that prints a string out to the screen in  reverse. Algorithm:   Inputs : a  string , and its  length L  1. If the length  is  zero , then DONE.
Image of page 30
More Recursion! void writeReverse(string s, int size) { if (size > 0)     { // write last character of string cout << s[size-1]; // write first size-1 characters writeReverse (s,size-1);     } } main() { writeReverse(“ohm”,3); } Let’s examine a recursive  function that prints a  string in reverse. What’s the  base case ? What’s the  simplifying case ?
Image of page 31

Subscribe to view the full document.

Recursion: Reversing a String void writeReverse(string s, int size) { if (size > 0) { cout << s[size-1]; writeReverse(s,size-1); } } main() { writeReverse(“ohm”,3); } “ohm”           3 Output: m void writeReverse(string s, int size) { if (size > 0) { cout << s[size-1]; writeReverse(s,size-1); } } “ohm”           2 h void writeReverse(string s, int size) { if (size > 0) { cout << s[size-1]; writeReverse(s,size-1); } } “ohm”           1 o void writeReverse(string s, { if (size > 0) { cout << s[size-1]; writeReverse(s,size-1); } } “ohm”    0
Image of page 32
Recursion: Another Reverse void writeRev2(string s, int pos) { if (pos < s.length()) { writeRev2( s,pos+1 ); cout << s[ pos ]; } } main() { writeRev2(“Carey”,0); } Here’s another version of  our reverse function…  Can you tell what’s  different about it? What’s the  base case ? What’s the  simplifying case ?
Image of page 33

Subscribe to view the full document.

Here’s how the new reverse algorithm works: Recursion: Another Reverse Example : If the string is “ abcd ”, then the recursive function first calls itself to print out  bcd ” in reverse, and when its done, prints out “ a ”.   However, in order to print out “ bcd ” in reverse, the recursive function must call itself to  print out “ cd ” in reverse, and then print out “ b ”.   However, in order to print out “ cd ” in reverse, the recursive function must call itself to  print out “ d ” in reverse, and then print out “ c ”.   To print out “ d ” in reverse, just print it to the screen.
Image of page 34
Image of page 35
You've reached the end of this preview.
  • Spring '09
  • John
  • Recursion, Grendel, The Queen, Albert Brandy Carol David Eugene Frank Gordon Grendel

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern