calculating value of pi using monte carlo method
what is wrong in the code It does not print anything
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
double calculate_pi(const int n)
{
int darts_in_circle=0;
double x= rand()/(double) RAND_MAX;
double y= rand()/(double) RAND_MAX;
for( int i=0;i<n;i++)
{
if(sqrt((x*x)+(y*y))<1)
darts_in_circle++;
}
return darts_in_circle/ (double) (n)*4;
}
int main()
{
calculate_pi(1);
return 0;
}
|
Hello hkataria11,
The program does not print anything because you never tell to print anything.
Try this before the "return 0;" in main:
std::cout << calculate_pi(1) std::endl;
Hope that helps ,
Andy
Topic archived. No new replies allowed.