#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
int main()
{
double x, y, r, ratio, points_in_circle, outside_points, total_points;
srand(time(0));
double arr1[100000] = {0};
double arr2[100000] = {0};
constint NUMBERS = 99999;
for(int i=0;i<=NUMBERS;i++){
x = (double)rand()/(double)RAND_MAX;
y = (double)rand()/(double)RAND_MAX;
arr1[i] = x;
arr2[i] = y;
r = std::sqrt((x*x)+(y*y));
{
if(r<=1)
r = points_in_circle;
++points_in_circle;
}
{
if(r>1)
r = outside_points;
outside_points;
}
}
total_points = outside_points + points_in_circle;
ratio = (points_in_circle)/(total_points);
std::cout << ratio;
return 0;
}
Basically, what my assignment is, is to approximate pi within a quarter circle, (pi/4). I am to do so by having two arrays. The arrays have dimensions of 100,000 by 100,000. I populate them with random x and y points. I find out whether the points are inside the quarter circle or not, by this equation:r=sqrt(x^2 + y^2), and if the answer is between 0 and 1, then the point is within the circle. Otherwise if it is greater than 1, then it is the outside point. I have to basically make a ratio of inside points to total points (inside + outside points).
Right now, I keep getting '1' as my output. I don't see why this is.
Can anyone please point out what I'm doing wrong? I'm working in UNIX and compiling with g ++.
Points inside divided by total points will never give more than 1 (when all points are inside the circle). It could never give more than one because that would imply that there are more points inside the circle than there are total points. It would be absurd.
Try dividing points inside by points outside.