/* nag_1d_everett_interp (e01abc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 23, 2011.
 */

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

int main(void)
{
  /* Scalars */
  Integer exit_status = 0;
  Integer i, n, r;
  double  p;
  NagError fail;
  /* Local Arrays */
  double  *a = 0, *g = 0;

  INIT_FAIL(fail);

  printf("nag_1d_everett_interp (e01abc) Example Program Results\n");

  /* Skip heading in data file*/
  scanf("%*[^\n] ");
  scanf("%"NAG_IFMT "", &n);
  scanf("%lf", &p);
  scanf("%*[^\n] ");

  /* Allocate memory */
  if (!(a = NAG_ALLOC((2*n), double)) ||
      !(g = NAG_ALLOC((2*n+1), double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

  for (i = 0; i < 2*n; i++)
    scanf("%lf", &a[i]);
  scanf("%*[^\n] ");

  /* nag_1d_everett_interp (e01abc).
   * Interpolated values, Everett's formula, equally spaced data, one variable.
   */
  nag_1d_everett_interp(n, p, a, g, &fail);
  if (fail.code != NE_NOERROR){
    printf("Error from nag_1d_everett_interp (e01abc).\n%s\n",
            fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\n");
  for (r = 0; r <= n - 1; r++){
    printf("Central differences order %"NAG_IFMT "  of Y0 = %12.5f\n",
            r, g[2 * r]);
    printf("                                Y1 = %12.5f\n",
            g[2 * r + 1]);
  }
  printf("\n");
  printf("Function value at interpolation point = %12.5f\n", g[2*n]);

 END:
  NAG_FREE(a);
  NAG_FREE(g);

  return exit_status;
}