/* nag_1d_quad_vals (d01gac) Example Program. * * Copyright 1991 Numerical Algorithms Group. * * Mark 2, 1991. * 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 ans, error, *x = 0, *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_1d_quad_vals (d01gac) Example Program Results\n"); fscanf(fpin, "%*[^\n]"); /* Skip heading in data file */ fscanf(fpin, "%ld", &n); if (n >= 4) { 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%lf", &x[i], &y[i]); /* nag_1d_quad_vals (d01gac). * One-dimensional integration of a function defined by data * values only */ nag_1d_quad_vals(n, x, y, &ans, &error, &fail); if (fail.code == NE_NOERROR) { fprintf(fpout, "Integral = %7.4f\n", ans); fprintf(fpout, "Estimated error = %7.4f\n", error); } else { fprintf(fpout, "Error from nag_1d_quad_vals (d01gac).\n%s\n", fail.message); fprintf(fpout, "%s\n", fail.message); exit_status = 1; } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (x) NAG_FREE(x); if (y) NAG_FREE(y); return exit_status; }