/* nag_bessel_zeros (s17alc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * NAG C Library
 *
 * Mark 6, 2000.
 * Mark 7, revised, 2001.
 *
 */

#include <stdio.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nags.h>
#include <nagx02.h>

int main(void)
{

#define NMAX 100

  Integer  exit_status = 0, i, mode, n;
  NagError fail;
  double   a, rel, *x = 0;

  INIT_FAIL(fail);

  /* Skip heading in data file */
  scanf("%*[^\n]");
  printf("nag_bessel_zeros (s17alc) Example Program Results\n\n");

  if (!(x = NAG_ALLOC(NMAX, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
    }
  /* nag_machine_precision (x02ajc).
   * The machine precision
   */
  rel = sqrt(nag_machine_precision);
  scanf("%lf %ld %ld", &a, &n, &mode);
  /* nag_bessel_zeros (s17alc).
   * Zeros of Bessel functions J_alpha(x), (J_alpha')(x),
   * Y_alpha(x) or (Y_alpha')(x)
   */
  nag_bessel_zeros(a, n, mode, rel, x, &fail);

  if (fail.code == NE_NOERROR)
    {
      printf("   a   n   mode\n");
      printf(" %4.1f%3ld%6ld\n\n", a, n, mode);
      if (mode == 1)
        printf("Leading n positive zeros of J\n");
      else if (mode == 2)
        printf("Leading n positive zeros of Y\n");
      else if (mode == 3)
        {
          if (a == 0.0)
            printf("Leading n non-negative zeros of J'\n");
          else
            printf("Leading n positive zeros of J'\n");
        }
      else if (mode == 4)
        printf("Leading n positive zeros of Y'\n\n");
      for (i = 0; i <= n-1; ++i)
        printf("  x = %13.4e\n", x[i]);
      printf("\n");
    }
  else
    {
      printf("Error from nag_bessel_zeros (s17alc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }
 END:
  NAG_FREE(x);
  return exit_status;
}