Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > Histogram
diff FloatHist.c @ 2:06128e387cfa
Added DblHist and FloatHist
| author | Me |
|---|---|
| date | Sat, 30 Oct 2010 22:13:09 -0700 |
| parents | |
| children | 3d35477a5121 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/FloatHist.c Sat Oct 30 22:13:09 2010 -0700 1.3 @@ -0,0 +1,100 @@ 1.4 +/* 1.5 + * Copyright 2010 OpenSourceStewardshipFoundation.org 1.6 + * Licensed under GNU General Public License version 2 1.7 + * 1.8 + * Author: seanhalle@yahoo.com 1.9 + * 1.10 + */ 1.11 + 1.12 +#include "Histogram.h" 1.13 +#include <malloc.h> 1.14 + 1.15 + 1.16 +/*This Histogram Abstract Data Type has a number of bins, the starting 1.17 + * value, and the width of each bin, as a float, all chosen at creation. 1.18 + * 1.19 + *One creates a Histogram instance using the makeFloatHistogram function, then 1.20 + * updates it with the addToFloatHist function, and prints it out with the 1.21 + * printFloatHist function. 1.22 + * 1.23 + *Note, the bin width is an integer, so the end of the range is adjusted 1.24 + * accordingly. Use the bin-width to calculate the bin boundaries. 1.25 + */ 1.26 + 1.27 + 1.28 +FloatHist * 1.29 +makeFloatHistogram( int32 numBins, float32 startOfRange, float32 binWidth ) 1.30 + { 1.31 + FloatHist *hist; 1.32 + int i; 1.33 + 1.34 + hist = malloc( sizeof(FloatHist) ); 1.35 + hist->bins = malloc( numBins * sizeof(int) ); 1.36 + 1.37 + hist->numBins = numBins; 1.38 + hist->binWidth = binWidth; 1.39 + hist->endOfRange = startOfRange + hist->binWidth * numBins; 1.40 + hist->startOfRange = startOfRange; 1.41 + 1.42 + for( i = 0; i < hist->numBins; i++ ) 1.43 + { 1.44 + hist->bins[ i ] = 0; 1.45 + } 1.46 + 1.47 + return hist; 1.48 + } 1.49 + 1.50 + 1.51 +/*All values higher than or equal to a bin's start value and less than the 1.52 + * start value of the next higher are put into that bin. 1.53 + */ 1.54 +void 1.55 +addToFloatHist( float32 value, FloatHist *hist ) 1.56 + { 1.57 + int binIdx; 1.58 + 1.59 + if( value < hist->startOfRange ) 1.60 + { binIdx = 0; 1.61 + } 1.62 + else if( value > hist->endOfRange ) 1.63 + { binIdx = hist->numBins - 1; 1.64 + } 1.65 + else 1.66 + { //truncate so bin holds: binStartVal =< values < nextBinStartVal 1.67 + binIdx = (int32)((value - hist->startOfRange) / hist->binWidth); 1.68 + } 1.69 + 1.70 + hist->bins[ binIdx ] += 1; 1.71 + } 1.72 + 1.73 +void 1.74 +printFloatHist( FloatHist *hist ) 1.75 + { 1.76 + int32 binIdx, i, numBars, maxHeight; 1.77 + float32 barValue, binStart, binEnd; 1.78 + 1.79 + maxHeight = 0; 1.80 + for( i = 0; i < hist->numBins; i++ ) 1.81 + { 1.82 + if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; 1.83 + } 1.84 + barValue = maxHeight / 40; //40 spaces across page for tallest bin 1.85 + 1.86 + printf("histogram: \n"); 1.87 + if( barValue == 0 ) printf("error printing histogram\n"); 1.88 + for( binIdx = 0; binIdx < hist->numBins; binIdx++ ) 1.89 + { 1.90 + binStart = hist->startOfRange + hist->binWidth * binIdx; 1.91 + binEnd = binStart + hist->binWidth; 1.92 + printf("bin range: %3f - %3f", binStart, binEnd ); 1.93 + 1.94 + numBars = hist->bins[ binIdx ] / barValue; 1.95 + //print one bin, height of bar is num dashes across page 1.96 + for( i = 0; i < numBars; i++ ) 1.97 + { 1.98 + printf("-"); 1.99 + } 1.100 + printf("\n"); 1.101 + } 1.102 + } 1.103 +
