- #1
jam12
- 38
- 0
Hi guys, i am using the C language and have created the following function:
void counter (double c) {
FILE *fp;
char output[]="output.xls";
int n,p=0;
int r=0;
int i,j;
float x=0;
fp=fopen(output,"w");
fprintf(fp,"rmax\tnumber of particles within rmax\r\r\n");
for(n=1;n<=10;n++) {
x=pow(c,n/10);
r=ceilf(x); /*here i need to convert the float x into integer r, since in the for statements below, r must be an integer */
for(i=-r;i<=r;i++) {
for(j=-r;j<=r;j++) {
if(grid[N/2+i][N/2+j]) {
if(sqrt(i*i+j*j)<=r) p++;
}
}
}
fprintf(fp,"%f\t%d\r\n",r, p);
p=0;
}
fclose(fp);
}
Basically as the comment in the code above says...
I need to convert my r value to be of type int that takes a value of the integer part of float x.
I thought the ceil function would do the trick, but it doesnt, my file just prints all zeros for r.
Any help would be appreciated thanks.
void counter (double c) {
FILE *fp;
char output[]="output.xls";
int n,p=0;
int r=0;
int i,j;
float x=0;
fp=fopen(output,"w");
fprintf(fp,"rmax\tnumber of particles within rmax\r\r\n");
for(n=1;n<=10;n++) {
x=pow(c,n/10);
r=ceilf(x); /*here i need to convert the float x into integer r, since in the for statements below, r must be an integer */
for(i=-r;i<=r;i++) {
for(j=-r;j<=r;j++) {
if(grid[N/2+i][N/2+j]) {
if(sqrt(i*i+j*j)<=r) p++;
}
}
}
fprintf(fp,"%f\t%d\r\n",r, p);
p=0;
}
fclose(fp);
}
Basically as the comment in the code above says...
I need to convert my r value to be of type int that takes a value of the integer part of float x.
I thought the ceil function would do the trick, but it doesnt, my file just prints all zeros for r.
Any help would be appreciated thanks.