{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture9slides

lecture9slides - Monday November 5th Todays Topics 1 2 3 4...

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

View Full Document Right Arrow Icon
Monday, November 5 th Today’s Topics 1. Talk about midterm (give it back?) 2. Questions about project #5 3. “The stack” and functions 4. Recursion: OH NO! 5. Pointers: OH NO!
Image of page 1

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

View Full Document Right Arrow Icon
The Stack The stack is a special memory area of the computer where local variables are stored. The “box diagrams” I draw are all representations of the stack. Every time you declare a local variable or call a function , your program uses the stack. The stack is said to “grow upward. 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); }
Image of page 2
Recursion Recursion Drives Some People Crazy! How do we calculate a factorial? int fact(int n) { int f = 1; while (n > 0) { f = f * n; n = n – 1; } return(f); } 5! = 5 * 4 * 3 * 2 * 1 What’s another way to calculate a factorial? 5! = 5 * 4! and 4! = 4 * 3! and 3! = 3 * 2!, etc.. Or: N! = N * (N-1)! fact(N) = N * fact(N-1); This is a recursive way of thinking!
Image of page 3

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

View Full Document Right Arrow Icon
int recfac(int num) { if (num == 0) return (1); int f = num * recfac(num-1); return(f); } main() { cout << recfac(0); cout << recfac(3); }
Image of page 4
Recursion Recursion is calling a function from itself, directly (simple recursion) or via calls to other functions (mutual recursion). A recursive function works by applying itself on a simpler sub-problem over and over until the most basic subproblem is reached and handled trivially. THE TWO RULES OF RECURSION RULE #1 1. The Stopping Condition: We take the simplest, most basic problem and we handle this directly and return a result WITHOUT USING RECURSION. Every recursive function must have a “stopping condition!” Remember that a recursive function is a function which continues to call itself. Therefore, every recursive function must have some mechanism to allow the function to stop calling itself.
Image of page 5

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

View Full Document Right Arrow Icon
What is our base case? int recfac(int num) { if (num == 0) return (1); int f = num * recfac(num-1); return(f); } What if we didn’t have a stopping condition/base case?
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ 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