#include #include #include #define NPERIOD 50 /* maximum number of period values */ #define NPHASE 20 /* maximum number of phase values */ struct dpoint { /* define set of period, phase, and chi2 */ double period; double phase; double chi2; }; void FGS01(struct dpoint *spointset); void FGS02(struct dpoint *spointset, int n, double *sumval); int main() { FILE *fp; char *FGS01f = "fgs01_snippet.dat"; /* contains output data */ int i,j,k; double pi, sumval; struct dpoint pointset[NPERIOD*NPHASE]; pi = 2*asin(1.0); /* Write out parameters to file */ if ((fp = fopen(FGS01f,"w")) == NULL) { printf("Could not open file for writing: %s - ",FGS01f); abort(); } fprintf(fp,"# \n# FGS Sample Parameters (period, phase, and chi2) \n# \n"); /* Calculate chi2 values by passing structure to function */ for (i = 0; i < NPERIOD; i++) { for (j = 0; j < NPHASE; j++) { k = i*NPHASE + j; pointset[k].period = i; pointset[k].phase = (2*pi) * (double)j / (double)NPHASE; FGS01(&pointset[k]); fprintf(fp,"%4d %4d %8d %10.4f %10.4f %10.4f \n", i, j, k, pointset[k].period, pointset[k].phase, pointset[k].chi2); } } /* Calculate summed function by accessing entire array of structures within function */ FGS02(&pointset[0], NPERIOD*NPHASE, &sumval); fprintf(fp,"# \n%12.4f\n# \n", sumval); fclose(fp); return 0; } /* FGS01: function to show array of structures input into a function */ void FGS01(struct dpoint *spointset) { /* Fill chi2 values based on period and phase values */ spointset->chi2 = spointset->period * spointset->phase; return; } /* FGS02: function to show array of structures input into a function */ void FGS02(struct dpoint *spointset, int n, double *sumval) { int i; /* Calculate sum of the square of the chi-squared component of the structure */ *sumval = 0.0; for (i = 0; i < n; i++) { *sumval += spointset->chi2 * spointset->chi2; spointset++; } return; }