# Thread: Program to calculate the least squares of a set of data.

1. ## Program to calculate the least squares of a set of data.

Have posted previously with some problems but have made a lot of progress on the task, but i just cant see what is going wrong as for what i can see and what others have thought from a quick look it should work fin but does not so.... if you could have a look through to find the problem it would be much appreciated.

previous task description:"I have been given the assignment to create a program to find the the least squares method of fitting a straight line function to one of two data sets given to me in a txt format. The file has an unknown number of data points. all data points are separated by a tab"

sample file content:
13.079 0.051658 0.77359 0.00052980
14.162 0.051750 0.0097982 0.0019695
15.888 0.054027 0.80159 0.0019485
16.191 0.054512 0.62303 8.9929e-05
17.339 0.055797 0.99587 0.00041386
18.784 0.056146 0.25421 0.0016562
19.967 0.056846 0.60045 0.00038078
20.437 0.058294 0.61663 0.00014638
21.397 0.058011 0.026190 0.00032348
22.998 0.059799 0.26090 0.00092712
23.456 0.059883 0.64564 0.0016402
24.070 0.060772 0.83006 0.0017486
25.109 0.060415 0.80091 0.00093706
26.887 0.061940 0.80918 6.3588e-05
27.527 0.063505 0.86446 0.0012275
28.860 0.064270 0.31924 0.0014887
29.616 0.063735 0.63400 0.0016803
30.430 0.065634 0.64624 0.00025035
31.863 0.067435 0.20427 0.00051878
32.326 0.067050 0.0066200 0.00017732
33.043 0.068377 0.71793 0.0017101

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void freeData(double **data);
double *calcofcoeffs(double **data);

int main()
{
char filename[100];
double **data;
double *result;

printf("Please Enter The Name Of The File You Would Like To Use:\n");
scanf("%99s", filename);

result = calcofcoeffs(data);

printf("poo");
printf("\nA =  %.20lf \n\nB = %.20lf \n\nSigma A = %.20lf \n\nSigma B = %.20lf", result[0], result[1], result[2], result[3]);

freeData (data);
free (result);

return 0;
}

{
FILE *userfile; //declares the file.
double **data; //declares double array.
int i, j; //declares loop variables.
int nrows; //declares a variable that tracks the row number when reading in the file.
int ncolumns; //declares and initialises the number of columns of data and means the following loop starts at the correct point.

//allocating memory using malloc for the array to store the data from the file.
data = (double**)malloc(10000*sizeof(double*));
for(i=0; i<10000; i++)
{
data[i] = (double*)malloc(4*sizeof(double));
}

userfile = fopen(filename,"r");
//initialising all the array elements to zero

for(i=0; i<10000; i++)
{
for(j=0; j<4; j++)
{
data[i][j] = 0;
}
}
if (userfile==NULL) //check to see if file was found.
{
return data;
}

nrows=0;
ncolumns=4;

while (ncolumns==4 && nrows<10000);
{
ncolumns = fscanf(userfile, "%lf %lf %lf %lf\n", &data[nrows][0], &data[nrows][1], &data[nrows][2], &data[nrows][3]);
nrows++;
}

fclose(userfile);

return data;
}

void freeData(double **data)
{
int i;
for (i=0;  i<10000; i++)
{
free(data[i]);
}

free (data);

return;
}

double *calcofcoeffs(double **data)
{
//declaring and initialising the variables used for summations
int i;
double x;
double y;
double xy;
double xx;
double one;
double sigmai2;

//declaring variables to be given as final results.
double A;
double B;
double sigmaA;
double sigmaB;

//declaring a pointer to be used in int main to display final results.
double *result;
result = (double*)malloc(4*sizeof(double));

i=0;
x=0;
y=0;
xy=0;
xx=0;
one=0;

//a while loop that reads through the data to calculate the summations
while(i<10000 && data[i][3]!=0)
{

sigmai2 = data[i][3]*data[i][3];//making a more simple shorthand for calculation

// summations used in working out coeffs A and B.
y+=(data[i][1]/sigmai2);
xx+=((data[i][0]*data[i][0])/sigmai2);
x+=(data[i][0]/sigmai2);
xy+=((data[i][0]*data[i][1])/sigmai2);
one+=(1/sigmai2);

i++;
}
//working out coeffs A and B.
A = ((y*xx)-(x*xy))/((one*xx)-(x*x));
B = ((one*xy)-(x*y))/((one*xx)-(x*x));

//working out errors in coeffs.
sigmaA = sqrt((xx)/((one*xx)-(x*x)));
sigmaB = sqrt((one)/((one*xx)-(x*x)));

//output the values required.
result[0] = A;
result[1] = B;
result[2] = sigmaA;
result[3] = sigmaB;

return result;
}```

Thanks for the help

2. Bugs in code, are not going away, so you have to learn to debug, as well.

Take these initial steps:

Before the program runs:

1) Do you have any compiler warnings, or errors? If so, (and treat all warnings as if they were error, at this point), what are they, and at what line number/s?

During the program run:

1) Use simple input that fails (shows the problem), as your testing input sample.

2) print out the input your program has, before it makes any calculations.

Does it get all the input from the file, and store it correctly?

3) After input is checked, check the calculations on it. Are they correct? Has each calculation checked out OK?

Which ones are NOT correct, if any?

4) Check the final output. Again, which output of the calculations are not correct, now? If you have the right calculations, but you print it out with the wrong format, it may show the wrong values. Compare this output, with the intermediate calculation results from step #3.

I'm reluctant to debug your program, when you appear to have made no effort yourself -- or at least, none that you are reporting. You've just left it for others to do, for you.

Debugging a program is a skill you MUST learn, and you MUST practice to be any good at it. I'm glad to help, but I'm not glad to do it all for you.

Take the above steps, and report back, please.