One problem for Monte Carlo Programming

Problem: Try this out for a variety of n and tell me how large of an n you needed to get an approximation of accurate to 3 decimal places.

I know if I input the number of iterations, how to approximate the pi value (just the for loop). I try to use the while loop to solve problem,however, it seems not work

Here is my code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

using namespace std;

int main()
{
int n=0,i, count=0;
double x,y,z,pi;

count=0;


while (pi==3.141)
{
x=(double)rand()/RAND_MAX;
y=(double)rand()/RAND_MAX;
if (abs(x) <= 0.5 && abs(y) <= 0.5)
z=x*x+y*y;
if (z<=1) count++;
}


n=double(count)/(pi/4);

/*pi=(double)count/n*4;*/

cout << "estimate of number of iteration is " << n;

}
Last edited on
how to solve this problem?
Anyone can help me to solve this problem?
Topic archived. No new replies allowed.