/* nag_monotonic_intg (e01bhc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */

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

int main(void)
{
  Integer exit_status = 0, n, r;
  NagError fail;
  double a, b, *d = 0, *f = 0, integral, *x = 0;

  INIT_FAIL(fail);

  printf("nag_monotonic_intg (e01bhc) Example Program Results\n");
  scanf("%*[^\n]"); /* Skip heading in data file */
  scanf("%" NAG_IFMT "", &n);
  if (n >= 2) {
    if (!(d = NAG_ALLOC(n, double)) ||
        !(f = NAG_ALLOC(n, double)) || !(x = 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 (r = 0; r < n; r++)
    scanf("%lf%lf%lf", &x[r], &f[r], &d[r]);
  printf("                                          Integral\n");
  printf("          a                  b            over (a,b)\n");
  /* Read a, b pairs until end of file and compute
   * definite integrals.
   */
  while (scanf("%lf%lf", &a, &b) != EOF)
  {
    /* nag_monotonic_intg (e01bhc).
     * Evaluation of interpolant computed by
     * nag_monotonic_interpolant (e01bec), definite integral
     */
    nag_monotonic_intg(n, x, f, d, a, b, &integral, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_monotonic_intg (e01bhc).\n%s\n", fail.message);
      exit_status = 1;
      goto END;
    }
    printf("%13.4f      %13.4f      %13.4f\n", a, b, integral);
  }
END:
  NAG_FREE(d);
  NAG_FREE(f);
  NAG_FREE(x);
  return exit_status;
}