### Passing structure elements to a function.

I am trying to work out the best way to use a function that performs linear regression calculations for a set of x and y data values.

I have included my code below. The function works fine, however it only works for the specific elements in the gas structure, I need to use the function for other elements or even another structure type.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` ``````#include #include #define MAXVALS 200 struct gas { float V, T, P, logV, logT; }; void regression(int n, struct gas *gas, float *gradient, float *intercept) { float sumx=0.0, sumy=0.0, sumxx=0.0, sumxy=0.0, del; struct gas *pgas; /* Calculate sums */ for ( pgas=gas ; pgas<&gas[n] ; pgas++ ) { sumx += pgas->logV; sumy += pgas->logT; sumxx += pow( pgas->logV , 2 ); sumxy += pgas->logV * pgas->logT; } /* Use sums to calculate the outputs */ del = n * sumxx - pow(sumx,2); *gradient = (n * sumxy - sumx * sumy) / del; *intercept = (sumxx * sumy - sumx * sumxy) / del; } int main() { struct gas gas[MAXVALS]; int i, n; float gradient, intercept; double gamma; char validdata, filename[100]; FILE *data; /* Choose data file to open */ while( validdata==0 ) { printf("Enter input filename: "); scanf("%s",filename); data = fopen(filename,"r"); /* Return an error if the file cannot be opened */ if ( data==NULL ) { printf("Cannot open data file\n\n"); fclose(data); } else { printf("\nFile opened\n\n"); validdata=1; } } /* Skip first line of data file */ fseek( data , 24 , SEEK_SET ); /* Calculate the log of each value of T and V */ for (i=0 ; ! feof(data) && i

I can't work out how to pass individual elements of the structure to the regression function, rather than the whole structure, or if that's the correct method at all.

Sorry if any of that was unclear.

Thank you.
There are various convoluted ways of doing what you are asking, but I am not sure I understand why you want to put yourself through the grief.

What is wrong with your gas structure? Why not use it exclusively?

If there is a need to communicate with other structure types, just write yourself a couple of conversion functions to transfer the data to and from the gas type.

Hope this helps.
Thanks, you're right it does make more sense to use a couple of conversion functions instead. That's working fine for me.
Topic archived. No new replies allowed.