Compute Pi using arrays (code finished, but not working right)

I posted a thread related to this earlier but after redoing the code over and over I decided to start a fresh one.

So here is what I have:
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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};
	const int 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 ++.
New update, however I know keep getting .432193, .43545 etc.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>

int main()
{
	double x, y, r, ratio, points_in_circle=0, outside_points=0, total_points=0;

	srand(time(0));
	
	double arr1[100000] = {0};
	double arr2[100000] = {0};
	const int NUMBER = 99999;	

	for(int i=0;i<=NUMBER;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 = (double)(points_in_circle)/(double)(total_points);
	
	std::cout << "The approximation of pi is: " <<  ratio << std::endl;

	return 0;

}
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.
It worked!! Thank-you so much!!! :D :D :) I've been working on this a week straight! ahh!
Last edited on
Topic archived. No new replies allowed.