In the first four lines of the program, we include the header files for the libraries used in the program: iostream (input-output streams), iomanip (input/output stream manipulation, which we'll use for output stream formatting), cstdlib (the C standard library containing pseudo-random number generator functions), and ctime (the C time handling library). In the sixth line, we inform the compiler that we will be using names defined in the std namespace in the program.
The main Function and Variable Declaration
In C++, a program begins with the execution of the main function. In our example, this is the only function in the program. Line 8 contains the beginning of the main function definition, which is the start of our algorithm's execution (START block (1)). In the next line, we declare the integer variable n, which we will use shortly. In C++, variables can be declared anywhere before their use, which is why we don't immediately declare the variables i, k, and p here, as they appear later in the program.
- Lines 10 and 11 execute the input/output operation (2), where the input data—the number of points to be sampled—is read.
- In line 12, we call the standard library function srand. This function initializes the built-in pseudo-random number generator with the value provided as the function call argument. As the argument, we provide the result of the function call time(NULL). The time() function is included in the ctime library. Calling it with the NULL argument returns the current system time, which will, of course, be different every time the program is run. If we didn't call this function, the program would generate the same sequence of pseudo-random numbers every time it runs, and as a result, we would always get the same result for a specific number of sampled points. In every C++ program where we use the pseudo-random number generator, we should call the srand(time(NULL)) function once before the first sampling.
- In line 13, we declare two floating-point variables (float)—x and y—where we will store the coordinates of the sampled points. In the next line, we define and initialize the variable k with the value 0—the counter for points lying within the circle of radius 1 (3).
The for Loop Implementation (Monte Carlo Steps)
- Line 15 starts the for loop instruction. The instructions contained between the curly braces { } will be executed n times (the loop control variable i changes from 1 up to n, increasing its value by 1 each time). This line implements part of step (3) of the algorithm by initializing the variable i to 1 at the beginning, and also implements steps (7) and (8) of the algorithm by increasing the control variable i by 1 after each execution of the loop instructions and checking whether the value of i has not yet exceeded the upper limit of the loop (i≤n). Exactly one instruction is always executed inside a for loop. If we need more instructions here, as is the case in our example, we must enclose them between curly braces { } as a compound statement.
- The instructions executed within the loop are contained in lines 16–18. For each sampled point, we first sample its coordinates x and y—step (4) of the algorithm. We use the library function rand() for sampling, whose result is a random (pseudo-random) integer of type int in the range from 0 to RAND_MAX. RAND_MAX is a constant defined in the cstdlib library and specifies the maximum integer generated by the pseudo-random number generator. If we divide the sampled integer by its maximum value (RAND_MAX), the result will be a random number in the range from 0 to 1. However, in C++, the result of dividing two integers is always an integer. If we want to get a random number of type float in the range [0,1], we must force floating-point division on the compiler by converting (casting) at least one of the division operands to a floating-point number. float(RAND_MAX) casts the integer constant RAND_MAX to the float type before the division operation is performed.
- In line 18, we check whether the point with the sampled coordinates x,y lies within the circle of radius r=1 (5) (i.e., x2+y2≤1). If it does, further in the same line, we increase the content of the variable k by 1 (6).
Result Calculation and Program Termination
After the loop finishes, the variable n contains the ** total number of sampled points** (the content of n has not changed), and the variable k contains information about the number of sampled points lying within the circle of radius 1.
- In line 20, we declare the variable p and assign the approximate value of π to it (9), and then in line 21, we display the obtained result (input-output block (10)).
- The instructions in lines 22–23 have no connection with the execution of the algorithm. They serve to pause the program until any character is entered and the Enter key is pressed, which prevents the program window from closing immediately.
- The closing curly brace } in line 24 terminates the main function and, simultaneously, the execution of the entire program (end of algorithm block (11)).
loadposition Spis-mod_AC_en}
