any general comments about my style of coding would be massively helpful |
Avoid non-descriptive variable names except when used as simple counter variable (e.g. i, j).
In the definition of your piMC function, your description seems fine, but I have no idea what t and d are supposed to mean. Give them meaningful names. Also, is r supposed to be "radius"? If so, why are you increasing the radius every time? I see from your comment that r is "number of points inside circle", but it would be better for this to be expressed in the variable name as well, ex: num_points.
cout << scientific << left << setprecision(4) << setfill ('d')
Why are you filling with the letter 'd'? It gives you output like this:
Number of points ddd $ Value of Pi ddddd $ Difference versus Actual Pi
90dddddddddddddddddd $ 2.8000e+00dddddddddd $ 3.4159e-01dddddddddd $
|
Also, to me at least, I don't understand your use of the 'a' and 'b' variables. What is the point of them? What should the user expect if they enter 90 and 100? They have no idea what this "step size" internal functionality is. Why not just give a single number to mean the # of iterations you want the program to run for?
Here's roughly how I would do it (clearly this isn't doing exactly what your program is doing, but does the monte carlo calcluations nonetheless). Feel free to disagree or get ideas from this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std; //avoids having to uses std:: with cout and cin
const double Pi = 2.0 * acos(0.0);
//calculates the Euclidean distance of the point from the origin
double dist(double x, double y)
{
return sqrt(x*x + y*y);
}
int main (int argc, char* argv[])
{
srand(time(NULL));
cout << "Enter number of points to generate: ";
int num_iterations;
cin >> num_iterations;
int num_points_in_circle = 0;
for (int i = 0; i < num_iterations; i++)
{
double x_coord = static_cast<double>(rand()) / RAND_MAX;
double y_coord = static_cast<double>(rand()) / RAND_MAX;
if (dist(x_coord, y_coord) < 1)
{
num_points_in_circle++;
}
// The ratio of the # of points inside the 1st quadrant of the circle to the # of iterations.
// converges to the ratio of the area of the 1st quadrant of the circle to the tatal square area.
// (or, Pi / 4)
double ratio = static_cast<double>(num_points_in_circle) / i;
cout << "New estimate for Pi: " << 4 * ratio << "\n";
}
//Let user see results before ending programme
cout << "\n\nPress enter to end the program";
cin.get();
}
|
Edit, note that the Monte Carlo method seems to converge extremely slowly... there are faster algorithms for calculating Pi that aren't probabilistic
New estimate for Pi: 3.14998
New estimate for Pi: 3.15003
New estimate for Pi: 3.15008
New estimate for Pi: 3.15013
New estimate for Pi: 3.15017
New estimate for Pi: 3.15022
New estimate for Pi: 3.15004
New estimate for Pi: 3.14986
New estimate for Pi: 3.14968
New estimate for Pi: 3.14973
New estimate for Pi: 3.14978
New estimate for Pi: 3.14983
New estimate for Pi: 3.14988
New estimate for Pi: 3.14993
New estimate for Pi: 3.14997
New estimate for Pi: 3.14979
New estimate for Pi: 3.14962
New estimate for Pi: 3.14966
New estimate for Pi: 3.14971
New estimate for Pi: 3.14976
New estimate for Pi: 3.14981
New estimate for Pi: 3.14986
New estimate for Pi: 3.14991
New estimate for Pi: 3.14995
New estimate for Pi: 3.15
New estimate for Pi: 3.15005
New estimate for Pi: 3.1501
New estimate for Pi: 3.14992
New estimate for Pi: 3.14997
New estimate for Pi: 3.15002
New estimate for Pi: 3.14984
New estimate for Pi: 3.14989 |
https://en.wikipedia.org/wiki/Category:Pi_algorithms