0

#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#define LIMIT 100000000 //max value of n void main(){ double mypi,x,y ; int minus ; // 1 for pos., 2 for neg.
long int m,n;
time_t t;
n = 0; m = 0;
srand((unsigned) time(&t));
clrscr();
do{
n++; x = (rand()/(double)RAND_MAX); //pick a random X between 0..1
minus = 1+(rand()/(double)RAND_MAX);//pick a sign for X
switch (minus) {
case1 : x = x; case2 : x = -1*x; }y = rand()/(double)RAND_MAX;//pick a random Y between 0..1
minus = 1+rand()/(double)RAND_MAX;//pick a sign for Y
switch (minus) {
case1 : x = x; case2 : x = -1*x; }if (pow(x,2)+pow(y,2) < 1) m++; //the point is inside the unit circle mypi = 4.0*m/n; //calculate pi if (n % (LIMIT / 1000) == 0) {
gotoxy(1,1);
printf("X:%f ",x);
printf("Y:%f ",y);
printf("n:%ld ",n);
printf("m:%ld ",m);
printf(" - ");
printf("PI:%f ",mypi);
}
}
while (n != LIMIT); getch();

}

2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by Captonmike
1

Is it showing a compiler error? post the errors
Is it showing unwanted output? post the output
Do you know how to properly format your code and wrap it it in code tags before you post? If yes then do so

Edited by zeroliken: n/a

0

Is it showing a compiler error? post the errors
Is it showing unwanted output? post the output
Do you know how to properly format your code and wrap it it in code tags before you post? If yes then do so

thank you very much for your reminding, I really have not wrap all of code before post. It disappear about 1 or 2 lines. Now I can solve it .
^^

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.