lecture9slides

lecture9slides - Monday, November 5th Todays Topics 1. 2....

Info iconThis 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!
Background image of page 1

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

View Full DocumentRight 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); }
Background 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!
Background image of page 3

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

View Full DocumentRight 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); }
Background 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.
Background image of page 5

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

View Full DocumentRight Arrow Icon
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? int recfac(int num)
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.

This note was uploaded on 10/08/2011 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Page1 / 24

lecture9slides - Monday, November 5th Todays Topics 1. 2....

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