jiaying
09-14-2009, 01: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;
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;
}
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;
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;
}