Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159
#define RADIUS 10
double RandNumInRange (double, double);
int DartHits(double, double);
double ThrowDarts(unsigned long *, double *, double *, double *, double *);
void analysis(double,unsigned long,double,double,double,double,double,
double,double,double,unsigned long);
int main (void) {
int seed;
unsigned long N; //number of darts to be thrown
double estRectArea;
double lowx, lowy, hix, hiy;
printf("Enter the integer for the seed: ");
scanf("%d",&seed);
srand(seed);
estRectArea = ThrowDarts(&N,&lowx,&lowy,&hix,&hiy);
printf("\n");
return 0;
}
void GetBorders(double *xmin, double *xmax, double *ymin, double *ymax) {
double a, b, c, d;
a = 27;
b = 58;
c = 3;
d = 30;
*xmin = a;
*xmax = b;
*ymin = c;
*ymax = d;
}
double RandNumInRange (double z, double w) { //where w is any max value, and z is any min value
double rn;
rn = ((double)rand()/(double)RAND_MAX)*(w-z);
return rn;
}
int DartHits(double x, double y) {
double distance = sqrt(x*x + y*y);
if (distance <= RADIUS) {
return 1;
}
else {
return 0;
}
}
double ThrowDarts(unsigned long *N,double *lowx,double *lowy,double *hix,double *hiy) {
printf("Enter the number of darts to be thrown in this simulation: ");
scanf("%ul",N);
double xmax, xmin, ymax, ymin;
GetBorders(&xmin, &xmax, &ymin, &ymax);
printf("\nCheck: xmax: %f, xmin: %f, ymax: %f, ymin: %f\n\n",xmax,xmin,ymax,ymin); getchar();
double rectArea = (xmax -xmin) * (ymax - ymin);
double estRectArea = 0.0;
double xdart;
double ydart;
int i = 1;
unsigned long nhit = 0; //hits can't be anything but integers
//loop goes here:
*lowx=*lowy=1000;
*hix=*hiy=0;
//printf("
while (i <= *N) {
//generate random number between xmin and xmax
xdart = RandNumInRange(xmin,xmax);
printf("xdart: %f\n",xdart); getchar();
if(xdart<*lowx)
*lowx=xdart;
if(xdart>*hix)
*hix=xdart;
//generate random number between ymin and ymax
ydart = RandNumInRange(ymin,ymax);
printf("ydart: %f\n",ydart); getchar();
if(ydart<*lowy)
*lowy=ydart;
if(ydart>*hiy)
*hiy=ydart;
if(DartHits(xdart,ydart)) {
nhit++;
}
++i;
}
printf("lowx: %f, lowy: %f, hix: %f, hiy: %f\n\n",*lowx,*lowy,*hix,*hiy); getchar();
estRectArea = rectArea * (double) nhit/ *N;
analysis(estRectArea, *N,xmin,xmax,ymin,ymax,*lowx,*lowy,*hix,*hiy,nhit);
return estRectArea; //no reason for this return, imo
}
void analysis(double estRectArea, unsigned long N, double xmin,double xmax,double ymin,double ymax,
double lowx,double lowy,double hix, double hiy, unsigned long nhit) {
double percentError = 0.0;
double error = 0.0;
double actualArea = (ymax-ymin) * (xmax-xmin);
printf("Rectange has length of %.2f and a width of %.2f\n\n",ymax-ymin,xmax-xmin);
printf("Area of the rectangle is: %.2f\n\n",actualArea);
printf("Darts thrown: %lu. Hits: %lu, hit percentage: %.2f %%\n\n",N,nhit,(double)nhit/N*100);
error = abs(actualArea - estRectArea);
printf("Estimated Rectangular Area: %.2f Error in Area: %.2f\n\n",estRectArea,error);
percentError = (error/actualArea) * 100;
printf("Percent error was: %.2f %%\n",percentError);
}
I'm going to hunt down that ydart problem, asap. Just found out about it a few minutes ago.