/* nag_approx_quantiles_fixed (g01anc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 23, 2011.
 */

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

int main(void)
{
  /* Scalars */
  Integer  exit_status = 0;
  Integer  i, ind, j, licomm, lrcomm, n, nb, np, nq, nrv;
  double   eps;
  /* Arrays */
  double   *q = 0, *qv = 0, *rcomm = 0, *rv = 0, trcomm[1], trv[1];
  Integer  *icomm = 0, ticomm[2];
  /* Nag Types */
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_approx_quantiles_fixed (g01anc) Example Program Results\n");

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

  /* Read in the problem size */
  scanf("%ld%*[^\n] ", &n);
  scanf("%lf%*[^\n] ", &eps);
  scanf("%ld%*[^\n] ", &nq);

  if (!(qv = NAG_ALLOC(nq, double)) ||
      !(q = NAG_ALLOC(nq, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read in the quantiles that are required */
  for (i = 0; i < nq; ++i)
    scanf("%lf", &q[i]);
  scanf("%*[^\n] ");

  /* Call the routine for the first time to obtain lrcomm and licomm */
  nb = lrcomm = 1;
  licomm = 2;
  ind = 0;
  nag_approx_quantiles_fixed(&ind, n, trv, nb, eps, &np, q, qv,
                             nq, trcomm, lrcomm, ticomm, licomm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_approx_quantiles_fixed (g01anc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Use calculated array sizes to allocate the communication arrays */
  lrcomm = ticomm[0];
  licomm = ticomm[1];
  if (!(rcomm = NAG_ALLOC(lrcomm, double)) ||
      !(icomm = NAG_ALLOC(licomm, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read in the number of blocks of data */
  scanf("%ld%*[^\n] ", &nrv);

  /* Loop over each block of data */
  for (i = 0; i < nrv; ++i) {

    /* Read in the size of the i'th block of data */
    scanf("%ld%*[^\n] ", &nb);

    /* Reallocate rv */
    NAG_FREE(rv);
    if (!(rv = NAG_ALLOC(nb, double))) {
      printf("Allocation failure\n");
      exit_status = -1;
      goto END;
    }

    /* Read in the data for the i'th block */
    for (j = 0; j < nb; ++j)
      scanf("%lf", &rv[j]);
    scanf("%*[^\n] ");

    /* Update the summaries based on the i'th block of data */
    nag_approx_quantiles_fixed(&ind, n, rv, nb, eps, &np, q, qv, nq,
                               rcomm, lrcomm, icomm, licomm, &fail);
    if (fail.code != NE_NOERROR) {
      printf("Error from nag_approx_quantiles_fixed (g01anc).\n%s\n",
             fail.message);
      exit_status = 1;
      goto END;
    }
    if (ind == 4) break;
  }

  /* Call the routine again to calculate quantiles specified in vector q */
  ind = 3;
  nag_approx_quantiles_fixed(&ind, n, rv, nb, eps, &np, q, qv, nq, rcomm,
                             lrcomm, icomm, licomm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_approx_quantiles_fixed (g01anc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print the results */
  printf("\n    Input data:\n");
  printf("    %ld observations\n", n);
  printf("    eps = %5.2f\n", eps);
  printf("    Quantile   Result\n\n");
  for (i = 0; i < nq; ++i) {
    printf("  %7.2f    %7.2f\n", q[i], qv[i]);
  }

 END:
  NAG_FREE(rv);
  NAG_FREE(q);
  NAG_FREE(qv);
  NAG_FREE(rcomm);
  NAG_FREE(icomm);

  return exit_status;
}