Implement the Monte Carlo Method for estimating the value of PI in c++ using thi
ID: 3685254 • Letter: I
Question
Implement the Monte Carlo Method for estimating the value of PI in c++ using this template.
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
// given the coordinates of a point (x,y) computes if the point is inside the circle
// centered at origin with radius r. Returns 'true' if it is inside, 'false' otherwise.
bool isInside(double x, double y, double r)
{
}
// given s, the size of the side of a square that is centered at the origin,
// chooses a random coordinates inside the square, and calls isInside function
// to test if the point is inside the circle or not.
bool throwDart(int s)
{
int x, y;
// assign x and y to two random integers between -s/2 and s/2
//Call the isInside function and return its output.
//You do not have to cast x & y to doubles, it is done automatically.
}
int main()
{
srand(333);
int side; // this is the side of the square and is also our resolution.
int tries; // this is the number of tries.
//Ask the user for the size (integer) of a side of the square
//Get the users input using cin
//Ask the user for the number of tries using cout.
//Get the users input using cin.
int inCount = 0; //counter to track number of throws that fall inside the circle
for(int i = 0; i < tries; ++i)
{
//throw a dart using throwDart method and increment the counter depending on its output.
}
//Compute and display the estimated value of PI. Make sure you are not using integer division.
return 0;
}
Explanation / Answer
// This program implements the Monte Carlo Method for estimating the value of PI.
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
// given the coordinates of a point (x,y) computes if the point is inside the circle
// centered at origin with radius r. Returns 'true' if it is inside, 'false' otherwise.
bool isInside(double x, double y, double r)
{
if ((x*x) + (y*y) < (r*r))
{
return true;
}
return false;
}
// given s, the size of the side of a square that is centered at the origin,
// chooses a random coordinates inside the square, and calls isInside function
// to test if the point is inside the circle or not.
bool throwDart(int s)
{
double h = s/2;
double x = rand() % s - h;
double y = rand() % s - h;
// assign x and y to two random integers between -s/2 and s/2
return isInside(x, y, s);//Call the isInside function and return its output.
//You do not have to cast x & y to doubles, it is done automatically.
}
int main()
{
srand(333);
int side; // this is the side of the square and is also our resolution.
int tries; // this is the number of tries.
//Ask the user for the size (integer) of a side of the square
cout << "Input side: ";
cin >> side;//Get the users input using cin
//Ask the user for the number of tries using cout.
cout << "Input number of tries: ";
cin >> tries;//Get the users input using cin.
int inCount = 0;
//counter to track number of throws that fall inside the circle
for(int i = 0; i < tries; ++i)
{
if (throwDart(side/2) == true)
{
inCount = inCount + 1;
}//throw a dart using throwDart method and increment the counter depending on its output.
}
double x = inCount/tries;
x = 4.0 * x;
cout << "PI: " << x << endl;//Compute and display the estimated value of PI. Make sure you are not using integer division.
return 0;
}