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