Write a C program that estimates the value of using a Monte Carlo simulation. Se
ID: 3759419 • Letter: W
Question
Write a C program that estimates the value of using a Monte Carlo simulation. See the following website for some additional details: http://polymer.bu.edu/java/java/montepi/MontePi.html
1. Statement of the problem
2. An explanation of the problem
Specifications:
Functions:
1. void getRandomXY(*float x, *float y)
a. gives a random (x,y) pair
2. int insideCircle(float x, float y)
a. returns whether the point is inside the circle (0->false, 1->true)
* This is the minimum functions that you must use. You may use others if you like. Outputs:
1. Your estimated value of
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void getRandomXY(float *x, float *y)
{
*x = (double)rand()/RAND_MAX;
*y = (double)rand()/RAND_MAX;
}
int insideCircle(float x, float y)
{
if(x*x + y*y <= 1)
return 1;
return 0;
}
int main()
{
float x, y, pi;
int i, numberOfDartsInSquare = 10000, numberOfDartsInCircle = 0;
for(i = 0; i < numberOfDartsInSquare; i++)
{
getRandomXY(&x, &y);
if(insideCircle(x, y))
numberOfDartsInCircle++;
}
pi = 4.0 * (float)numberOfDartsInCircle / numberOfDartsInSquare;
printf("The Monte Carlo PI estimate for %i number of darts is: %f ", numberOfDartsInSquare, pi);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.