Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > Histogram
comparison FloatHist.c @ 2:06128e387cfa
Added DblHist and FloatHist
| author | Me |
|---|---|
| date | Sat, 30 Oct 2010 22:13:09 -0700 |
| parents | |
| children | 3d35477a5121 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:3d52e900a017 |
|---|---|
| 1 /* | |
| 2 * Copyright 2010 OpenSourceStewardshipFoundation.org | |
| 3 * Licensed under GNU General Public License version 2 | |
| 4 * | |
| 5 * Author: seanhalle@yahoo.com | |
| 6 * | |
| 7 */ | |
| 8 | |
| 9 #include "Histogram.h" | |
| 10 #include <malloc.h> | |
| 11 | |
| 12 | |
| 13 /*This Histogram Abstract Data Type has a number of bins, the starting | |
| 14 * value, and the width of each bin, as a float, all chosen at creation. | |
| 15 * | |
| 16 *One creates a Histogram instance using the makeFloatHistogram function, then | |
| 17 * updates it with the addToFloatHist function, and prints it out with the | |
| 18 * printFloatHist function. | |
| 19 * | |
| 20 *Note, the bin width is an integer, so the end of the range is adjusted | |
| 21 * accordingly. Use the bin-width to calculate the bin boundaries. | |
| 22 */ | |
| 23 | |
| 24 | |
| 25 FloatHist * | |
| 26 makeFloatHistogram( int32 numBins, float32 startOfRange, float32 binWidth ) | |
| 27 { | |
| 28 FloatHist *hist; | |
| 29 int i; | |
| 30 | |
| 31 hist = malloc( sizeof(FloatHist) ); | |
| 32 hist->bins = malloc( numBins * sizeof(int) ); | |
| 33 | |
| 34 hist->numBins = numBins; | |
| 35 hist->binWidth = binWidth; | |
| 36 hist->endOfRange = startOfRange + hist->binWidth * numBins; | |
| 37 hist->startOfRange = startOfRange; | |
| 38 | |
| 39 for( i = 0; i < hist->numBins; i++ ) | |
| 40 { | |
| 41 hist->bins[ i ] = 0; | |
| 42 } | |
| 43 | |
| 44 return hist; | |
| 45 } | |
| 46 | |
| 47 | |
| 48 /*All values higher than or equal to a bin's start value and less than the | |
| 49 * start value of the next higher are put into that bin. | |
| 50 */ | |
| 51 void | |
| 52 addToFloatHist( float32 value, FloatHist *hist ) | |
| 53 { | |
| 54 int binIdx; | |
| 55 | |
| 56 if( value < hist->startOfRange ) | |
| 57 { binIdx = 0; | |
| 58 } | |
| 59 else if( value > hist->endOfRange ) | |
| 60 { binIdx = hist->numBins - 1; | |
| 61 } | |
| 62 else | |
| 63 { //truncate so bin holds: binStartVal =< values < nextBinStartVal | |
| 64 binIdx = (int32)((value - hist->startOfRange) / hist->binWidth); | |
| 65 } | |
| 66 | |
| 67 hist->bins[ binIdx ] += 1; | |
| 68 } | |
| 69 | |
| 70 void | |
| 71 printFloatHist( FloatHist *hist ) | |
| 72 { | |
| 73 int32 binIdx, i, numBars, maxHeight; | |
| 74 float32 barValue, binStart, binEnd; | |
| 75 | |
| 76 maxHeight = 0; | |
| 77 for( i = 0; i < hist->numBins; i++ ) | |
| 78 { | |
| 79 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; | |
| 80 } | |
| 81 barValue = maxHeight / 40; //40 spaces across page for tallest bin | |
| 82 | |
| 83 printf("histogram: \n"); | |
| 84 if( barValue == 0 ) printf("error printing histogram\n"); | |
| 85 for( binIdx = 0; binIdx < hist->numBins; binIdx++ ) | |
| 86 { | |
| 87 binStart = hist->startOfRange + hist->binWidth * binIdx; | |
| 88 binEnd = binStart + hist->binWidth; | |
| 89 printf("bin range: %3f - %3f", binStart, binEnd ); | |
| 90 | |
| 91 numBars = hist->bins[ binIdx ] / barValue; | |
| 92 //print one bin, height of bar is num dashes across page | |
| 93 for( i = 0; i < numBars; i++ ) | |
| 94 { | |
| 95 printf("-"); | |
| 96 } | |
| 97 printf("\n"); | |
| 98 } | |
| 99 } | |
| 100 |
