3

I'm trying to write a Quasi- Monte Carlo Approximation of pi in C. I am not well versed in it yet and am trying to translate my python based skills, so I may be simply overlooking something. I keep getting 0 as a result and I can't figure out why. How should I fix this? Also, I get an error on the last two printf calls saying they are type double * and not double. It compiles anyways, is this related?

#include <stdio.h>
/*  
Tristen Wentling
montepithon.c
October 31, 2013

*/  
int main(void)
{   
    float i,j,x;
    float count=0,counter=0;
    printf("Please enter the desired grid division size (n=?)");
    scanf("%f", &x);
    float y=x*x;
    if(x==0){
        printf("goodbye");
    }   
    else if(x!=0){
        for(i=0;i<=x;i++){
            for(j=0;j<=x;j++){
                float check=((i*i)*(1/y))+((j*j)*(1/y));
                /*printf("%f\n", check);*/
                if(check<=1){
                    count+=1;
                }   
                else{
                    counter+=1;
                }   
            }   
        }   
    }   
    else{
        printf("error");
    }   
    float prsum=count/y;
    float ptsum=(1-counter)*(1/y);
    double pirprox=4*prsum;
    double pitprox=4*ptsum;
    printf("%f\n", &pirprox);
    printf("%f\n", &pitprox);
    getchar();
}   
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Tristen
  • 57
  • 1
  • 1
  • 7
  • Warnings in C are the same as errors. Don't ignore them. In this case the compiler is telling you that you pass the address of a variable instead of the variable itself. Please follow its advice. Otherwise you will not see the result of your code. Also: (1) SO is not a site for code review, (2) if you post code (when you have a concrete technical question) please indent it properly. – Jens Gustedt Nov 01 '13 at 21:20
  • Sorry about that. I'm usually on ask ubuntu and mathematics. I thought here was the best fit by description. Where is this type of question more appropriately asked? – Tristen Nov 01 '13 at 22:42
  • You realize that division is allowed, right? Each occurrence of `*(1/y)` in your program would be faster and more accurate (and shorter) if it was written `/y`. – Pascal Cuoq Dec 25 '13 at 00:50
  • If you are trying to make it “efficient”, compare `i*i + j*j` to `y` instead of comparing the ratio to `1`. You won't even need floating-point. – Pascal Cuoq Dec 25 '13 at 00:54

1 Answers1

6

%f format specifier in printf expects double type argument. &pirprox and &pitprox is of type double * and you cannot print an address with %f. Wrong format specifier would invoke undefined behavior.
Change your code snippet

 printf("%f\n", &pirprox);

 printf("%f\n", &pitprox);  

to

 printf("%f\n", pirprox);

 printf("%f\n", pitprox);  
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 2
    It works perfectly after the fix, thank you for pointing out what I was overlooking. – Tristen Nov 01 '13 at 22:43
  • `return 0;` can be omitted in `main()` since C99. – Pascal Cuoq Dec 25 '13 at 00:57
  • @PascalCuoq; Yes you can. But adding it to `main` is not bad at all. Take a look at the C11 specification: **5.1.2.2.3:** `If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;11) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.` – haccks Dec 25 '13 at 01:14