We estimate pi.
We assume as in Figure 10 is a side of the square.
The first type, which defines a function IsInCircle
Quadrant is the point or not.
intIsInCircle (floatx, floaty);
The number of random points that are inside the square, the
Number of points within the quadrant to the total number of points in your account.
This ratio is equal to one-fourth of pi.
Find a way to write the function to determine the number of
Must be at least several hundred random locations to get an error.
IntFind ();
The number of an incoming call and read the number on the Find function and
Average output as the final print.
why isn't this code work??/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int lslnCircle(float x,float y);/*function portotype*/
int Find();
int main(){
int n,x,sum=0,k;
scanf("%d",&n);/*read an intefer*/
for(k=1;k<=n;k++){
x=Find();
sum+=x;
}/*end for*/
float average;
average=sum/n;
printf("%.2f",average);
return 0;
}/*end main*/
int Find(){
int i,j=1,counter=0;
float a,b;
float c=0,p=3.14;
while( (float)p-c>0.01||(float)c-p>0.01){
for(i=1;i<=j;i++){
a=(rand()%10000+1)/1000;
b=(rand()%10000+1)/1000;
if(lslnCircle(a,b)){
++counter;
}/*end if*/
}/*end for*/
c=counter*4/j;
++j;
}
return j;
}
int lslnCircle(float x,float y){
if(pow(x,2)+pow(y,2)<100){
return 1;
}/*end if*/
else{
return 0;
}/*end else*/
}
×