/* nag_order_data (g10zac) Example Program. * * Copyright 2000 Numerical Algorithms Group. * * Mark 6, 2000. */ #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; Integer exit_status = 0, i, *iwrk = 0, n, nord; NagError fail; char weight[2]; double rss, *weights = 0, *wtord = 0, *wtptr, *x = 0, *xord = 0, *y = 0; double *yord = 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_order_data (g10zac) Example Program Results\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n]"); fscanf(fpin, "%ld", &n); if (!(x = NAG_ALLOC(n, double)) || !(y = NAG_ALLOC(n, double)) || !(weights = NAG_ALLOC(n, double)) || !(xord = NAG_ALLOC(n, double)) || !(yord = NAG_ALLOC(n, double)) || !(wtord = NAG_ALLOC(n, double)) || !(iwrk = NAG_ALLOC(n, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } fscanf(fpin, " %s ", weight); for (i = 1; i <= n; ++i) fscanf(fpin, "%lf %lf", &x[i - 1], &y[i - 1]); if (*weight == 'W') wtptr = weights; else wtptr = 0; /* nag_order_data (g10zac). * Reorder data to give ordered distinct observations */ nag_order_data(n, x, y, wtptr, &nord, xord, yord, wtord, &rss, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_order_data (g10zac).\n%s\n", fail.message); exit_status = 1; goto END; } /* Print results */ fprintf(fpout, "\n"); fprintf(fpout, "%s%6ld\n", "Number of distinct observations = ", nord); fprintf(fpout, "%s%13.5f\n", "Residual sum of squares = ", rss); fprintf(fpout, "\n"); fprintf(fpout, " X Y WEIGHTS\n"); for (i = 1; i <= nord; ++i) fprintf(fpout, " %13.5f %13.5f %13.5f\n", xord[i - 1], yord[i - 1], wtord[i - 1]); END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (x) NAG_FREE(x); if (y) NAG_FREE(y); if (weights) NAG_FREE(weights); if (xord) NAG_FREE(xord); if (yord) NAG_FREE(yord); if (wtord) NAG_FREE(wtord); if (iwrk) NAG_FREE(iwrk); return exit_status; }