changeset 0:6e864a0cb520

Initial add
author Me
date Sat, 11 Sep 2010 04:41:55 -0700
parents
children dbb58ebfd690
files Histogram.c Histogram.h
diffstat 2 files changed, 128 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Histogram.c	Sat Sep 11 04:41:55 2010 -0700
     1.3 @@ -0,0 +1,95 @@
     1.4 +/*
     1.5 + *  Copyright 2010 OpenSourceStewardshipFoundation.org
     1.6 + *  Licensed under GNU General Public License version 2
     1.7 + *
     1.8 + * Author: seanhalle@yahoo.com
     1.9 + *
    1.10 + */
    1.11 +
    1.12 +#include "Histogram.h"
    1.13 +#include <malloc.h>
    1.14 +
    1.15 +
    1.16 +/*This Histogram Abstract Data Type has a number of bins plus a range of
    1.17 + * values that the bins span, both chosen at creation.
    1.18 + *
    1.19 + *One creates a Histogram instance using the makeHistogram function, then
    1.20 + * updates it with the addToHist function, and prints it out with the
    1.21 + * printHist function.
    1.22 + *
    1.23 + *Note, the bin width is an integer, so the end of the range is adjusted
    1.24 + * accordingly.  Use the bin-width to calculate the bin boundaries.
    1.25 + */
    1.26 +
    1.27 +
    1.28 +Histogram *
    1.29 +makeHistogram( int numBins, int startOfRange, int endOfRange )
    1.30 + {
    1.31 +   Histogram *hist;
    1.32 +   int i;
    1.33 +
    1.34 +   hist = malloc( sizeof(Histogram) );
    1.35 +   hist->bins = malloc( numBins * sizeof(int) );
    1.36 +
    1.37 +   hist->numBins      = numBins;
    1.38 +   hist->binWidth     = (endOfRange - startOfRange) / numBins;
    1.39 +   hist->endOfRange   = startOfRange + hist->binWidth * numBins;
    1.40 +   hist->startOfRange = startOfRange;
    1.41 +
    1.42 +   for( i = 0; i < hist->numBins; i++ )
    1.43 +    {
    1.44 +      hist->bins[ i ] = 0;
    1.45 +    }
    1.46 +
    1.47 +   return hist;
    1.48 + }
    1.49 +
    1.50 +void
    1.51 +addToHist( int value, Histogram *hist )
    1.52 + {
    1.53 +   int binIdx;
    1.54 +
    1.55 +   if( value < hist->startOfRange )
    1.56 +    { binIdx = 0;
    1.57 +    }
    1.58 +   else if( value > hist->endOfRange )
    1.59 +    { binIdx = hist->numBins - 1;
    1.60 +    }
    1.61 +   else
    1.62 +    {
    1.63 +      binIdx = (value - hist->startOfRange) / hist->binWidth;
    1.64 +    }
    1.65 +
    1.66 +   hist->bins[ binIdx ] += 1;
    1.67 + }
    1.68 +
    1.69 +void
    1.70 +printHist( Histogram *hist )
    1.71 + {
    1.72 +   int binIdx, i, numBars, maxHeight, barValue, binStart, binEnd;
    1.73 +
    1.74 +   maxHeight = 0;
    1.75 +   for( i = 0; i < hist->numBins; i++ )
    1.76 +    {
    1.77 +      if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
    1.78 +    }
    1.79 +   barValue = maxHeight / 60;  //60 spaces across page for tallest bin
    1.80 +
    1.81 +   printf("histogram: \n");
    1.82 +   if( barValue == 0 ) printf("error printing histogram\n");
    1.83 +   for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
    1.84 +    {
    1.85 +      binStart = hist->startOfRange + hist->binWidth * binIdx;
    1.86 +      binEnd = binStart + hist->binWidth - 1;
    1.87 +      printf("bin range: %d - %d", binStart, binEnd );
    1.88 +
    1.89 +      numBars = hist->bins[ binIdx ] / barValue;
    1.90 +         //print one bin, height of bar is num dashes across page
    1.91 +      for( i = 0; i < numBars; i++ )
    1.92 +       {
    1.93 +         printf("-");
    1.94 +       }
    1.95 +      printf("\n");
    1.96 +    }
    1.97 + }
    1.98 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Histogram.h	Sat Sep 11 04:41:55 2010 -0700
     2.3 @@ -0,0 +1,33 @@
     2.4 +/*
     2.5 + *  Copyright 2010 OpenSourceStewardshipFoundation.org
     2.6 + *  Licensed under GNU General Public License version 2
     2.7 + *
     2.8 + * Author: seanhalle@yahoo.com
     2.9 + *
    2.10 + */
    2.11 +
    2.12 +
    2.13 +#ifndef _HISTOGRAM_H
    2.14 +#define	_HISTOGRAM_H
    2.15 +
    2.16 +typedef struct
    2.17 + {
    2.18 +   int  startOfRange;
    2.19 +   int  endOfRange;
    2.20 +   int  numBins;
    2.21 +   int  binWidth;
    2.22 +   int *bins;
    2.23 + }
    2.24 +Histogram;
    2.25 +
    2.26 +Histogram *
    2.27 +makeHistogram( int numBins, int startOfRange, int endOfRange );
    2.28 +
    2.29 +void
    2.30 +addToHist( int value, Histogram *hist );
    2.31 +
    2.32 +void
    2.33 +printHist( Histogram *hist );
    2.34 +
    2.35 +#endif	/* _HISTOGRAM_H */
    2.36 +