comparison Histogram.c @ 6:a2388fae93ff

Merge addInterval version with VMS__malloc version
author SeanHalle
date Thu, 11 Nov 2010 05:45:08 -0800
parents 13b8591dd045 83a412f2ef98
children fa6a281bd854
comparison
equal deleted inserted replaced
3:0cf30147687a 4:bd856c869be4
3 * Licensed under GNU General Public License version 2 3 * Licensed under GNU General Public License version 2
4 * 4 *
5 * Author: seanhalle@yahoo.com 5 * Author: seanhalle@yahoo.com
6 * 6 *
7 */ 7 */
8 8 #include <stdio.h>
9 #include "Histogram.h" 9 #include "Histogram.h"
10 #include <malloc.h>
11 10
12 11
13 /*This Histogram Abstract Data Type has a number of bins plus a range of 12 /*This Histogram Abstract Data Type has a number of bins plus a range of
14 * values that the bins span, both chosen at creation. 13 * values that the bins span, both chosen at creation.
15 * 14 *
22 */ 21 */
23 22
24 23
25 Histogram * 24 Histogram *
26 makeHistogram( int32 numBins, int32 startOfRange, int32 endOfRange ) 25 makeHistogram( int32 numBins, int32 startOfRange, int32 endOfRange )
26
27 { 27 {
28 Histogram *hist; 28 Histogram *hist;
29 int32 i; 29 int32 i;
30 30
31 hist = malloc( sizeof(Histogram) ); 31
32 hist->bins = malloc( numBins * sizeof(int32) ); 32 hist = VMS__malloc( sizeof(Histogram) );
33 hist->bins = VMS__malloc( numBins * sizeof(int32) );
33 34
34 hist->numBins = numBins; 35 hist->numBins = numBins;
35 hist->binWidth = (endOfRange - startOfRange) / numBins; 36 hist->binWidth = (endOfRange - startOfRange) / numBins;
36 hist->endOfRange = startOfRange + hist->binWidth * numBins; 37 hist->endOfRange = startOfRange + hist->binWidth * numBins;
37 hist->startOfRange = startOfRange; 38 hist->startOfRange = startOfRange;
77 void 78 void
78 printHist( Histogram *hist ) 79 printHist( Histogram *hist )
79 { 80 {
80 int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd; 81 int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd;
81 82
83
82 maxHeight = 0; 84 maxHeight = 0;
83 for( i = 0; i < hist->numBins; i++ ) 85 for( i = 0; i < hist->numBins; i++ )
84 { 86 {
85 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; 87 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
86 } 88 }
87 barValue = maxHeight / 60; //60 spaces across page for tallest bin 89 barValue = maxHeight / 60; //60 spaces across page for tallest bin
88 90
89 printf("histogram: \n"); 91 printf( "histogram: \n" );
90 if( barValue == 0 ) printf("error printing histogram\n"); 92 if( barValue == 0 ) printf("error printing histogram\n");
91 for( binIdx = 0; binIdx < hist->numBins; binIdx++ ) 93 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
92 { 94 {
93 binStart = hist->startOfRange + hist->binWidth * binIdx; 95 binStart = hist->startOfRange + hist->binWidth * binIdx;
94 binEnd = binStart + hist->binWidth - 1; 96 binEnd = binStart + hist->binWidth - 1;
102 } 104 }
103 printf("\n"); 105 printf("\n");
104 } 106 }
105 } 107 }
106 108
109 void
110 freeHist( Histogram *hist )
111 {
112 VMS__free( hist->bins );
113 VMS__free( hist );
114 }