/* nag_prob_students_t_vector (g01sbc) 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 ltail, lt, ldf, i, lout;
  Integer *ivalid = 0;
  Integer exit_status = 0;

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

  /* Double scalar and array declarations */
  double *t = 0, *df = 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_students_t_vector (g01sbc) 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] ", &lt);
  if (!(t = NAG_ALLOC(lt, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < lt; i++)
    scanf("%lf", &t[i]);
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &ldf);
  if (!(df = NAG_ALLOC(ldf, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < ldf; i++)
    scanf("%lf", &df[i]);
  scanf("%*[^\n] ");

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

  /* Calculate probability */
  nag_prob_students_t_vector(ltail, tail, lt, t, ldf, df, p, ivalid, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_prob_students_t_vector (g01sbc).\n%s\n",
           fail.message);
    exit_status = 1;
    if (fail.code != NW_IVALID)
      goto END;
  }

  /* Display title */
  printf("          tail           t         df        p     ivalid\n");
  printf("-----------------------------------------------------------\n");

  /* Display results */
  for (i = 0; i < lout; i++)
    printf(" %17s    %6.3f    %6.1f    %6.4f    %3" NAG_IFMT "\n",
           nag_enum_value_to_name(tail[i % ltail]), t[i % lt], df[i % ldf],
           p[i], ivalid[i]);

END:
  NAG_FREE(tail);
  NAG_FREE(t);
  NAG_FREE(df);
  NAG_FREE(p);
  NAG_FREE(ivalid);

  return (exit_status);
}