NAG Library Manual, Mark 28.3
Interfaces:  FL   CL   CPP   AD 

NAG CL Interface Introduction
Example description
/* nag_stat_prob_beta_vector (g01sec) Example Program.
 *
 * Copyright 2022 Numerical Algorithms Group.
 *
 * Mark 28.3, 2022.
 */
#include <nag.h>
#include <stdio.h>

int main(void) {
  /* Integer scalar and array declarations */
  Integer ltail, lbeta, la, lb, i, lout;
  Integer *ivalid = 0;
  Integer exit_status = 0;

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

  /* Double scalar and array declarations */
  double *beta = 0, *a = 0, *b = 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_stat_prob_beta_vector (g01sec) 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] ", &lbeta);
  if (!(beta = NAG_ALLOC(lbeta, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < lbeta; i++)
    scanf("%lf", &beta[i]);
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &la);
  if (!(a = NAG_ALLOC(la, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < la; i++)
    scanf("%lf", &a[i]);
  scanf("%*[^\n] ");
  scanf("%" NAG_IFMT "%*[^\n] ", &lb);
  if (!(b = NAG_ALLOC(lb, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  for (i = 0; i < lb; i++)
    scanf("%lf", &b[i]);
  scanf("%*[^\n] ");

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

  /* Calculate probability */
  nag_stat_prob_beta_vector(ltail, tail, lbeta, beta, la, a, lb, b, p, ivalid,
                            &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_stat_prob_beta_vector (g01sec).\n%s\n",
           fail.message);
    exit_status = 1;
    if (fail.code != NW_IVALID)
      goto END;
  }

  /* Display title */
  printf("        tail          beta        a         b        ");
  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]), beta[i % lbeta], a[i % la],
           b[i % lb], p[i], ivalid[i]);

END:
  NAG_FREE(tail);
  NAG_FREE(beta);
  NAG_FREE(a);
  NAG_FREE(b);
  NAG_FREE(p);
  NAG_FREE(ivalid);

  return (exit_status);
}