ВУЗ:
Составители:
Рубрика:
- 44 -
int main(int argc, char *argv[])
{
double pi, /* average of pi after ‘darts’ is thrown */
avepi, /* average pi value for all iterations */
t1, t2, // time’s moments
pi_prec=4.0*(atanl(1.0)-atan(0.0)); // pi precision
int i, n;
t1=f_time(); // fix start time calculated
srandom (5); // init of random generator
avepi = 0;
for (i=0; i<ROUNDS; i++) // rounds times call dboard function
{
pi = dboard(DARTS);
avepi = ((avepi * i) + pi)/(i + 1);
t2=f_time(); // fix end time calculated
printf("%7d throws aver.value of PI= %.12lf (rel.error= %.5lf %%, time= %.3lf sec)\n",
(DARTS * (i+1)), avepi, 1.0e2*(pi_prec-avepi)/pi_prec, t2-t1);
} // end for (i=0; i<ROUNDS; i++)
} // end of MAIN function
// end of PI_SER.C program
Текст функции DBOARD (подключается посредством #include “dboard.c”):
double dboard(int darts)
{
double x_coord, /* x coordinate, between -1 and 1 */
y_coord, /* y coordinate, between -1 and 1 */
pi, /* pi */
r; /* random number between 0 and 1 */
int score, /* number of darts that hit circle */
n;
unsigned long cconst;
cconst = 2 << (31 - 1); /* used to convert integer random number
between 0 and 2^31 to double random number between 0 and 1 */
score = 0; // start summ
for (n=1; n<=darts; n++) // cicles at random 2D-points
{
r = (double)random() / cconst;
x_coord = (2.0 * r) - 1.0; // x-coord of 2D-point
r = (double)random() / cconst;
y_coord = (2.0 * r) - 1.0; // y-coord of 2D-point
if (((x_coord*x_coord) + (y_coord*y_coord)) <= 1.0) // 2D-point in circle?
score++;
} // end for (n=1; n<=darts; n++)
pi = 4.0 * (double)score / (double)darts;
return(pi);
} // end of dboard function
int main(int argc, char *argv[]) { double pi, /* average of pi after ‘darts’ is thrown */ avepi, /* average pi value for all iterations */ t1, t2, // time’s moments pi_prec=4.0*(atanl(1.0)-atan(0.0)); // pi precision int i, n; t1=f_time(); // fix start time calculated srandom (5); // init of random generator avepi = 0; for (i=0; i
Страницы
- « первая
- ‹ предыдущая
- …
- 42
- 43
- 44
- 45
- 46
- …
- следующая ›
- последняя »