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

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

int main(void)
{
  Integer exit_status = 0;
  Integer n;
  double a, b;
  Integer i;
  Nag_QuadType quadtype;
  NagError fail;
  double *abscis = 0, *weight = 0;

  INIT_FAIL(fail);

  printf("nag_quad_1d_gauss_wset (d01tbc) Example Program Results\n");
  /* Skip heading in data file */
  scanf("%*[^\n] ");
  /* Input a, b and n */
  scanf("%lf %lf", &a, &b);
  scanf("%" NAG_IFMT "%*[^\n] ", &n);
  quadtype = Nag_Quad_Gauss_Laguerre_Adjusted;

  if (!(abscis = NAG_ALLOC(n, double)) || !(weight = NAG_ALLOC(n, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* nag_quad_1d_gauss_wset (d01tbc).
   * Pre-computed weights and abscissae for
   * Gaussian quadrature rules, restricted choice of rule.
   */
  nag_quad_1d_gauss_wset(quadtype, a, b, n, weight, abscis, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_quad_1d_gauss_wset (d01tbc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\nLaguerre formula, %3" NAG_IFMT " points\n\n"
         "    Abscissae        Weights\n\n", n);
  for (i = 0; i < n; i++) {
    printf("%15.6e", abscis[i]);
    printf("%15.6e\n", weight[i]);
  }
  printf("\n");

END:
  NAG_FREE(abscis);
  NAG_FREE(weight);

  return exit_status;
}