/* nag_prob_lin_chi_sq (g01jdc) Example Program.
 *
 * Copyright 2017 Numerical Algorithms Group.
 *
 * Mark 26.1, 2017.
 */

#include <stdio.h>
#include <string.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg01.h>

int main(void)
{
  /* Scalars */
  double c, d, prob;
  Integer exit_status, i, n;
  /* Arrays */
  char nag_enum_arg[40];
  double *rlam = 0;
  Nag_LCCMethod method;
  NagError fail;

  INIT_FAIL(fail);

  exit_status = 0;
  printf("nag_prob_lin_chi_sq (g01jdc) Example Program Results\n");

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

  /* Allocate memory */
  if (!(rlam = NAG_ALLOC(n, double)))
  {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }
  scanf(" %39s%lf%lf%*[^\n] ", nag_enum_arg, &d, &c);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  method = (Nag_LCCMethod) nag_enum_name_to_value(nag_enum_arg);

  for (i = 1; i <= n; ++i)
    scanf("%lf", &rlam[i - 1]);
  scanf("%*[^\n] ");

  /* nag_prob_lin_chi_sq (g01jdc).
   * Computes lower tail probability for a linear combination
   * of (central) chi^2 variables
   */
  nag_prob_lin_chi_sq(method, n, rlam, d, c, &prob, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_prob_lin_chi_sq (g01jdc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("\n");
  printf(" Values of lambda ");
  for (i = 1; i <= n; ++i) {
    if (i % 6 == 0)
      printf("%18s", " ");

    printf("%6.2f ", rlam[i - 1]);

    if (i % 5 == 0 || i == n)
      printf("\n");
  }
  printf(" Value of D       %6.2f\n", d);
  printf(" Value of C       %6.2f\n\n", c);
  printf(" Probability = %11.4f\n", prob);

END:
  NAG_FREE(rlam);

  return exit_status;
}