...

# Estimated Circle Area

jiaying
09-14-2009, 02:56 PM
Hi, I was given a normal lab question and I have difficulty in understand and doing it. The question is:
http://i31.tinypic.com/zsvins.jpg
http://i30.tinypic.com/2eydi83.jpg

The questions are: (Any idea how to show?)
http://i27.tinypic.com/2chrs49.jpg

And lastly I was given the sample codes to start off.
http://i30.tinypic.com/rwhohk.jpg

This is my codes. However I cant seems to get the correct results.
Any ideas on how to improve it?
Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define RAND(X) ((double) rand()/RAND_MAX * X)
#define PI 3.141593

int main()
{
int x=0, y=0, points=0, i, count=0;

printf("Please enter the number of points to generate: ");
scanf("%d", &points);

// Actual area of circle

for (i=0; i<points; i++)
{
x=RAND(x);
y=RAND(y);
if(x*x+y*y<4)
{
count++;
}

}

// Output
printf("\n\nAfter %d simulations, the area of circle is estimated to be %.2f \n", points, estArea);
printf("Actual area of circle is %.2f\n", actualArea);

return 0;
}

BrickInTheWall
09-14-2009, 07:17 PM
This is where you check if your coords are in the circle:

if(x*x+y*y<4)
{
count++;
}

Whats the '4' doing in your condition?
If I were to check the distance between the center (r, r) and some coordinate (x, y), I would just calculate the connecting vector, then its magnitude, and compare that to r.

Example: |(r - x, r - y)| < r? where |(r - x, r - y)| = sqrt((r-x)^2 + (r-y)^2);

edit: You might aswell leave the square root out and compare it to the square of the radius to make things a little cleaner.