我正在尝试用C编写一个Quasi- Monte Carlo逼近pi.我还不太熟悉它并试图翻译我的基于python的技能,所以我可能只是忽略了一些东西.结果我一直得0,我无法弄明白为什么.我该怎么解决这个问题?另外,我在最后两次printf调用时收到错误,说他们是类型double *而不是double.无论如何它编译,这是相关的吗?
#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();
}
Run Code Online (Sandbox Code Playgroud)