Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > Histogram
view DblHist.c @ 36:6bdcb337576b
adding netbeans project directories to repository
| author | Sean Halle <seanhalle@yahoo.com> |
|---|---|
| date | Fri, 14 Feb 2014 07:47:13 -0800 |
| parents | 8166ea441cb5 |
| children |
line source
1 /*
2 * Copyright 2010 OpenSourceResearchInstitute.org
3 * Licensed under GNU General Public License version 2
4 *
5 * Author: seanhalle@yahoo.com
6 *
7 */
9 #include <stdio.h>
10 #include <PR__include/prmalloc.h>
11 #include <PR__include/prhistogram.h>
14 /*This Histogram Abstract Data Type has a number of bins, the starting
15 * value, and the width of each bin, as a float, all chosen at creation.
16 *
17 *One creates a Histogram instance using the makeFloatHistogram function, then
18 * updates it with the addToFloatHist function, and prints it out with the
19 * printFloatHist function.
20 *
21 *Note, the bin width is an integer, so the end of the range is adjusted
22 * accordingly. Use the bin-width to calculate the bin boundaries.
23 */
26 DblHist *
27 makeDblHistogram( int32 numBins, float64 startOfRange, float64 binWidth )
28 {
29 DblHist *hist;
30 int i;
32 hist = PR__malloc( sizeof(DblHist) );
33 hist->bins = PR__malloc( numBins * sizeof(int) );
35 hist->numBins = numBins;
36 hist->binWidth = binWidth;
37 hist->endOfRange = startOfRange + hist->binWidth * numBins;
38 hist->startOfRange = startOfRange;
40 for( i = 0; i < hist->numBins; i++ )
41 {
42 hist->bins[ i ] = 0;
43 }
45 return hist;
46 }
49 /*All values higher than or equal to a bin's start value and less than the
50 * start value of the next higher are put into that bin.
51 */
52 void
53 addToDblHist( float64 value, DblHist *hist )
54 {
55 int binIdx;
57 if( value < hist->startOfRange )
58 { binIdx = 0;
59 }
60 else if( value > hist->endOfRange )
61 { binIdx = hist->numBins - 1;
62 }
63 else
64 { //truncate so bin holds: binStartVal =< values < nextBinStartVal
65 binIdx = (int32)((value - hist->startOfRange) / hist->binWidth);
66 }
68 hist->bins[ binIdx ] += 1;
69 }
71 void
72 printDblHist( DblHist *hist )
73 {
74 int32 binIdx, i, numBars, maxHeight;
75 float64 barValue, binStart, binEnd;
77 maxHeight = 0;
78 for( i = 0; i < hist->numBins; i++ )
79 {
80 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
81 }
82 barValue = maxHeight / 40; //40 spaces across page for tallest bin
84 printf( "histogram: \n" );
85 if( barValue == 0 ) printf( "error printing histogram\n" );
86 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
87 {
88 binStart = hist->startOfRange + hist->binWidth * binIdx;
89 binEnd = binStart + hist->binWidth;
90 printf( "bin range: %.6fl - %.6fl", binStart, binEnd );
92 numBars = hist->bins[ binIdx ] / barValue;
93 //print one bin, height of bar is num dashes across page
94 for( i = 0; i < numBars; i++ )
95 {
96 printf("-");
97 }
98 printf("\n");
99 }
100 }
103 void
104 freeDblHist( DblHist *hist )
105 {
106 PR__free( hist->bins );
107 PR__free( hist );
108 }
