/* TestHist.c Test the Histogram class Author: J Morris Date: 5 October, 1997 */ #include #include #include "Histogram.h" #define DATA_FILE "hist.dat" main( int argc, char *argv[] ) { FILE *f; int n_bins, cnt, i, j, m_bin, mf; double factor, min, max, x, m; Histogram h; char *file_name; file_name = DATA_FILE; f = fopen( file_name, "r" ); if( f != NULL ) { /* Read the number of bins and the factor from the file */ fscanf( f, "%d%lg", &n_bins, &factor ); printf("Bins %d factor %g\n", n_bins, factor ); /* Calculate allowable minimum and maximum */ min = 0.0; max = n_bins * factor; if ( max < min ) { double tmp; tmp = min; min = max; max = tmp; } if ( (n_bins > 0) && (factor != 0.0) ) { h = ConsHistogram( n_bins, factor ); cnt = 0; if ( h != NULL ) { while( fscanf(f, "%lg", &x ) > 0 ) { if ( (x>=min) && (x <=max) ) AddDatum( h, x ); cnt++; } /* Our count should match the one kept by the object! */ assert( cnt == TotalPoints( h ) ); printf("Added %d data points\nMean %g\n", TotalPoints(h), SampleMean(h) ); m = SampleMedian( h, &m_bin ); printf("Median %g (bin %d)\n", m, m_bin ); mf = MaxFrequency( h, &m_bin ); printf("Max Frequency %d (bin %d)\n", mf, m_bin ); /* Make a simple representation of the histogram */ for(i=0;i