/* nag_prob_hypergeom_vector (g01slc) 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)
{
  /* Integer scalar and array declarations */
  Integer ln, ll, lm, lk, i, lout;
  Integer *ivalid = 0, *n = 0, *l = 0, *m = 0, *k = 0;
  Integer exit_status = 0;

  /* NAG structures */
  NagError fail;

  /* Double scalar and array declarations */
  double *peqk = 0, *pgtk = 0, *plek = 0;

  /* Initialize the error structure to print out any error messages */
  INIT_FAIL(fail);

  printf("nag_prob_hypergeom_vector (g01slc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &ln);
  if (!(n = NAG_ALLOC(ln, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < ln; i++)
    scanf("%" NAG_IFMT "", &n[i]);
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &ll);
  if (!(l = NAG_ALLOC(ll, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < ll; i++)
    scanf("%" NAG_IFMT "", &l[i]);
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &lm);
  if (!(m = NAG_ALLOC(lm, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < lm; i++)
    scanf("%" NAG_IFMT "", &m[i]);
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &lk);
  if (!(k = NAG_ALLOC(lk, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < lk; i++)
    scanf("%" NAG_IFMT "", &k[i]);
  scanf("%*[^\n] ");

  /* Allocate memory for output */
  lout = MAX(ln, MAX(ll, MAX(lm, lk)));
  if (!(peqk = NAG_ALLOC(lout, double)) ||
      !(pgtk = NAG_ALLOC(lout, double)) ||
      !(plek = NAG_ALLOC(lout, double)) ||
      !(ivalid = NAG_ALLOC(lout, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Calculate probability */
  nag_prob_hypergeom_vector(ln, n, ll, l, lm, m, lk, k,
                            plek, pgtk, peqk, ivalid, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_prob_hypergeom_vector (g01slc).\n%s\n",
           fail.message);
    exit_status = 1;
    if (fail.code != NW_IVALID)
      goto END;
  }

  /* Display title */
  printf("     n         l         m         k       ");
  printf("plek      pgtk      peqk  ivalid\n");
  printf(" -----------------------------------");
  printf("-----------------------------------------\n");

  /* Display results */
  for (i = 0; i < lout; i++) {
    printf(" %6" NAG_IFMT "    %6" NAG_IFMT "    %6" NAG_IFMT "    %6"
           NAG_IFMT "", n[i % ln], l[i % ll], m[i % lm], k[i % lk]);
    printf("    %6.3f    %6.3f    %6.3f   %3" NAG_IFMT "\n", plek[i], pgtk[i],
           peqk[i], ivalid[i]);
  }

END:
  NAG_FREE(n);
  NAG_FREE(l);
  NAG_FREE(m);
  NAG_FREE(k);
  NAG_FREE(plek);
  NAG_FREE(pgtk);
  NAG_FREE(peqk);
  NAG_FREE(ivalid);

  return (exit_status);
}