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