/* nag_double_quantiles (g01amc) Example Program.
 *
 * NAGPRODCODE Version.
 *
 * Copyright 2016 Numerical Algorithms Group.
 *
 * Mark 26, 2016.
 */

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

int main(void)
{
  /* Scalars */
  Integer exit_status = 0, i, n, nq;
  /* Arrays */
  double *vec = 0, *quants = 0, *quant_vec = 0;
  /* Nag Types */
  NagError fail;

  INIT_FAIL(fail);

  /* Skip heading in data file */
  scanf("%*[^\n]");
  printf("nag_double_quantiles (g01amc) Example Program Results\n");
  scanf("%" NAG_IFMT "", &n);
  scanf("%" NAG_IFMT "", &nq);
  if (n >= 1 && nq >= 1) {
    if (!(vec = NAG_ALLOC(n, double)) ||
        !(quants = NAG_ALLOC(nq, double)) ||
        !(quant_vec = NAG_ALLOC(nq, double)))
    {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }
  }
  else {
    if (n < 1) {
      printf("Invalid n.\n");
    }
    else {
      printf("Invalid nq.\n");
    }
    exit_status = 1;
    goto END;
  }
  for (i = 0; i < n; ++i)
    scanf("%lf", &vec[i]);
  for (i = 0; i < nq; ++i)
    scanf("%lf", &quants[i]);

  /* nag_double_quantiles (g01amc).
   * Find quantiles of set of values of data type double
   */
  nag_double_quantiles(n, vec, nq, quants, quant_vec, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_double_quantiles (g01amc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  printf("    Quantile   Result\n\n");
  for (i = 0; i < nq; ++i) {
    printf("   %7.4f     %7.4f\n", quants[i], quant_vec[i]);
  }

END:
  NAG_FREE(vec);
  NAG_FREE(quants);
  NAG_FREE(quant_vec);

  return exit_status;
}