• No se han encontrado resultados

Evaluación de los geotextiles inoculados con bacterias en los niveles de

6. RESULTADOS

6.10. Evaluación de los geotextiles inoculados con bacterias en los niveles de

While the Combinations program can be interesting in its own right, the purpose of this example is to illustrate the steps involved in executing functions. In C++, all programs begin by making a call to the function main. To implement a function call, the system—which encompasses both the operating system you’re using and the hardware on which it runs—creates a new stack frame to keep track of the local variables that function declares. In the Combinations program, the main function declares two integers, n and k, so the stack frame must include space for these variables.

In the diagrams in this book, each stack frame appears as a rectangle surrounded by a double line. The frame rectangle shows the code for the function along with a pointing-hand icon that makes it easy to keep track of the current execution point. It also contains labeled boxes for each of the local variables. The stack frame for main therefore looks like this when execution begins:

From this point, the system executes the statements in order, printing out the prompts on the console, reading in the associated values from the user, and storing those values in the variables in that frame. If the user enters the values shown in the earlier sample run, the frame will look like this when it reaches the statement that displays the result:

int main() { int n, k;

cout << "Enter the number of objects (n): "; cin >> n;

cout << "Enter the number to be chosen (k): "; cin >> k;

cout << "C(n, k) = " << combinations(n, k) << endl; return 0;

} k n

int main() { int n, k;

cout << "Enter the number of objects (n): "; cin >> n;

cout << "Enter the number to be chosen (k): "; cin >> k;

cout << "C(n, k) = " << combinations(n, k) << endl; return 0; } n 6 k 2

Before the program can complete the output line, it has to evaluate the call to combinations(n, k). At this point, the function main is calling the function combinations, which means that the computer has to go through all the steps that are required in making a function call.

The first step is to evaluate the arguments in the context of the current frame. The variable n has the value 6, and the variable k has the value 2. These two arguments are then copied into the parameter variables n and k when the computer creates the combinations stack frame. The new frame gets stacked on top of the old one, which allows the computer to remember the values of the local variables in main, even though they are not currently accessible. The situation after creating the new frame and initializing the parameter variables looks like this:

To compute the value of the combinations function, the program must make three calls to the function fact. In C++, those function calls can happen in any order, but it’s easiest to process them from left to right. The first call, therefore, is the call to fact(n). To evaluate this function, the system must create yet another stack frame, this time for the function fact with an argument value of 6:

Unlike the earlier stack frames, the frame for fact includes both parameters and local variable. The parameter n is initialized to the value of the calling argument

int main() { int n, k;

cout << "Enter the number of objects (n): "; cin >> n;

cout << "Enter the number to be chosen (k): "; cin >> k;

cout << "C(n, k) = " << combinations(n, k) << endl; return 0;

}

int combinations(int n, int k) {

return fact(n) / ( fact(k) * fact(n - k) ); } n 6 k 2int main() { int n, k;

cout << "Enter the number of objects (n): "; cin >> n;

cout << "Enter the number to be chosen (k): "; cin >> k;

cout << "C(n, k) = " << combinations(n, k) << endl; return 0;

}

int combinations(int n, int k) {

return fact(n) / ( fact(k) * fact(n - k) ); }int fact(int n) { int result = 1;

for (int i = 1; i <= n; i++) { result *= i; } return result; } n 6 result i

and therefore has the value 6. The two local variables, i and result, have not yet been initialized, but the system nonetheless needs to reserve space in the frame for those variables. Until you assign a new value to those variables, they will contain whatever data happened to be left over in the memory cells assigned to that stack frame, which is completely unpredictable. It is therefore important to initialize all local variables before you use them, ideally as part of the declaration.

The system then executes the statements in the function fact. In this instance, the body of the for loop will be executed six times. On each cycle, the value of result is multiplied by the loop index i, which means that it will eventually hold the value 720 (1×2×3×4×5×6 or 6!). When the program reaches the return statement, the stack frame looks like this:

In this diagram, the box for the variable i is empty, because the value of i is no longer defined at this point in the program. In C++, index variables declared in a for loop header are accessible only inside the loop body. Showing an empty box emphasizes the fact that the value of i is no longer available.

Returning from a function involves copying the value of the return expression (in this case the local variable result), into the point at which the call occurred. The frame for fact is then discarded, which leads to the following configuration:

int main() { int n, k;

cout << "Enter the number of objects (n): "; cin >> n;

cout << "Enter the number to be chosen (k): "; cin >> k;

cout << "C(n, k) = " << combinations(n, k) << endl; return 0;

}

int combinations(int n, int k) {

return fact(n) / ( fact(k) * fact(n - k) ); }int fact(int n) { int result = 1;

for (int i = 1; i <= n; i++) { result *= i; } return result; } n 6 result 720 iint main() { int n, k;

cout << "Enter the number of objects (n): "; cin >> n;

cout << "Enter the number to be chosen (k): "; cin >> k;

cout << "C(n, k) = " << combinations(n, k) << endl; return 0;

}

int combinations(int n, int k) {

return fact(n) / ( fact(k) * fact(n - k) ); } n 6 k 2 720

The next step in the process is to make a second call to fact, this time with the argument k. In the calling frame, k has the value 2. That value is then used to initialize the parameter n in the new stack frame, as follows:

The computation of fact(2) is a bit easier to perform in one’s head than the earlier call to fact(6). This time around, the value of result will be 2, which is then returned to the calling frame, like this:

The code for combinations makes one more call to fact, this time with the argument n - k. As before, this call creates a new frame with n equal to 4:

int main() { int n, k;

cout << "Enter the number of objects (n): "; cin >> n;

cout << "Enter the number to be chosen (k): "; cin >> k;

cout << "C(n, k) = " << combinations(n, k) << endl; return 0;

}

int combinations(int n, int k) {

return fact(n) / ( fact(k) * fact(n - k) ); }int fact(int n) { int result = 1;

for (int i = 1; i <= n; i++) { result *= i; } return result; } n 2 result iint main() { int n, k;

cout << "Enter the number of objects (n): "; cin >> n;

cout << "Enter the number to be chosen (k): "; cin >> k;

cout << "C(n, k) = " << combinations(n, k) << endl; return 0;

}

int combinations(int n, int k) {

return fact(n) / ( fact(k) * fact(n - k) ); } n 6 k 2 720 2 int main() { int n, k;

cout << "Enter the number of objects (n): "; cin >> n;

cout << "Enter the number to be chosen (k): "; cin >> k;

cout << "C(n, k) = " << combinations(n, k) << endl; return 0;

}

int combinations(int n, int k) {

return fact(n) / ( fact(k) * fact(n - k) ); }int fact(int n) { int result = 1;

for (int i = 1; i <= n; i++) { result *= i; } return result; } n 4 result i

The value of fact(4) is 1×2×3×4, or 24. When this call returns, the system is able to fill in the last of the missing values in the calculation, as follows:

The computer then divides 720 by the product of 2 and 24 to get the answer 15. This value is then returned to the main function, which leads to the following state:

From here, all that remains is to generate the output line and return from the main function, which completes the execution of the program.

Documento similar