For this assignment you are to implement the Monte Carlo alg

For this assignment you are to implement the Monte Carlo algorithm for the estimation of Pi. Your program should ask the user for the value of N. Your program should then randomly pick N points with x and y coordinates each between (0,1). Let M be the number of points that fall within the circle. Your program should then output the value of Pi, which is calculated using the following formula given input N:

Pi = (4*M) / N

Solution

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int WithInCircle (float a, float b) {

float value = a*a + b*b ;

float root = sqrt(value)l

if (root < 1) {

return 1;

}

else {

return 0;

}

}

int main() {

int N ;

int i;

int M=0;

printf(\"Enter N value : \");

scanf(\"%d\",N);

for(i=0; i < N; i++ ) {

float xCor = ((float)rand()/(float)(RAND_MAX)) * 1;

float yCor = ((float)rand()/(float)(RAND_MAX)) * 1;

int result = WithInCircle (xCor,yCor);

if(result == 1) {

   M++;

}

}

float Pi = 4*M/N;

}

For this assignment you are to implement the Monte Carlo algorithm for the estimation of Pi. Your program should ask the user for the value of N. Your program s
For this assignment you are to implement the Monte Carlo algorithm for the estimation of Pi. Your program should ask the user for the value of N. Your program s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site