Mac Osxtopus
Registered
YArggh!! stupid thing!
We have to make a program that aproximates pi to...the 100th digit?
I tried using the monte carlo method, but i'm stuck after this, have a look at the code:
#include iostream.h // forum doesn't like alligators, so any blanks
#include math.h // are just made by the forum.
#include stdlib.h
main()
{
double x= rand(); // the x, y co-ordinates of where it is in the square.
double y = rand();
float dist = sqrt(x*x + y*y);// the distance, to calculate the hypotenuse.
long double n; // ...counter or something?
long double N = 100000;
int other=0; // had put it in to try to stop main() from ending.
if(x < 0 || x > 1) { // ... x and y have to be between 1 and 0, including 1 and 0, of course
x = rand();
dist = sqrt(x*x + y*y);
}
if(y > 1 || y < 0 ) {
y = rand();
dist = sqrt(x*x + y*y);
}
if(dist <= 1) {
for(n=0;n<N;n++) {
cout << n << '\n';
}
}
if(50==other) {
return 0;
}
}
We have to make a program that aproximates pi to...the 100th digit?
I tried using the monte carlo method, but i'm stuck after this, have a look at the code:
#include iostream.h // forum doesn't like alligators, so any blanks
#include math.h // are just made by the forum.
#include stdlib.h
main()
{
double x= rand(); // the x, y co-ordinates of where it is in the square.
double y = rand();
float dist = sqrt(x*x + y*y);// the distance, to calculate the hypotenuse.
long double n; // ...counter or something?
long double N = 100000;
int other=0; // had put it in to try to stop main() from ending.
if(x < 0 || x > 1) { // ... x and y have to be between 1 and 0, including 1 and 0, of course
x = rand();
dist = sqrt(x*x + y*y);
}
if(y > 1 || y < 0 ) {
y = rand();
dist = sqrt(x*x + y*y);
}
if(dist <= 1) {
for(n=0;n<N;n++) {
cout << n << '\n';
}
}
if(50==other) {
return 0;
}
}