| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id$";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* Compute a histogram from input data
|
| 6 |
*
|
| 7 |
* 9/5/96 Greg Ward
|
| 8 |
*/
|
| 9 |
|
| 10 |
#include <stdio.h>
|
| 11 |
#include <stdlib.h>
|
| 12 |
#include <math.h>
|
| 13 |
#include <ctype.h>
|
| 14 |
|
| 15 |
#define MAXCOL 64 /* maximum number of input columns */
|
| 16 |
#define MAXDIV 1024
|
| 17 |
|
| 18 |
#define isint(x) (floor((x)+1e-6) != floor((x)-1e-6))
|
| 19 |
|
| 20 |
char *progname;
|
| 21 |
|
| 22 |
int cumulative = 0;
|
| 23 |
|
| 24 |
long histo[MAXCOL][MAXDIV];
|
| 25 |
double minv, maxv;
|
| 26 |
int ndiv;
|
| 27 |
|
| 28 |
int ncols;
|
| 29 |
|
| 30 |
|
| 31 |
main(argc, argv)
|
| 32 |
int argc;
|
| 33 |
char *argv[];
|
| 34 |
{
|
| 35 |
progname = argv[0];
|
| 36 |
if (argc > 1 && !strcmp(argv[1], "-c")) {
|
| 37 |
cumulative++;
|
| 38 |
argc--; argv++;
|
| 39 |
}
|
| 40 |
if (argc < 3)
|
| 41 |
goto userr;
|
| 42 |
minv = atof(argv[1]);
|
| 43 |
maxv = atof(argv[2]);
|
| 44 |
if (argc == 4)
|
| 45 |
ndiv = atoi(argv[3]);
|
| 46 |
else {
|
| 47 |
if (argc > 4 || !isint(minv) || !isint(maxv))
|
| 48 |
goto userr;
|
| 49 |
maxv += 0.5;
|
| 50 |
minv -= 0.5;
|
| 51 |
ndiv = maxv - minv + 0.5;
|
| 52 |
}
|
| 53 |
if (minv >= maxv | ndiv <= 0)
|
| 54 |
goto userr;
|
| 55 |
if (ndiv > MAXDIV) {
|
| 56 |
fprintf(stderr, "%s: maximum number of divisions: %d\n",
|
| 57 |
progname, MAXDIV);
|
| 58 |
goto userr;
|
| 59 |
}
|
| 60 |
readinp();
|
| 61 |
if (cumulative)
|
| 62 |
printcumul();
|
| 63 |
else
|
| 64 |
printhisto();
|
| 65 |
exit(0);
|
| 66 |
userr:
|
| 67 |
fprintf(stderr, "Usage: %s [-c] min max n\n", progname);
|
| 68 |
fprintf(stderr, " Or: %s [-c] imin imax\n", progname);
|
| 69 |
exit(1);
|
| 70 |
}
|
| 71 |
|
| 72 |
|
| 73 |
readinp() /* gather statistics on input */
|
| 74 |
{
|
| 75 |
char buf[16*MAXCOL];
|
| 76 |
double d;
|
| 77 |
register int c;
|
| 78 |
register char *cp;
|
| 79 |
|
| 80 |
while ((cp = fgets(buf, sizeof(buf), stdin)) != NULL) {
|
| 81 |
for (c = 0; c < MAXCOL; c++) {
|
| 82 |
while (isspace(*cp))
|
| 83 |
cp++;
|
| 84 |
if (!*cp)
|
| 85 |
break;
|
| 86 |
d = atof(cp);
|
| 87 |
while (*cp && !isspace(*cp))
|
| 88 |
cp++;
|
| 89 |
if (d >= minv && d < maxv)
|
| 90 |
histo[c][(int)(ndiv*(d-minv)/(maxv-minv))]++;
|
| 91 |
}
|
| 92 |
if (c > ncols)
|
| 93 |
ncols = c;
|
| 94 |
}
|
| 95 |
}
|
| 96 |
|
| 97 |
|
| 98 |
printcumul() /* print cumulative histogram results */
|
| 99 |
{
|
| 100 |
long ctot[MAXCOL];
|
| 101 |
register int i, c;
|
| 102 |
|
| 103 |
for (c = ncols; c--; )
|
| 104 |
ctot[c] = 0L;
|
| 105 |
|
| 106 |
for (i = 0; i < ndiv; i++) {
|
| 107 |
printf("%g", minv + (maxv-minv)*(i+1)/ndiv);
|
| 108 |
for (c = 0; c < ncols; c++) {
|
| 109 |
ctot[c] += histo[c][i];
|
| 110 |
printf("\t%ld", ctot[c]);
|
| 111 |
}
|
| 112 |
putchar('\n');
|
| 113 |
}
|
| 114 |
}
|
| 115 |
|
| 116 |
|
| 117 |
printhisto() /* print histogram results */
|
| 118 |
{
|
| 119 |
register int i, c;
|
| 120 |
|
| 121 |
for (i = 0; i < ndiv; i++) {
|
| 122 |
printf("%g", minv + (maxv-minv)*(i+.5)/ndiv);
|
| 123 |
for (c = 0; c < ncols; c++)
|
| 124 |
printf("\t%ld", histo[c][i]);
|
| 125 |
putchar('\n');
|
| 126 |
}
|
| 127 |
}
|