From 538ec774a7303ab3edc3c7fc59a57707aa89a1f6 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 19 Sep 2023 23:23:14 -0400 Subject: [PATCH] displaying activation records --- .../07_order_notation_recursion/README.md | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/lectures/07_order_notation_recursion/README.md b/lectures/07_order_notation_recursion/README.md index ff51b2a..b4a54f4 100644 --- a/lectures/07_order_notation_recursion/README.md +++ b/lectures/07_order_notation_recursion/README.md @@ -145,9 +145,7 @@ for (int j=i; j ![alt text](power_equation.png "power equation") - These are both examples of recursive definitions. @@ -188,6 +186,7 @@ confusing since there are multiple activation records waiting for an answer from - This is illustrated in the following diagram of the call fact(4). Each box is an activation record, the solid lines indicate the function calls, and the dashed lines indicate the returns. Inside of each box we list the parameters and local variables and make notes about the computation. +![alt_text](activation_records.png "chain of activation records") - This chain of activation records is stored in a special part of program memory called the stack ## 7.13 Iteration vs. Recursion @@ -197,10 +196,10 @@ version of factorial: ```cpp int ifact(int n) { -int result = 1; -for (int i=1; i<=n; ++i) -result = result * i; -return result; + int result = 1; + for (int i=1; i<=n; ++i) + result = result * i; + return result; } ``` @@ -242,25 +241,25 @@ recursive function. It is common to have a driver function that just initializes ```cpp void print_vec(std::vector& v, unsigned int i) { -if (i < v.size()) { -cout << i << ": " << v[i] << endl; -print_vec(v, i+1); -} + if (i < v.size()) { + cout << i << ": " << v[i] << endl; + print_vec(v, i+1); + } } ``` ```cpp void print_vec(std::vector& v) { -print_vec(v, 0); + print_vec(v, 0); } ``` What will this print when called in the following code? ```cpp int main() { -std::vector a; -a.push_back(3); a.push_back(5); a.push_back(11); a.push_back(17); -print_vec(a); + std::vector a; + a.push_back(3); a.push_back(5); a.push_back(11); a.push_back(17); + print_vec(a); } ``` How can you change the second print vec function as little as possible so that this code prints the contents