Histogram.cc
changeset 0 6b8091ca909a
equal deleted inserted replaced
-1:000000000000 0:6b8091ca909a
       
     1 #include "Histogram.h"
       
     2 #include <math.h>
       
     3 
       
     4 inline double sqr(double x) {return(x*x);}
       
     5 
       
     6 void Histogram::calc_mean()
       
     7 {
       
     8 	int total_weight = 0;
       
     9 	int i;
       
    10 	for(i = 0; i < rows; i++)
       
    11 		total_weight = total_weight + weights[i];
       
    12 	mean = (double) total_weight / (double) rows;
       
    13 }
       
    14 
       
    15 void Histogram::calc_variance()
       
    16 {
       
    17 	double sum = 0;
       
    18 	for(int i = 0; i < rows; i++)
       
    19 		sum += sqr(weights[i] - mean);
       
    20 	variance = (double) sum / (double) rows;
       
    21 }
       
    22 
       
    23 void Histogram::calc_std_dev()
       
    24 {
       
    25 	std_dev = sqrt(variance);
       
    26 }
       
    27 
       
    28 void Histogram::calcStatistics()
       
    29 {
       
    30 	calc_mean();
       
    31 	calc_variance();
       
    32 	calc_std_dev();
       
    33 }
       
    34 
       
    35 Histogram::Histogram(int numrows)
       
    36 {
       
    37 	rows = numrows;
       
    38 	weights = new int[numrows];
       
    39 }
       
    40 
       
    41 Histogram::~Histogram()
       
    42 {
       
    43 	delete[] weights;
       
    44 }