/* nag_tsa_mean_range (g13auc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 7, 2002.
 */

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

int main(void)
{
  /* Scalars */
  Integer  exit_status, i, ngrps, m, n;

  /* Arrays */
  double   *mean = 0, *range = 0, *z = 0;
  NagError fail;

  INIT_FAIL(fail);

  exit_status = 0;

  printf("nag_tsa_mean_range (g13auc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%ld%ld%*[^\n] ", &n, &m);
  if (n >= m && m >= 1)
    {
      ngrps = n / m;

      /* Allocate arrays */
      if (!(mean = NAG_ALLOC(ngrps, double)) ||
          !(range = NAG_ALLOC(ngrps, double)) ||
          !(z = NAG_ALLOC(n, double)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }

      for (i = 1; i <= n; ++i)
        scanf("%lf", &z[i-1]);
      scanf("%*[^\n] ");

      printf("\n");

      /* nag_tsa_mean_range (g13auc).
       * Computes quantities needed for range-mean or standard
       * deviation-mean plot
       */
      nag_tsa_mean_range(n, z, m, Nag_UseRange, range, mean, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_tsa_mean_range (g13auc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      printf("   Range      Mean\n");
      for (i = 1; i <= ngrps; i++)
        printf("%8.3f  %8.3f\n", range[i-1], mean[i-1]);
    }

 END:
  NAG_FREE(mean);
  NAG_FREE(range);
  NAG_FREE(z);

  return exit_status;
}