/* nag_median_1var (g07dac) Example Program. * * Copyright 1990 Numerical Algorithms Group. * * Mark 1, 1990. * * Mark 3 revised, 1994. * Mark 8 revised, 2004. */ #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, n; NagError fail; double *x = 0, xmd, xme, xsd, *y = 0; 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); fprintf(fpout, "nag_median_1var (g07dac) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld", &n); if (n > 1) { if (!(x = NAG_ALLOC(n, double)) || !(y = 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]); /* nag_median_1var (g07dac). * Robust estimation, median, median absolute deviation, * robust standard deviation */ nag_median_1var(n, x, y, &xme, &xmd, &xsd, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_median_1var (g07dac).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "Output y:\n"); for (i = 0; i < n; ++i) fprintf(fpout, "%6.3f%s", y[i], (i%11 == 10 || i == n-1)?"\n":" "); fprintf(fpout, "\nxme = %6.3f, xmd = %6.3f, xsd = %6.3f\n", xme, xmd, xsd); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (x) NAG_FREE(x); if (y) NAG_FREE(y); return exit_status; }