Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Estimated Circle Area

Hi, I was given a normal lab question and I have difficulty in understand and doing it. The question is:

The questions are: (Any idea how to show?)

And lastly I was given the sample codes to start off.

This is my codes. However I cant seems to get the correct results.
Any ideas on how to improve it?
Thanks!
Code:
```#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;
}```

• This is where you check if your coords are in the circle:
Code:
```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.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•