Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > Histogram
view Histogram.c @ 4:83a412f2ef98
Added stdio.h and copy-paste error on FreeDblHist to FreeFloatHist
| author | Me |
|---|---|
| date | Tue, 02 Nov 2010 16:47:21 -0700 |
| parents | 3d35477a5121 |
| children | a2388fae93ff |
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"
12 /*This Histogram Abstract Data Type has a number of bins plus a range of
13 * values that the bins span, both chosen at creation.
14 *
15 *One creates a Histogram instance using the makeHistogram function, then
16 * updates it with the addToHist function, and prints it out with the
17 * printHist function.
18 *
19 *Note, the bin width is an integer, so the end of the range is adjusted
20 * accordingly. Use the bin-width to calculate the bin boundaries.
21 */
24 Histogram *
25 makeHistogram( int numBins, int startOfRange, int endOfRange )
26 {
27 Histogram *hist;
28 int i;
30 hist = VMS__malloc( sizeof(Histogram) );
31 hist->bins = VMS__malloc( numBins * sizeof(int) );
33 hist->numBins = numBins;
34 hist->binWidth = (endOfRange - startOfRange) / numBins;
35 hist->endOfRange = startOfRange + hist->binWidth * numBins;
36 hist->startOfRange = startOfRange;
38 for( i = 0; i < hist->numBins; i++ )
39 {
40 hist->bins[ i ] = 0;
41 }
43 return hist;
44 }
46 void
47 addToHist( int value, Histogram *hist )
48 {
49 int binIdx;
51 if( value < hist->startOfRange )
52 { binIdx = 0;
53 }
54 else if( value > hist->endOfRange )
55 { binIdx = hist->numBins - 1;
56 }
57 else
58 {
59 binIdx = (value - hist->startOfRange) / hist->binWidth;
60 }
62 hist->bins[ binIdx ] += 1;
63 }
65 void
66 printHist( Histogram *hist )
67 {
68 int binIdx, i, numBars, maxHeight, barValue, binStart, binEnd;
70 maxHeight = 0;
71 for( i = 0; i < hist->numBins; i++ )
72 {
73 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
74 }
75 barValue = maxHeight / 60; //60 spaces across page for tallest bin
77 printf( "histogram: \n" );
78 if( barValue == 0 ) printf("error printing histogram\n");
79 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
80 {
81 binStart = hist->startOfRange + hist->binWidth * binIdx;
82 binEnd = binStart + hist->binWidth - 1;
83 printf("bin range: %d - %d", binStart, binEnd );
85 numBars = hist->bins[ binIdx ] / barValue;
86 //print one bin, height of bar is num dashes across page
87 for( i = 0; i < numBars; i++ )
88 {
89 printf("-");
90 }
91 printf("\n");
92 }
93 }
95 void
96 freeHist( Histogram *hist )
97 {
98 VMS__free( hist->bins );
99 VMS__free( hist );
100 }
