 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??/

int lslnCircle(float x,float y);/*function portotype*/
int Find();
int main(){
    int n,x,sum=0,k;
    scanf("%d",&n);/*read an intefer*/
    }/*end for*/
    float 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){



}/*end if*/
}/*end for*/

return j;
int lslnCircle(float x,float y){
        return 1;
    }/*end if*/
        return 0;
    }/*end else*/

Recommended Answers

All 7 Replies

Put in a few test printf() and find out where it gets stuck.

why isn't this code work??/

You need to explain a bit more. Do you take your car to a machanic and just say "it doesn't work". Of course not, you tell him the symptoms, such as it doesn't start. Programming is a bit like that -- we can't tell you why "it doesn't work" unless we know what's wrong with it.

  • Always use the double type not the float type unless you have a comunicable reason for needing to use float (like it's an embedded platformwithout much memory or the question you are answering specifies use of float).
  • Line 9 for(k=1;k<=n;k++) is poor form, you k is an unsigned type and n happens to be the max value for that type then what you have is an infinite loop. The more normal form to get n iterations is for(k=0;k<n;k++)
  • Line 22 while( (float)p-c>0.01||(float)c-p>0.01) no need to cast c or p to float, they are already floats also you are using math.h so make use of its functions while( fabs(p-c) > 0.01) (fabs returns the absolute value).
  • Line24, similar to 9, for(i=1;i<=j;i++) is poor form. The more normal form to get n iterations is for(i=0;i<j;i++)
  • Line 26 and 27 a=(rand()%10000+1)/1000; doesn't do what you think because it uses integer division. This means you get an integer result between 0 and 10 but I suspect you want a floating point result between 0.001 and 10.000. Since all the types in the formula are integers the compiler does the calculation as integers meaning the /1000 just chops off all the decimal places, if you want it to retain the decimal places you need to force the compiler to use floating point maths. The easiest way is to use floating point constants a=(rand()%10000+1.0F)/1000.0F;. Similarly for line 27. This is the problem as you squew the ration of points in a square to points in a quadrant of a circle from 0.785398(PI/4) to 0.570248 when you only consider integer points.

Oh yeah and finally trying out your formula in Excel suggests this isn't a terribly good method to approximate PI. Using 10500+ random points the result of the formula when run repeatedly was 3.1x where x veried a lot (but was sometimes 4) so evern after correction you program might take a good while to run to completion

commented: Great. +15

stil this code dose not print any output!!

Please post revised code that corrrects problems mentioned previously.

after changeing what Banfa said


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.