{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CarranoChap2 - Program Memory Allocation Program B's...

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

View Full Document Right Arrow Icon
 Carrano -   CS 150 11 Program Memory Allocation Program B’s Run-Time Stack r ( Room for B’s growth ) Program B’s Data Heap Program B Program A’s Run-Time Stack r ( Room for A’s growth ) Program A’s Data Heap Program A Operating System Each program being executed on a computer is allocated extra space which it could use for its stack of function calls, and/or for “dynamically” expanding its use of memory for data. The memory used for data is known as a “heap”.
Background image of page 1

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

View Full Document Right Arrow Icon
 Carrano -   CS 150 22 A Program’s Run-Time Stack Program XYZ: void main() { int snap = 7; int yap = 10; int blap = 100; blap = snippy(snap); yippy(yap, blap); zippy(yap); } int snippy(int snoo) { int hep = yippy(snoo,5); if (hep > 0) return hep; else return 0; } void yippy(int &yoo, int boo) { int i; for (i = 1; i < boo; i+ +) yoo += i; return; } void zippy(int zoo) { cout << zoo << endl; } snap: 7 yap: 10 blap: 100 void main() { int snap = 7; int yap = 10; int blap = 100; blap = snippy(snap); yippy(yap, blap); zippy(yap); } snoo: 7 hep: ?? ? int snippy(int snoo) { int hep = yippy(snoo,5); if (hep > 0) return hep; else return 0; } yoo: boo: 5 int yippy(int &yoo, int boo) { int i; for (i = 1; i < boo; i++) yoo += i; return boo + yoo; } i: 5
Background image of page 2
 Carrano -   CS 150 33 Recursion It is sometimes useful to have a function call itself, making it  a recursive  function.  This approach often enables the  programmer to solve a problem by repeatedly solving smaller,  more manageable versions of the problem, until the original  problem’s solution becomes apparent. Care must be taken when using recursion, however.  Because  a new incarnation of the function is placed upon the run-time  stack each time the function is recursively called, there is a  danger that the stack will overflow. To prevent this from happening, the function must contain a  termination condition, which will recognize when the  recursion should halt, and pop the current incarnation of the  recursive function off of the run-time stack.
Background image of page 3

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

View Full Document Right Arrow Icon
 Carrano -   CS 150 44 Recursion Example #1: Factorial ///////////////////////////////////////////////////////////////////////// /// // Program file: factorial.cpp // // This program queries the user for a non-negative integer and then uses // // a recursive function to calculate the factorial of the input value. // ///////////////////////////////////////////////////////////////////////// /// #include <iostream> #include <iomanip> using namespace std; int factorial(int n); //////////////////////////////////////////////////////////// // The main function asks the user for a value, calls the // // recursive factorial function, and outputs the result. // //////////////////////////////////////////////////////////// void main() { int number, result; cout << "Enter a non-negative integer for factorial computation: "; cin >> number; while (number < 0) { cout << "I said, enter a NON-NEGATIVE integer: "; cin >> number; } cout << endl; result = factorial(number); cout << "FINAL RESULT: " << number << "! = " << result << endl << endl;
Background image of page 4
 Carrano -   CS 150 55 Factorial (Continued) ///////////////////////////////////////////////////////////////////////// //// // Function factorial recursively calculates the factorial of parameter n, // // outputting entering and exiting messages to demonstrate to the user the // // recursive function calls which make the final calculation possible.
Background image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}