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

NAG CL Interface Introduction
Example description
/* nag_contab_tabulate_percentile (g11bbc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.2, 2024.
 */

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

int main(void) {
  Integer exit_status = 0, i, items, j, k, ltmax, maxt, n, ncells;
  Integer ncol, ndim, nfac, nrow, tdf;
  Integer *count = 0, *factor = 0, *idim = 0, *lfac = 0, *sf = 0;
  double percnt, *table = 0, *wt = 0, *wtptr, *y = 0;
  char nag_enum_arg[40];
  Nag_TabulateVar type;
  Nag_Weightstype weight;
  NagError fail;

#define FACTOR(I, J) factor[((I)-1) * nfac + (J)-1]

  INIT_FAIL(fail);

  printf("nag_contab_tabulate_percentile (g11bbc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%39s", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  type = (Nag_TabulateVar)nag_enum_name_to_value(nag_enum_arg);
  scanf("%39s", nag_enum_arg);
  weight = (Nag_Weightstype)nag_enum_name_to_value(nag_enum_arg);
  scanf("%" NAG_IFMT " %" NAG_IFMT " %lf", &n, &nfac, &percnt);
  ltmax = 18;
  maxt = ltmax;
  if (!(sf = NAG_ALLOC(nfac, Integer)) || !(lfac = NAG_ALLOC(nfac, Integer)) ||
      !(idim = NAG_ALLOC(nfac, Integer)) ||
      !(factor = NAG_ALLOC(n * nfac, Integer)) ||
      !(count = NAG_ALLOC(maxt, Integer)) || !(y = NAG_ALLOC(n, double)) ||
      !(table = NAG_ALLOC(maxt, double)) || !(wt = NAG_ALLOC(n, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  if (weight == Nag_Weights || weight == Nag_Weightsvar) {
    for (i = 1; i <= n; ++i) {
      for (j = 1; j <= nfac; ++j)
        scanf("%" NAG_IFMT "", &FACTOR(i, j));
      scanf("%lf %lf ", &y[i - 1], &wt[i - 1]);
    }
    wtptr = wt;
  } else {
    for (i = 1; i <= n; ++i) {
      for (j = 1; j <= nfac; ++j)
        scanf("%" NAG_IFMT "", &FACTOR(i, j));
      scanf("%lf", &y[i - 1]);
    }
    wtptr = 0;
  }
  for (j = 1; j <= nfac; ++j)
    scanf("%" NAG_IFMT "", &lfac[j - 1]);
  for (j = 1; j <= nfac; ++j)
    scanf("%" NAG_IFMT "", &sf[j - 1]);
  tdf = nfac;

  /* nag_contab_tabulate_percentile (g11bbc).
   * Computes multiway table from set of classification
   * factors using given percentile/quantile
   */
  nag_contab_tabulate_percentile(type, n, nfac, sf, lfac, factor, tdf, percnt,
                                 y, wtptr, table, maxt, &ncells, &ndim, idim,
                                 count, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_contab_tabulate_percentile (g11bbc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }
  printf("\n");
  printf("%s%4.0f%s\n", "Table for ", percnt, "th percentile");
  printf("\n");
  ncol = idim[ndim - 1];
  nrow = ncells / ncol;
  k = 1;
  for (i = 1; i <= nrow; ++i) {
    for (items = 1, j = k; j <= k + ncol - 1; ++j, items++) {
      printf("%8.2f(%2" NAG_IFMT ")%s", table[j - 1], count[j - 1],
             items % 6 ? "" : "\n");
    }
    k += ncol;
  }
END:
  NAG_FREE(sf);
  NAG_FREE(lfac);
  NAG_FREE(idim);
  NAG_FREE(factor);
  NAG_FREE(count);
  NAG_FREE(y);
  NAG_FREE(table);
  NAG_FREE(wt);
  return exit_status;
}