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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <math.h>

#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<MAXVALS ; i++)
  {
    fscanf(data, "%f %f %f\n" , &gas[i].V, &gas[i].T, &gas[i].P);
    gas[i].logV = log10(gas[i].V);
    gas[i].logT = log10(gas[i].T);
  }
  n = i;
  printf("%d data points\n\n",n);
    
  printf("Volume \t %f \nPressure %f \nTemp \t %f\n\n", gas[2].V, gas[2].T, gas[2].P);
    
  regression(n, gas, &gradient, &intercept);
    
  printf("Gradient = %f \nIntercept = %f \n\n", gradient, intercept);

  gamma = 1 - gradient;
  
  printf("Adiabatic index = %f\n\n", gamma);
    
  /* Return success message */
  printf("Success!\n\n");
  fclose(data); 
  system("pause");   
  return 1;
}


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.