Enjoy an ad free experience by logging in. Not a member yet? Register.


Results 1 to 2 of 2
Thread: Estimated Circle Area

09142009, 01:56 PM #1
 Join Date
 Sep 2009
 Posts
 1
 Thanks
 0
 Thanked 0 Times in 0 Posts
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; double radius=0.0, estArea, actualArea; printf("Please enter the radius of the circle: "); scanf("%lf", &radius); printf("Please enter the number of points to generate: "); scanf("%d", &points); // Actual area of circle actualArea=PI*radius*radius; for (i=0; i<points; i++) { x=RAND(x); y=RAND(y); if(x*x+y*y<4) { count++; } } estArea=(count/points)*((2*radius)*(2*radius)); // 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; }
09142009, 06:17 PM
#2
 Join Date
 Mar 2009
 Location
 Munich, Germany
 Posts
 139
 Thanks
 1
 Thanked 13 Times in 13 Posts
This is where you check if your coords are in the circle:
Whats the '4' doing in your condition?Code:if(x*x+y*y<4) { count++; }
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((rx)^2 + (ry)^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.
Last edited by BrickInTheWall; 09142009 at 06:23 PM.