/* nag_prob_f_vector (g01sdc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */
#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg01.h>

int main(void)
{
  /* Integer scalar and array declarations */
  Integer ltail, lf, ldf1, ldf2, i, lout;
  Integer *ivalid = 0;
  Integer exit_status = 0;

  /* NAG structures */
  NagError fail;
  Nag_TailProbability *tail = 0;

  /* Double scalar and array declarations */
  double *f = 0, *df1 = 0, *df2 = 0, *p = 0;

  /* Character scalar and array declarations */
  char ctail[40];

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

  printf("nag_prob_f_vector (g01sdc) Example Program Results\n\n");

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

  /* Read in the input vectors */
  scanf("%" NAG_IFMT "%*[^\n] ", &ltail);
  if (!(tail = NAG_ALLOC(ltail, Nag_TailProbability))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < ltail; i++) {
    scanf("%39s", ctail);
    tail[i] = (Nag_TailProbability) nag_enum_name_to_value(ctail);
  }
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &lf);
  if (!(f = NAG_ALLOC(lf, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < lf; i++)
    scanf("%lf", &f[i]);
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &ldf1);
  if (!(df1 = NAG_ALLOC(ldf1, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < ldf1; i++)
    scanf("%lf", &df1[i]);
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &ldf2);
  if (!(df2 = NAG_ALLOC(ldf2, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < ldf2; i++)
    scanf("%lf", &df2[i]);
  scanf("%*[^\n] ");

  /* Allocate memory for output */
  lout = MAX(ltail, MAX(lf, MAX(ldf1, ldf2)));
  if (!(p = NAG_ALLOC(lout, double)) || !(ivalid = NAG_ALLOC(lout, Integer)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Calculate probability */
  nag_prob_f_vector(ltail, tail, lf, f, ldf1, df1, ldf2, df2,
                    p, ivalid, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_prob_f_vector (g01sdc).\n%s\n", fail.message);
    exit_status = 1;
    if (fail.code != NW_IVALID)
      goto END;
  }

  /* Display title */
  printf("        tail           f         df1       df2       ");
  printf("p     ivalid\n");
  printf(" ----------------------------------------------------");
  printf("--------------\n");

  /* Display results */
  for (i = 0; i < lout; i++)
    printf(" %15s    %6.2f    %6.2f    %6.2f    %6.3f    %3" NAG_IFMT "\n",
           nag_enum_value_to_name(tail[i % ltail]), f[i % lf], df1[i % ldf1],
           df2[i % ldf2], p[i], ivalid[i]);

END:
  NAG_FREE(tail);
  NAG_FREE(f);
  NAG_FREE(df1);
  NAG_FREE(df2);
  NAG_FREE(p);
  NAG_FREE(ivalid);

  return (exit_status);
}