Compute pi using arrays.

Okay, so I have this assignment to do. It's technically easy.. but I'm just not getting it. I understand the concept partially. I don't know however how to put this into code. I've started chipping at this but I'm just lost now. (Especially on the arrays.)

Problem given:
"Compute an approximate value of pi using the following statistical method. If we draw a quarter circle of unit radius inside a unit square, then the ratio of the area of the quarter circle to the square is pi/4. Now, generate a set of points inside and on the unit square which inscribes the quarter circle of unit radius. Then the ratio of the number of points inside and on the quarter circle to the number of points inside and on the unit square approximates pi/4."


Now for input:
"Two, one-dimensional arrays or a single two-dimensional array storing the x and y coordinate values of points inside and on a unit square. The x and y values will be type double."


And output is supposed to an approximate value of pi and it should, according to this problem, be slightly different every time.


The program will be in three pieces separating the main function, header, and the actual functions file. So the following is just the functions file I have written thus far:

(Note: Dimensions of the arrays are supposed to be and meant to be 100,000 by 100,000 in my code. Seems like a lot to me though.Also the r=sqrt(x^2 + y^2) is supposed to determine if the point is within the circle.)
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
#include "functions.h"
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>


void point_test()
{

//This function will determine if r=sqrt(x^2)+(y^2) is in the range of 0 to 1. 
	srand(time(0));

	double x, y, r;

	x = (double)rand()/(double)RAND_MAX;
	y = (double)rand()/(double)RAND_MAX;

	r = std::sqrt((x*x)+(y*y));

	if(r<=1)
		std::cout << r;

}

void array()
{

//This function is to somehow use arrays to compute the approximate value of pi.
	double x, y;
	double a[100000][100000] = {x,y};

	x = (double)rand()/(double)RAND_MAX;
	y = (double)rand()/(double)RAND_MAX;

	double r = a[x][y];

	for(int i=0; i<=2; i++)
		a[i][i]
	std::cout << i;

}


Here however is what I've got to as sort of an alternative code. I don't know exactly which direction to go with this assignment.

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
#include "functions.h"
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>


void array()
{

    double x, y, r, ratio, inside_ratio, outside_ratio, inside_circle, outside_circle;

    srand(time(0));

    x = (double)rand()/(double)RAND_MAX;
    y = (double)rand()/(double)RAND_MAX;

    r = std::sqrt((x*x)+(y*y));

    double a[100000] = {x};
    double b[100000] = {y};

    {
    if(r<=1)
        inside_ratio = inside_circle;

    else
        outside_ratio = outside_circle;
    }

    ratio = (inside_ratio)/(outside_ratio);

    std::cout << ratio;

}


Any help please.

Also, I'm compiling with g++ in a UNIX environment.
Mmm, 100,000 * 100,000 = 10,000,000,000, or 10 billion.

sizeof(double) == 6 on my machine, so you are attempting to allocate 60 billion bytes (60 gigabytes)
of memory. How much RAM do you have?????

Well the program is supposed to store 100,000 x points and 100,000 y points in arrays. Now from there it's supposed to take a random x and a random y and compute an approximation of pi by comparing the amount of points in pi/4 and the total amount of points in the square (that pi/4 sits in). The problem is that I'm still kind of sketchy on this whole concept, and putting this into syntax is even more of a confusion for me. I'm not supposed to display 100,000 times 100,000 points. I'm just supposed to store them.
I think it wants either two single dimensional arrays, x[] and y[], or one two-dimensional array, p[][2] (where p[][0] stores x, and p[][1] stores y).
Topic archived. No new replies allowed.