[ create a new paste ] login | about

Link: http://codepad.org/RStNqGJv    [ raw code | output | fork ]

C++, pasted on Nov 10:
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;

bool IsPointInCircle(double R, double x, double y);

int main()
{
	int Npoints = (int)1E3;
	int Nincircle = 0;
	srand(time(0));
	for(int i = 0; i < Npoints; i++)
	{
		if
		(
			IsPointInCircle
			(
				1.0, 
				(rand()%(4*Npoints) - 2*Npoints)/(2.0*Npoints), 
				(rand()%(4*Npoints) - 2*Npoints)/(2.0*Npoints)
			)
		)
			Nincircle++;
	}
	cout<<"Calc  Value of PI : "<<setprecision(5)<<Nincircle*4.0/Npoints<<endl;
	cout<<"Check Value of PI : "<<setprecision(5)<<asin(1.0)*2.0<<endl;
	cin.get();
	return 0;
}

bool IsPointInCircle(double R, double x, double y)
{
	return ((x*x + y*y) < R*R);
}


Output:
1
2
Calc  Value of PI : 3.184
Check Value of PI : 3.1416


Create a new paste based on this one


Comments: