/* srhist.c -- produce a summed rank histogram * * srhist n N * * Assume that n items are chosen at random from a numbered (i.e. * ranked) list of N items. srhist determines the distribution of the * sum of ranks of these n items by direct generation. Each possible * rank is written to stdout as a single line in the format: * * sr f rf cf rcf * * sr is the sum of ranks. f is the absolute number of times that * particular sum occurs. rf is its relative frequency as a * percentage. cf is the absolute cumulative frequency, i.e., the * number of sums of ranks less than or equal to sr. rcf is the * relative cumulative frequency as a percentage. */ #include #include "common.h" char *ealloc(); int atoi(); int sumhist(); char *PROGNAME; int n, N; int *pick; long *hist; int minsr, maxsr, nsr; long npicks = 0L; main(argc, argv) int argc; char *argv[]; { register int i; register long s; register double ds; register double rf, rcf; STARTUP; options(argc, argv); numbers(argc, argv); minsr = n * (n-1) / 2; maxsr = (N-n) * n + minsr; nsr = maxsr - minsr + 1; pick = (int *) ealloc(n * sizeof(int)); hist = (long *) ealloc(nsr * sizeof(long)); for(i=0; i 0) { if ( (NULL == (s = *argv++)) || ('-' != *s++) ) continue; argv[-1] = NULL; while('\0' != (c = *s++)) { switch(c) { default: usage(); } } } } /* numbers -- get numbers for srhist */ numbers(argc, argv) int argc; char *argv[]; { register char *s; register int i = 0; int nums[2]; while(argc-- > 0) { if ( (NULL == (s = *argv++)) || ('0' > *s) || ('9' < *s) ) continue; argv[-1] = NULL; if (i >= 2) usage(); nums[i++] = atoi(s); } n = nums[0]; N = nums[1]; } /* sumhist -- sum up the histogram */ int sumhist(na, Na, picka) register int na, Na, *picka; { register int sr = -minsr; while(na-- > 0) { sr += *picka++; } hist[sr]++; return(0); } usage() { error("usage: srhist n N"); }