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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
#include <iostream> //preprocessor directive needed in order to use std::cout
//and std::cin
#include <iomanip> //preprocessor directive in order to use any manipulator that
//uses an argument
#include <cmath> //preprocessor directive in order to use square root, etc
//functions
#include <ctime> //defines ctime() for random numbers
#include <cstdlib> //defines rand(), srand(), RAND_MAX
#include <windows.h>//needed for text and background colour changes
#define Pi 2.0*acos(0.0) //Defining Pi as a more accurate value
#include <limits>
#include <string>
using namespace std; //avoids having to uses std:: with cout and cin
int main (int argc, char* argv[])
{
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
system("COLOR F0"); //inverts colours
//Declaring the variables
int a, b, MinNum, MaxNum; //Integer values a and b
double x, y; //Decimal values x and y
string ans= "Y";
//Student number and date
SetConsoleTextAttribute(hConsole, 117);
cout << "STUDENT ID: 15910237"
<< "\nDATE: 16/03/2018"
<< endl;
//Tells the user what is happening
SetConsoleTextAttribute(hConsole, 240);
cout << "\n\nThis program computes a value of Pi using the Monte Carlo"
<< " method and compares this value to the actual value of Pi."
<< "\nIt does this for a range of user inputted integer values"
<< " in steps of 100.\n\nThe values a and b represent"
<< " the minimum and maximum number of randomly generated"
<< " points (x, y)\nrespectively;"
<< " where 0 < (x,y) < 1. If 2 values are entered for a and b such that"
<< " they are greater than 80 and less\nthan 850,"
<< " the program will automatically"
<< " select the minimum and maximum values to be 80 and 850 respectively"
<< "\n\nThe Monte Carlo Pi is calculated by finding the number of"
<< " points that fall with in a circle of radius 1 \nin the positve"
<< " quadrant of a graph and dividing this value by the total number"
<< " of points before multiplying by 4"
<< endl;
do
{
//Ask user for input
cout << "\n\nPlease enter an integer value for a,"
" where (a > 0): ";
while(!(cin >> a))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
SetConsoleTextAttribute(hConsole, 252);
cout << "\nValue for a is not valid, please enter an integer value"
<< " greater than 0: ";
SetConsoleTextAttribute(hConsole,240);
}
if (a < 0)
{
SetConsoleTextAttribute(hConsole, 252);
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\nValue for a is not valid, please enter an integer"
<< " value greater than 0: ";
cin >> a; //enter value b if original value is invalid
SetConsoleTextAttribute(hConsole,240);
}
cout << "\n\nPlease enter an integer value for b,"
" where (b > 0): ";
while(!(cin >> b))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
SetConsoleTextAttribute(hConsole, 252);
cout << "\nValue for b is not valid, please enter an integer value"
<< " greater than 0: ";
SetConsoleTextAttribute(hConsole,240);
}
if (b < 0)
{
SetConsoleTextAttribute(hConsole, 252);
cout << "\nValue for b is not valid, please enter an integer value"
<< " gretaer than 0: ";
cin >> b; //enter value b if original value is invalid
SetConsoleTextAttribute(hConsole,240);
}
if (a < 80 && a < b && a >0)
{
MinNum = a;
}
else if (b < 80 && b < a && b > 0)
{
MinNum = b;
}
else
{
MinNum = 80;
}
if (b > 850 && b > a && b > 0)
{
MaxNum = b;
}
else if (a > 850 && a > b && a >0)
{
MaxNum = a;
}
else
{
MaxNum = 850;
}
srand( time( 0 ) ); //Creates the seed time
//Print out headings
SetConsoleTextAttribute(hConsole, 143);
cout << "\n\n" << left
<< setfill('d')
<< setw( 20 ) << "Total Points "
<< setw( 3 ) << " $ "
<< setw( 20 ) << "Points in Circle "
<< setw( 3 ) << " $ "
<< setw( 20 ) << "Value Monte Carlo Pi"
<< setw( 3 ) << " $ "
<< setw( 20 ) << "Difference vs Pi "
<< endl;
for ( int N = MinNum; N <= MaxNum; N += 100 ) //perform loop
//with increment 100
{
int NumberOfPointsInCircle = 0; //Number of times a point is in the
//circle of radius 1
for ( int i = 0; i < N; i++ ) //Loop to generate random points
{
x = ((double) rand() / (RAND_MAX)); //Random x coordinate
y = ((double) rand() / (RAND_MAX)); //Random y coordinate
if ( sqrt((x * x) + (y * y)) < 1 ) //Distance formula
{
NumberOfPointsInCircle++; //if equation is correct, add 1 to
//number of points in circle
}
}
double MonteCarloPi = 4.0 * NumberOfPointsInCircle / N; //Formula to
//calculate Pi using Monte Carlo method
double DifferencePi = MonteCarloPi - Pi; //formula to calculate the
//difference between Monte Carlo Pi and actual Pi
//Print out the results
SetConsoleTextAttribute(hConsole, 245);
cout << setfill ('d')
<< scientific << setprecision ( 4 ) << left
<< setw(20) << N;
SetConsoleTextAttribute(hConsole, 143);
cout << setw( 3 ) << " $ " ;
SetConsoleTextAttribute(hConsole, 245);
cout << setw( 20 ) << NumberOfPointsInCircle;
SetConsoleTextAttribute(hConsole, 143);
cout << setw( 3 ) << " $ ";
SetConsoleTextAttribute(hConsole, 245);
cout << setw( 20 ) << MonteCarloPi;
SetConsoleTextAttribute(hConsole, 143);
cout << setw( 3 ) << " $ ";
SetConsoleTextAttribute(hConsole, 245);
cout << setw( 20 ) << DifferencePi
<< endl;
}
SetConsoleTextAttribute(hConsole, 240);
cout << "\nWould you like to try again? (Y/N): ";
cin >> ans;
if (ans == "N" || ans == "n")
{
cin.get();//end program
}
while (ans != "Y" && ans != "N" && ans != "y" && ans != "n")
{
SetConsoleTextAttribute(hConsole, 252);
cout << "\nInvalid input, please enter Y or N: ";
SetConsoleTextAttribute(hConsole, 240);
cin >> ans;
}
}
while (ans == "Y" || ans == "y");
//Let user see results before ending program
SetConsoleTextAttribute(hConsole,240);
cout << "\n\nPress <Enter> to end the program";
cin.get();
}
|