/* nag_1d_spline_intg (e02bdc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 2, 1991.
 * Mark 8 revised, 2004.
 */

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


int main(void)
{
  Integer    exit_status = 0, j;
  NagError   fail;
  Nag_Spline spline;
  double     integral;

  INIT_FAIL(fail);

  /* Initialise spline */
  spline.lamda = 0;
  spline.c = 0;

  printf("nag_1d_spline_intg (e02bdc) Example Program Results\n");
  scanf("%*[^\n]"); /* Skip heading in data file */
  while (scanf("%ld", &(spline.n)) != EOF)
    {
      if (spline.n > 0)
        {
          if (!(spline.c = NAG_ALLOC(spline.n, double)) ||
              !(spline.lamda = NAG_ALLOC(spline.n, double)))
            {
              printf("Storage allocation failed. Reduce the "
                      "size of spline.n\n");
              exit_status = 1;
              goto END;
            }
        }
      else
        {
          printf("spline.n is out of range : spline.n = %ld\n",
                  spline.n);
          exit_status = 1;
          goto END;
        }
      for (j = 0; j < spline.n; j++)
        scanf("%lf", &(spline.lamda[j]));
      for (j = 0; j < spline.n-3; j++)
        scanf("%lf", &(spline.c[j]));
      /* nag_1d_spline_intg (e02bdc).
       * Evaluation of fitted cubic spline, definite integral
       */
      nag_1d_spline_intg(&spline, &integral, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_1d_spline_intg (e02bdc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }
      printf("Definite integral = %12.3e\n", integral);
      NAG_FREE(spline.c);
      NAG_FREE(spline.lamda);
    }
 END:
  return exit_status;
}