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

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

int main(void)
{
  Integer exit_status = 0, i, ix, j, k, n;
  NagError fail;
  double fr, sig, *x = 0;

#define X(I, J) x[((I) -1)*n +(J) -1]

  INIT_FAIL(fail);

  printf("nag_friedman_test (g08aec) Example Program Results\n");

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

  n = 18;
  k = 3;
  ix = k;
  if (!(x = NAG_ALLOC(ix * n, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 1; i <= ix; ++i)
    for (j = 1; j <= n; ++j)
      scanf("%lf", &X(i, j));

  printf("\nFriedman test\n");
  printf("\nData values\n");
  printf("\n   Group  Group  Group\n");
  printf("     1      2      3\n");
  for (j = 1; j <= 18; ++j) {
    for (i = 1; i <= 3; ++i)
      printf("%7.1f", X(i, j));
    printf("\n");
  }
  /* nag_friedman_test (g08aec).
   * Friedman two-way analysis of variance on k matched
   * samples
   */
  nag_friedman_test(k, n, x, n, &fr, &sig, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_friedman_test (g08aec).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  printf("\n");
  printf("%s%6.3f\n", "Test statistic       ", fr);
  printf("%s%6" NAG_IFMT "\n", "Degrees of freedom   ", k - 1);
  printf("%s%6.3f\n", "Significance         ", sig);
END:
  NAG_FREE(x);
  return exit_status;
}