| rev |
line source |
|
Me@0
|
1 /*
|
|
Me@0
|
2 * Copyright 2010 OpenSourceStewardshipFoundation.org
|
|
Me@0
|
3 * Licensed under GNU General Public License version 2
|
|
Me@0
|
4 *
|
|
Me@0
|
5 * Author: seanhalle@yahoo.com
|
|
Me@0
|
6 *
|
|
Me@0
|
7 */
|
|
Me@4
|
8 #include <stdio.h>
|
|
Me@0
|
9 #include "Histogram.h"
|
|
Me@0
|
10
|
|
Me@0
|
11
|
|
Me@0
|
12 /*This Histogram Abstract Data Type has a number of bins plus a range of
|
|
Me@0
|
13 * values that the bins span, both chosen at creation.
|
|
Me@0
|
14 *
|
|
Me@0
|
15 *One creates a Histogram instance using the makeHistogram function, then
|
|
Me@0
|
16 * updates it with the addToHist function, and prints it out with the
|
|
Me@0
|
17 * printHist function.
|
|
Me@0
|
18 *
|
|
Me@0
|
19 *Note, the bin width is an integer, so the end of the range is adjusted
|
|
Me@0
|
20 * accordingly. Use the bin-width to calculate the bin boundaries.
|
|
Me@0
|
21 */
|
|
Me@0
|
22
|
|
Me@0
|
23
|
|
Me@0
|
24 Histogram *
|
|
Me@0
|
25 makeHistogram( int numBins, int startOfRange, int endOfRange )
|
|
Me@0
|
26 {
|
|
Me@0
|
27 Histogram *hist;
|
|
Me@0
|
28 int i;
|
|
Me@0
|
29
|
|
Me@3
|
30 hist = VMS__malloc( sizeof(Histogram) );
|
|
Me@3
|
31 hist->bins = VMS__malloc( numBins * sizeof(int) );
|
|
Me@0
|
32
|
|
Me@0
|
33 hist->numBins = numBins;
|
|
Me@0
|
34 hist->binWidth = (endOfRange - startOfRange) / numBins;
|
|
Me@0
|
35 hist->endOfRange = startOfRange + hist->binWidth * numBins;
|
|
Me@0
|
36 hist->startOfRange = startOfRange;
|
|
Me@0
|
37
|
|
Me@0
|
38 for( i = 0; i < hist->numBins; i++ )
|
|
Me@0
|
39 {
|
|
Me@0
|
40 hist->bins[ i ] = 0;
|
|
Me@0
|
41 }
|
|
Me@0
|
42
|
|
Me@0
|
43 return hist;
|
|
Me@0
|
44 }
|
|
Me@0
|
45
|
|
Me@0
|
46 void
|
|
Me@0
|
47 addToHist( int value, Histogram *hist )
|
|
Me@0
|
48 {
|
|
Me@0
|
49 int binIdx;
|
|
Me@0
|
50
|
|
Me@0
|
51 if( value < hist->startOfRange )
|
|
Me@0
|
52 { binIdx = 0;
|
|
Me@0
|
53 }
|
|
Me@0
|
54 else if( value > hist->endOfRange )
|
|
Me@0
|
55 { binIdx = hist->numBins - 1;
|
|
Me@0
|
56 }
|
|
Me@0
|
57 else
|
|
Me@0
|
58 {
|
|
Me@0
|
59 binIdx = (value - hist->startOfRange) / hist->binWidth;
|
|
Me@0
|
60 }
|
|
Me@0
|
61
|
|
Me@0
|
62 hist->bins[ binIdx ] += 1;
|
|
Me@0
|
63 }
|
|
Me@0
|
64
|
|
Me@0
|
65 void
|
|
Me@0
|
66 printHist( Histogram *hist )
|
|
Me@0
|
67 {
|
|
Me@0
|
68 int binIdx, i, numBars, maxHeight, barValue, binStart, binEnd;
|
|
Me@0
|
69
|
|
Me@0
|
70 maxHeight = 0;
|
|
Me@0
|
71 for( i = 0; i < hist->numBins; i++ )
|
|
Me@0
|
72 {
|
|
Me@0
|
73 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
|
|
Me@0
|
74 }
|
|
Me@0
|
75 barValue = maxHeight / 60; //60 spaces across page for tallest bin
|
|
Me@0
|
76
|
|
Me@4
|
77 printf( "histogram: \n" );
|
|
Me@0
|
78 if( barValue == 0 ) printf("error printing histogram\n");
|
|
Me@0
|
79 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
|
|
Me@0
|
80 {
|
|
Me@0
|
81 binStart = hist->startOfRange + hist->binWidth * binIdx;
|
|
Me@0
|
82 binEnd = binStart + hist->binWidth - 1;
|
|
Me@0
|
83 printf("bin range: %d - %d", binStart, binEnd );
|
|
Me@0
|
84
|
|
Me@0
|
85 numBars = hist->bins[ binIdx ] / barValue;
|
|
Me@0
|
86 //print one bin, height of bar is num dashes across page
|
|
Me@0
|
87 for( i = 0; i < numBars; i++ )
|
|
Me@0
|
88 {
|
|
Me@0
|
89 printf("-");
|
|
Me@0
|
90 }
|
|
Me@0
|
91 printf("\n");
|
|
Me@0
|
92 }
|
|
Me@0
|
93 }
|
|
Me@0
|
94
|
|
Me@3
|
95 void
|
|
Me@3
|
96 freeHist( Histogram *hist )
|
|
Me@3
|
97 {
|
|
Me@3
|
98 VMS__free( hist->bins );
|
|
Me@3
|
99 VMS__free( hist );
|
|
Me@3
|
100 }
|