For each of the problems below state the purpose in your own words, specify inpu
ID: 3887923 • Letter: F
Question
For each of the problems below state the purpose in your own words, specify input that is needed, expected output, and the step by step process that will obtain the output from the input (the algorithm). Please do not write any C++ code, just a algorithm for step by step insturction. (Please include test data.)
Write a process that will use the Monte Carlo method to estimate PI as described below. The Monte Carlo method to estimate probability generates a large number of points then determines how many points meet a specific condition. Consider the figure below containing a square with a circle inside of it, both of which are centered on zero.
The area of the circle would be r2 where r is 1. The area of the square is s2 where s is 2 (-1 to 1). Therefore the ratio of the area of the circle to the area of the square would be /4. Now suppose we generate random points to fall uniformly in the area of the square (x varies from -1 to 1 and y varies from -1 to 1). We can state ratio of the number of points falling inside the circle to the total number of the points is equal to the ratio of the area of the circle to the area of the square.
/4 = (number of points in the circle)/(total number of points) Therefore can be defined by the equation
= 4 * (number of points in the circle)/(total number of points)
Generate a process that would generate random numbers between –1 and 1 for x and y positions of 10000 points and count how many points fall with the circle, then determine an estimated version of .
Thanks.
-1 -1Explanation / Answer
Notes: I actually wrote the code and then steps to work on it as comments ordered numerically. Look for
// X. Something to do. If you're stuck some place in this code, do comment.
Code with Steps:
#include <iostream>
#include <cmath>
using namespace std;
/**
* Generates **numberOf** random numbers.
* @param {int} numberOf Number of random numbers to generate.
* @param {int} start Lower Cut Off value, should be passed in as x1000.
* @param {int} end Upper Cut Off value, should be passed in as x1000.
* @returns {double *} generated random numbers array.
*/
double * generateRandom(int numberOf, int start, int end)
{
// make space for numberOf randomNumber-s.
double *randomNumber = (double *) malloc(numberOf*sizeof(double));
for (size_t i = 0; i < numberOf; ++i) {
randomNumber[i] = (rand() % end) + start;
randomNumber[i] /= 1000;
}
return randomNumber;
}
bool checkInCircle(double x, double y)
{
double circleCenterX = 0;
double circleCenterY = 0;
double circleRadius = 1;
double distanceFromCenter =
sqrt(pow(x - circleCenterX, 2) + pow(y - circleCenterY, 2));
if (distanceFromCenter < circleRadius) {
return true;
} else {
return false;
}
}
int main()
{
double SIZE{10000};
double *randomNumbers;
double numPointsInCircle{0};
// 1. generate 1000 random numbers between 0 and 1.
randomNumbers = generateRandom(10000, 0, 1000);
// 2. iterate on all numbers with a step of 2, we can use i and i+1 as x, y.
for (int i = 0; i < SIZE-1; i+=2) {
// 3. for each iteration check if the number is within randius of circle's
// distance from the center of circle.
if (checkInCircle(randomNumbers[i], randomNumbers[i+1])) {
numPointsInCircle++;
} else {
// do none.
}
}
// 4. use formula.
double pi = 4 * (numPointsInCircle / SIZE);
// 5. Show output.
cout << pi << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.