/* nag_summary_stats_1var (g01aac) Example Program. * * Copyright 1990 Numerical Algorithms Group. * * Mark 1, 1990. * * Mark 5 revised, 1998. * Mark 8 revised, 2004. * */ #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, j, n, nprob, nvalid, weight; NagError fail; double wsum, *wt = 0, *x = 0, xkurt, xmax, xmean, xmin, xsd, xskew; INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fprintf(fpout, "nag_summary_stats_1var (g01aac) Example Program Results\n"); fscanf(fpin, "%ld", &nprob); for (j = 1; j <= nprob; j++) { fscanf(fpin, "%ld %ld", &n, &weight); fprintf(fpout, "Problem %5ld\n", j); fprintf(fpout, "Number of cases %ld\n", n); if (n >= 1) { if (!(wt = NAG_ALLOC(n, double)) || !(x = NAG_ALLOC(n, double))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } } else { fprintf(fpout, "Invalid n.\n"); exit_status = 1; return exit_status; } for (i = 0; i < n; i++) fscanf(fpin, "%lf", &x[i]); fprintf(fpout, "Data as input -\n"); for (i = 0; i < n; i++) fprintf(fpout, "%12.1f%c", x[i], (i%5 == 4 || i == n-1)?'\n':' '); if (weight) { fprintf(fpout, "Weights as input -\n"); for (i = 0; i < n; i++) fscanf(fpin, "%lf", &wt[i]); for (i = 0; i < n; i++) fprintf(fpout, "%12.1f%c", wt[i], (i%5 == 4 || i == n-1)?'\n':' '); /* nag_summary_stats_1var (g01aac). * Mean, variance, skewness, kurtosis, etc., one variable, * from raw data */ nag_summary_stats_1var(n, x, wt, &nvalid, &xmean, &xsd, &xskew, &xkurt, &xmin, &xmax, &wsum, &fail); } else /* nag_summary_stats_1var (g01aac), see above. */ nag_summary_stats_1var(n, x, (double *) 0, &nvalid, &xmean, &xsd, &xskew, &xkurt, &xmin, &xmax, &wsum, &fail); if (fail.code == NE_NOERROR) { fprintf(fpout, "\n"); fprintf(fpout, "Successful call of " "nag_summary_stats_1var (g01aac)\n"); fprintf(fpout, "No. of valid cases %5ld\n", nvalid); fprintf(fpout, "Mean %13.1f\n", xmean); fprintf(fpout, "Std devn %13.1f\n", xsd); fprintf(fpout, "Skewness %13.1f\n", xskew); fprintf(fpout, "Kurtosis %13.1f\n", xkurt); fprintf(fpout, "Minimum %13.1f\n", xmin); fprintf(fpout, "Maximum %13.1f\n", xmax); fprintf(fpout, "Sum of weights %13.1f\n", wsum); } else { fprintf(fpout, "Unsuccessful call of " "nag_summary_stats_1var (g01aac)\n"); fprintf(fpout, "%s \n", fail.message); if (fail.code == NE_CASES_ONE) { fprintf(fpout, "No. of valid cases %5ld\n", nvalid); fprintf(fpout, "Mean %13.1f\n", xmean); fprintf(fpout, "Minimum %13.1f\n", xmin); fprintf(fpout, "Maximum %13.1f\n", xmax); fprintf(fpout, "Sum of weights %13.1f\n", wsum); fprintf(fpout, "Std devn and coeffts of skewness\n"); fprintf(fpout, "and kurtosis not defined\n"); exit_status = 2; } else { exit_status = 1; goto END; } } if (wt) { NAG_FREE(wt); wt = 0; } if (x) { NAG_FREE(x); x = 0; } } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (wt) NAG_FREE(wt); if (x) NAG_FREE(x); return exit_status; }