/* nag_1d_quad_vals (d01gac) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

#include <nag.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <nagd01.h>

int main(void)
{

  Integer exit_status = 0, i, n;
  NagError fail;
  double ans, error, *x = 0, *y = 0;

  INIT_FAIL(fail);

  printf("nag_1d_quad_vals (d01gac) Example Program Results\n");
  scanf("%*[^\n]"); /* Skip heading in data file */
  scanf("%" NAG_IFMT "", &n);
  if (n >= 4) {
    if (!(x = NAG_ALLOC(n, double)) || !(y = NAG_ALLOC(n, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  }
  else {
    printf("Invalid n.\n");
    exit_status = 1;
    return exit_status;
  }
  for (i = 0; i < n; ++i)
    scanf("%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) {
    printf("Integral = %7.4f\n", ans);
    printf("Estimated error = %7.4f\n", error);
  }
  else {
    printf("Error from nag_1d_quad_vals (d01gac).\n%s\n", fail.message);
    printf("%s\n", fail.message);
    exit_status = 1;
  }
END:
  NAG_FREE(x);
  NAG_FREE(y);
  return exit_status;
}