/* nag_tabulate_stats (g11bac) Example Program. * * Copyright 2000 Numerical Algorithms Group. * * Mark 6a revised, 2001. */ #include #include #include #include int main (void) { Integer *count=0, exit_status=0, *factor=0, i, *idim=0, *isf=0, items, j; Integer k, *lfac=0, ltmax, maxt, n, ncells, ncol, ndim, nfac, nrow, tdf; NagError fail; Nag_TableStats stat_enum; Nag_Weightstype weight_enum; char stat[2], weight[2]; double *comm_ar=0, *table=0, *wt=0, *y=0; #define FACTOR(I,J) factor[((I)-1)*nfac + (J) - 1] INIT_FAIL(fail); Vprintf("nag_tabulate_stats (g11bac) Example Program Results\n"); /* Skip heading in data file */ Vscanf("%*[^\n]"); Vscanf(" %s %s %ld %ld ", stat, weight, &n, &nfac); ltmax = 18; maxt = ltmax; if (!(isf = 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)) || !(wt = NAG_ALLOC(n, double)) || !(table = NAG_ALLOC(maxt, double)) || !(comm_ar = NAG_ALLOC(2*maxt, double))) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } if (*weight == 'W' || *weight == 'V') { for (i = 1; i <= n; ++i) { for (j = 1; j <= nfac; ++j) Vscanf("%ld", &FACTOR(i,j)); Vscanf("%lf %lf", &y[i - 1], &wt[i - 1]); } } else { for (i = 1; i <= n; ++i) { for (j = 1; j <= nfac; ++j) Vscanf("%ld", &FACTOR(i,j)); Vscanf("%lf", &y[i - 1]); } } for (j = 1; j <= nfac; ++j) Vscanf("%ld", &lfac[j - 1]); for (j = 1; j <= nfac; ++j) Vscanf("%ld", &isf[j - 1]); tdf = 3; maxt = ltmax; if (*stat == 'N') stat_enum = Nag_TableStatsNObs; else if (*stat == 'T') stat_enum = Nag_TableStatsTotal; else if (*stat == 'A') stat_enum = Nag_TableStatsAv; else if (*stat == 'V') stat_enum = Nag_TableStatsVar; else if (*stat == 'L') stat_enum = Nag_TableStatsLarge; else if (*stat == 'S') stat_enum = Nag_TableStatsSmall; else stat_enum = (Nag_TableStats)-999; if (*weight == 'U') weight_enum = Nag_NoWeights; else if (*weight == 'W') weight_enum = Nag_Weights; else if (*weight == 'V') weight_enum = Nag_Weightsvar; else weight_enum = (Nag_Weightstype)-999; /* nag_tabulate_stats (g11bac). * Computes multiway table from set of classification * factors using selected statistic */ nag_tabulate_stats(stat_enum, Nag_TableUpdateI, weight_enum, n, nfac, isf, lfac, factor, tdf, y, wt, table, maxt, &ncells, &ndim, idim, count, comm_ar, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_tabulate_stats (g11bac).\n%s\n", fail.message); exit_status = 1; goto END; } Vprintf("\n"); Vprintf("%s\n", "Table"); Vprintf("\n"); ncol = idim[ndim - 1]; nrow = ncells / ncol; k = 1; items = 0; for (i = 1; i <= nrow; ++i) { for (j = k, items =1; j <= k + ncol - 1; ++j, items++) Vprintf("%8.2f(%2ld)%s", table[j - 1], count[j - 1], items%6?"":"\n"); k += ncol; } END: if (isf) NAG_FREE(isf); if (lfac) NAG_FREE(lfac); if (idim) NAG_FREE(idim); if (factor) NAG_FREE(factor); if (count) NAG_FREE(count); if (y) NAG_FREE(y); if (wt) NAG_FREE(wt); if (table) NAG_FREE(table); if (comm_ar) NAG_FREE(comm_ar); return exit_status; }