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

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

int main(void)
{

  /* Scalars */
  double xsd, xskew, xkurt, xmean;
  Integer exit_status = 0, i, j, k, kmin1, n, nprob;

  NagError fail;

  /* Arrays */
  double *x = 0;
  Integer *ifreq = 0;

  INIT_FAIL(fail);

  printf("nag_summary_stats_freq (g01adc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &nprob);
  for (j = 1; j <= nprob; ++j) {
    scanf("%" NAG_IFMT "%*[^\n] ", &kmin1);
    k = kmin1 + 1;

    /* Allocate memory */
    if (!(x = NAG_ALLOC(k, double)) || !(ifreq = NAG_ALLOC(k, Integer)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

    for (i = 1; i <= kmin1; ++i)
      scanf("%lf%" NAG_IFMT "", &x[i - 1], &ifreq[i - 1]);
    scanf("%lf%*[^\n] ", &x[k - 1]);

    printf("\nProblem %4" NAG_IFMT "\n", j);
    printf("Number of classes %4" NAG_IFMT "\n", kmin1);

    /* nag_summary_stats_freq (g01adc).
     * Mean, variance, skewness, kurtosis, etc., one variable,
     * from frequency table
     */
    nag_summary_stats_freq(k, x, ifreq, &xmean, &xsd, &xskew, &xkurt, &n,
                           &fail);

    if (fail.code == NE_NOERROR) {
      printf("Successful call of " "nag_summary_stats_freq (g01adc)\n\n");
      printf("          Class          Frequency\n\n");
      for (i = 1; i <= kmin1; ++i)
        printf("%10.2f%10.2f%12" NAG_IFMT "\n", x[i - 1], x[i], ifreq[i - 1]);

      printf("\n Mean %16.4f\n", xmean);
      printf(" Std devn%13.4f\n", xsd);
      printf(" Skewness%13.4f\n", xskew);
      printf(" Kurtosis%13.4f\n", xkurt);
      printf(" Number of cases%8" NAG_IFMT "\n", n);
    }
    else {
      printf("Error from nag_summary_stats_freq (g01adc).\n%s\n",
             fail.message);
      exit_status = 1;
    }
    NAG_FREE(x);
    NAG_FREE(ifreq);
  }
END:
  NAG_FREE(x);
  NAG_FREE(ifreq);
  return exit_status;
}