/* nag_anderson_darling_normal_prob (g08ckc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 23, 2011.
 */
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg08.h>

int main(void)
{
  Integer        exit_status = 0;
  /* Scalars */
  double         a2, aa2, p, ybar, yvar;
  Integer        i, n;
  /* Arrays */
  double         *y = 0;
  /* Nag types */
  Nag_Boolean    issort;
  NagError       fail;

  printf("%s\n\n",
          "nag_anderson_darling_normal_prob (g08ckc) Example Program Results");

  /* Skip heading in data file */
  scanf("%*[^\n] ");

  /* Read number of observations */
  scanf("%"NAG_IFMT "", &n);
  scanf("%*[^\n] ");

  /* Memory allocation */
  if (!(y = NAG_ALLOC(n, double)))
  {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
  }

  /* Read observations */
  for (i = 0; i < n; i++)
  {
      scanf("%lf", y+i);
  }
  scanf("%*[^\n] ");

  /* Let nag_anderson_darling_normal_prob (g08ckc) sort the data */
  issort = Nag_FALSE;

  /* Calculate the Anderson-Darling goodness-of-fit test statistic and its
     probability for the case of a fully-unspecified Normal distribution */
  INIT_FAIL(fail);
  /* nag_anderson_darling_normal_prob (g08ckc) */
  nag_anderson_darling_normal_prob(n, issort, (const double *)y, &ybar, &yvar,
                                   &a2, &aa2, &p, &fail);

  /* Results */
  printf("%s ", "H0: data from Normal distribution with mean");
  printf("%6g ", ybar);
  printf("%s ", "and variance");
  printf("%6g\n", yvar);
  printf("%s", " Test statistic, A-squared: ");
  printf("%6g\n", a2);
  printf("%s", " Adjusted A-squared:        ");
  printf("%6g\n", aa2);
  printf("%s", " Upper tail probability:    ");
  printf("%6g\n", p);

 END:
  NAG_FREE(y);

  return exit_status;
}