/* nag_sign_test (g08aac) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

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

int main(void)
{
  Integer exit_status = 0, i, n, non_tied, s;
  NagError fail;
  double p, *x = 0, *y = 0;

  INIT_FAIL(fail);

  printf("nag_sign_test (g08aac) Example Program Results\n");

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

  n = 17;
  if (!(x = NAG_ALLOC(n, double))
      || !(y = NAG_ALLOC(n, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  for (i = 1; i <= n; i++)
    scanf("%lf", &x[i - 1]);

  for (i = 1; i <= n; i++)
    scanf("%lf", &y[i - 1]);

  printf("\n%s\n\n", "Sign test");
  printf("%s\n\n", "Data values");
  for (i = 1; i <= n; i++)
    printf("%3.0f%s", x[i - 1], i % n ? "" : "\n");
  printf("\n");

  for (i = 1; i <= n; i++)
    printf("%3.0f%s", y[i - 1], i % n ? "" : "\n");
  printf("\n");

  /* nag_sign_test (g08aac).
   * Sign test on two paired samples
   */
  nag_sign_test(n, x, y, &s, &p, &non_tied, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_sign_test (g08aac).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("%s%5" NAG_IFMT "\n", "Test statistic   ", s);
  printf("%s%5" NAG_IFMT "\n", "Observations     ", non_tied);
  printf("%s%5.3f\n", "Lower tail prob. ", p);
END:
  NAG_FREE(x);
  NAG_FREE(y);
  return exit_status;
}