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

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

int main(void)
{
  Integer c__0 = 0, exit_status = 0, i, *irep = 0, *it = 0, j, n, ncol,
         nrep, nrow, nt;
  NagError fail;
  const char *fmt_99999[] = { "%3.0f  ", "%10.4f  ", "%10.4f  ", "%10.4f  ",
    "%8.4f"
  };
  double *c = 0, c_b20 = 1e-5, *cmean = 0, *ef = 0, gmean, *r = 0;
  double *rmean = 0, *rpmean = 0, *table = 0, *tmean = 0, *y = 0;

#define TABLE(I, J) table[((I) -1)*5 + (J) -1]
#define C(I, J)     c[((I) -1)*nt + (J) -1]

  INIT_FAIL(fail);

  printf("nag_anova_row_col (g04bcc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT " %" NAG_IFMT " %" NAG_IFMT " %" NAG_IFMT "", &nrep,
        &nrow, &ncol, &nt);
  if (!(c = NAG_ALLOC(nt * nt, double))
      || !(cmean = NAG_ALLOC(nrep * ncol, double))
      || !(ef = NAG_ALLOC(nt, double))
      || !(r = NAG_ALLOC(nrep * nrow * ncol, double))
      || !(y = NAG_ALLOC(nrep * nrow * ncol, double))
      || !(rmean = NAG_ALLOC(nrep * nrow, double))
      || !(rpmean = NAG_ALLOC(nrep, double))
      || !(tmean = NAG_ALLOC(nt, double))
      || !(table = NAG_ALLOC(30, double))
      || !(irep = NAG_ALLOC(nt, Integer))
      || !(it = NAG_ALLOC(nrep * nrow * ncol, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  n = nrep * nrow * ncol;
  for (i = 1; i <= n; ++i)
    scanf("%lf", &y[i - 1]);
  for (i = 1; i <= n; ++i)
    scanf("%" NAG_IFMT "", &it[i - 1]);
  /* nag_anova_row_col (g04bcc).
   * Analysis of variance, general row and column design,
   * treatment means and standard errors
   */
  nag_anova_row_col(nrep, nrow, ncol, y, nt, it, &gmean, tmean, table,
                    c, nt, irep, rpmean, rmean, cmean, r, ef, c_b20, c__0,
                    &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_anova_row_col (g04bcc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }
  printf("\n ANOVA TABLE\n\n");
  if (nrep > 1) {
    printf("\n Reps        ");
    for (j = 1; j <= 5; ++j)
      printf(fmt_99999[j - 1], TABLE(1, j));
  }

  printf("\n Rows        ");
  for (j = 1; j <= 5; ++j)
    printf(fmt_99999[j - 1], TABLE(2, j));

  printf("\n Columns     ");
  for (j = 1; j <= 5; ++j)
    printf(fmt_99999[j - 1], TABLE(3, j));

  printf("\n\n Treatments  ");
  for (j = 1; j <= 5; ++j)
    printf(fmt_99999[j - 1], TABLE(4, j));

  printf("\n Residual    ");
  for (j = 1; j <= 3; ++j)
    printf(fmt_99999[j - 1], TABLE(5, j));

  printf("\n Total       ");
  for (j = 1; j <= 2; ++j)
    printf(fmt_99999[j - 1], TABLE(6, j));

  printf("\n Treatment means\n\n");
  for (i = 1; i <= nt; ++i)
    printf("%10.4f%s", tmean[i - 1], i % 6 ? "" : "\n");
  printf("\n\n S.E. of difference (orthogonal design) = %10.4f\n", C(2, 1));
END:
  NAG_FREE(c);
  NAG_FREE(cmean);
  NAG_FREE(ef);
  NAG_FREE(r);
  NAG_FREE(y);
  NAG_FREE(rmean);
  NAG_FREE(rpmean);
  NAG_FREE(tmean);
  NAG_FREE(table);
  NAG_FREE(irep);
  NAG_FREE(it);
  return exit_status;
}