/* nag_binomial_ci (g07aac) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 7, 2001.
 */

#include <stdio.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagg07.h>

int main(void)
{

  /* Scalars */
  double   clevel, phat, pl, pu;
  Integer  exit_status = 0, k, n;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_binomial_ci (g07aac) Example Program Results\n");

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

  printf("\n");
  printf(" Probability    Confidence Interval\n");
  printf("\n");
  while (scanf("%ld%ld%lf%*[^\n] ", &n, &k, &clevel) != EOF)
    {
      phat = (double) k / (double) n;
      /* nag_binomial_ci (g07aac).
       * Computes confidence interval for the parameter of a
       * binomial distribution
       */
      nag_binomial_ci(n, k, clevel, &pl, &pu, &fail);
      if (fail.code != NE_NOERROR)
        {
          printf("Error from nag_binomial_ci (g07aac).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }

      printf("%10.4f      ( %6.4f , %6.4f )\n", phat, pl, pu);
    }

 END:
  return exit_status;
}