/* nag_summary_stats_1var (g01aac) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 1, 1990.
 *
 * Mark 5 revised, 1998.
 * Mark 8 revised, 2004.
 *
 */

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

int main(void)
{
  Integer  exit_status = 0, i, j, n, nprob, nvalid, weight;
  NagError fail;
  double   wsum, *wt = 0, *x = 0, xkurt, xmax, xmean, xmin, xsd, xskew;

  INIT_FAIL(fail);

  /* Skip heading in data file */
  scanf("%*[^\n]");
  printf("nag_summary_stats_1var (g01aac) Example Program Results\n");
  scanf("%ld", &nprob);
  for (j = 1; j <= nprob; j++)
    {
      scanf("%ld %ld", &n, &weight);
      printf("Problem  %5ld\n", j);
      printf("Number of cases %ld\n", n);
      if (n >= 1)
        {
          if (!(wt = NAG_ALLOC(n, double)) ||
              !(x = NAG_ALLOC(n, double)))
            {
              printf("Allocation failure\n");
              exit_status = -1;
              goto END;
            }
        }
      else
        {
          printf("Invalid n.\n");
          exit_status = 1;
          return exit_status;
        }
      for (i = 0; i < n; i++)
        scanf("%lf", &x[i]);
      printf("Data as input -\n");
      for (i = 0; i < n; i++)
        printf("%12.1f%c", x[i], (i%5 == 4 || i == n-1)?'\n':' ');
      if (weight)
        {
          printf("Weights as input -\n");
          for (i = 0; i < n; i++)
            scanf("%lf", &wt[i]);
          for (i = 0; i < n; i++)
            printf("%12.1f%c", wt[i], (i%5 == 4 || i == n-1)?'\n':' ');
          /* nag_summary_stats_1var (g01aac).
           * Mean, variance, skewness, kurtosis, etc., one variable,
           * from raw data
           */
          nag_summary_stats_1var(n, x, wt, &nvalid, &xmean, &xsd, &xskew,
                                 &xkurt, &xmin, &xmax, &wsum, &fail);
        }
      else
        /* nag_summary_stats_1var (g01aac), see above. */
        nag_summary_stats_1var(n, x, (double *) 0, &nvalid, &xmean, &xsd,
                               &xskew, &xkurt, &xmin, &xmax, &wsum, &fail);

      if (fail.code == NE_NOERROR)
        {
          printf("\n");
          printf("Successful call of "
                  "nag_summary_stats_1var (g01aac)\n");
          printf("No. of valid cases %9ld\n", nvalid);
          printf("Mean           %13.1f\n", xmean);
          printf("Std devn       %13.1f\n", xsd);
          printf("Skewness       %13.1f\n", xskew);
          printf("Kurtosis       %13.1f\n", xkurt);
          printf("Minimum        %13.1f\n", xmin);
          printf("Maximum        %13.1f\n", xmax);
          printf("Sum of weights %13.1f\n", wsum);
        }
      else
        {
          printf("Unsuccessful call of "
                  "nag_summary_stats_1var (g01aac)\n");
          printf("%s \n", fail.message);
          if (fail.code == NE_CASES_ONE)
            {
              printf("No. of valid cases %9ld\n", nvalid);
              printf("Mean           %13.1f\n", xmean);
              printf("Minimum        %13.1f\n", xmin);
              printf("Maximum        %13.1f\n", xmax);
              printf("Sum of weights %13.1f\n", wsum);
              printf("Std devn and coeffts of skewness\n");
              printf("and kurtosis not defined\n");
              exit_status = 2;
            }
          else
            {
              exit_status = 1;
              goto END;
            }
        }

      NAG_FREE(wt);
      NAG_FREE(x);
    }
 END:
  NAG_FREE(wt);
  NAG_FREE(x);
  return exit_status;
}