Lecture 2b Recursion

Lecture 2b Recursion - Recursion (Part b) Objectives 1....

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Recursion (Part b) Objectives 1. Operations behind recursion 2. Disadvantages of recursion 3. Two speed-up techniques: memorization and backtracking How is recursion carried out? #include <iostream> using namespace std; //pre: given x, n >= 1 //post: return x^n 1: int power( int x, int n ){ 2: if( n==1 ){ 3: return x; 4: }else{ 5: int half = power(x,n/2); 6: return half*half*(n%2?x:1); 7: } 8: } 1: int main(){ 2: int x, n; 3: cin >> x >> n; 4: cout << power( x, n ) << endl; 5: return 0; 6: } ¡ The same function is executed many times. ¡ How to maintain information of different function calls? ¡ Memory space: stack ¡ One activation record for each call Stack and activation record y A stack is like a box: New item is put on top; Always take the top item. y Last in first out y put – Push an item; take – Pop an item y Activation record stores Stack Current address Return address Local variables Arguments Activation record (stack frame) Running the power() program y The activation record for main() is first pushed to the stack. y Current address is incremented as the program runs. Stack Current address Return address Local variables Arguments Activation record (stack frame) main() line 1 OS int x int n Running the power() program y The activation record for main() is first pushed to the stack. y Current address is incremented as the program runs. y Assume 4 and 3 are entered Stack Current address Return address Local variables Arguments Activation record (stack frame) main() line 3 OS int x = 4 int n = 3 Running the power() program y The activation record for main() is first pushed to the stack. y Current address is incremented as the program runs. y Assume 4 and 3 are entered . y When a function is called, a new activation record is pushed to the stack. Stack Current address Return address Local variables Arguments Activation record (stack frame) main() line 4 OS int x = 4 int n = 3 power() line 1 int half int x=4 int n=3 Running the power() program y The activation record for main() is first pushed to the stack. y Current address is incremented as the program runs. y Assume 4 and 3 are entered . y When a function is called, a new activation record is pushed to the stack....
View Full Document

Page1 / 26

Lecture 2b Recursion - Recursion (Part b) Objectives 1....

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