Cheers for all your help guys, I've now managed to get my programme to successfully count the number of lines in my data file
However, I am not sure whether I am incorrectly using malloc or something as when I come to do the math on the data, which was previously faultless, I now get answers of 0 for everything.
Code:
/* Project B */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main() {
FILE * pinfile;
int i,n;
double *x,*y, SUMx, SUMy, SUMxy, SUMxx;
float a,b,s,p,errora,errorb;
pinfile = fopen("xydata.txt", "r");
if (pinfile==0) {
printf("error opening file\n");
return 0;
}
char linebuffer[4096];
int lines = 0;
while(fgets(linebuffer,4096,pinfile)) lines++;
n=(lines - 2);
x = (double *) malloc ((lines - 2)*sizeof(double));
y = (double *) malloc ((lines - 2)*sizeof(double));
if(x==NULL){
printf("\n Error on malloc\n");
return 1;
}
if(y==NULL){
printf("\n Error on malloc\n");
return 1;
}
SUMx = 0; SUMy = 0; SUMxy = 0; SUMxx = 0;
for (i=0; i<n; i++) {
fscanf (pinfile, "%lf %lf", &x[i], &y[i]);
SUMx = SUMx + (x[i]/exp(2));
SUMy = SUMy + (y[i]/exp(2));
SUMxy = SUMxy + ((x[i]*y[i])/exp(2));
SUMxx = SUMxx + ((x[i]*x[i])/exp(2));
p=(lines - 2)*(1/exp(2));
a=(((SUMy*SUMxx)-(SUMx*SUMxy))/((p*SUMxx)-(SUMx*SUMx)));
b=(((p*SUMxy)-(SUMx*SUMy))/((p*SUMxx)-(SUMx*SUMx)));
s=(((a+(b*x[i])-y[i])*(a+(b*x[i])-y[i])/exp(1)));
errora=sqrt(s/((p*SUMxx)-(SUMx*SUMx)));
errorb=sqrt(p/((p*SUMxx)-(SUMx*SUMx)));
}
printf("n has the value %d \n",n);
printf("S has the value %f \n",s);
printf("The standard deviation of the estimate made of parameter 'A' is %f \n",errora);
printf("Additionally the standard deviation of the estimate made of parameter 'B' is %f \n",errorb);
return 0;
}
I can't see where it is going wrong, and it complies ok, but when executed it returns...
n has the value of 7
S has the value 0
The standard deviation of the estimate made of parameter 'A' is 0
Additionally the standard deviation of the estimate made of parameter 'B' is nan
clearly as it gives a value of n as 7 it is counting the lines correctly, so I don't understand why I no longer get correct numerical values for the other parameters. I have changed my data incase this was some problem with that but alas no improvement!