Main Page   Namespace List   Compound List   File List   Compound Members   File Members  

Statistics.cc

Go to the documentation of this file.
00001 // Statistics.cc
00002 
00003 /*
00004 
00005  Statistics.cc contains the source code for the Statistics class
00006  which consists of the definitions of all of the class functions
00007  that are not already defined in Statistics.hh
00008 
00009 */
00010 
00011 #include<stdlib.h>
00012 #include<math.h>
00013 
00014 
00015 
00016 // Always include the header file for the class!!
00017 
00018 #include "Statistics.hh"
00019 
00020 Statistics::Statistics(int arrayLengthIn, double *arrayIn)
00021   {
00022 
00023     // Initialize all private data members of the class
00024 
00025     arrayLength = arrayLengthIn;
00026     array = arrayIn;
00027 
00028     // To call a class function from within the class you just 
00029     // call it like you call a function in C
00030 
00031     ComputeMinimum(); 
00032     ComputeMaximum();
00033     ComputeMean();
00034     ComputeVariance();
00035 
00036     standardDeviation = sqrt(variance);
00037 
00038     // Dynamically allocate memory of size arrayLength for sortedArray
00039 
00040     sortedArray = new double[arrayLength];
00041 
00042     ComputeMedian();
00043 
00044   }// ## end Statistics constructor
00045 
00046 
00047 // ########################################################
00048 
00049 Statistics::~Statistics()
00050   {
00051 
00052     // Delete memory that was dynamically allocated in the constructor
00053 
00054     delete [] sortedArray;    
00055   }
00056   
00057 // ########################################################
00058 
00059 void Statistics::ComputeMinimum(void)
00060 {
00061 
00062   minimum = array[0];
00063 
00064   for(int i=0;i<arrayLength;i++)
00065     {
00066       if(array[i] < minimum)
00067          minimum = array[i];
00068     }
00069 
00070 } // ## end ComputeMinimum
00071 
00072 // ########################################################
00073 
00074 void Statistics::ComputeMaximum(void)
00075 {
00076 
00077   maximum = array[0];
00078 
00079   for(int i=0;i<arrayLength;i++)
00080     {
00081       if(array[i] > maximum)
00082          maximum = array[i];
00083     }
00084 
00085 } // ## end ComputeMaximum
00086 
00087 // ########################################################
00088 
00089 void Statistics::ComputeMean(void)
00090 {
00091 
00092   mean = 0.0;
00093 
00094   for(int i=0;i<arrayLength;i++)
00095      mean += array[i];
00096 
00097   mean = mean/(double(arrayLength));
00098 
00099 } // ## end ComputeMean
00100 
00101 // #####################################################
00102 
00103 void Statistics::ComputeMeanOfSquares(void)
00104 {
00105 
00106   meanOfSquares = 0.0;
00107 
00108   for(int i=0;i<arrayLength;i++)
00109     meanOfSquares += array[i]*array[i];
00110 
00111   meanOfSquares = meanOfSquares/(double(arrayLength));
00112 
00113 } // ## end ComputeMeanSquares
00114 
00115 // ########################################################
00116 
00117 void Statistics::ComputeVariance(void)
00118 {  
00119  // This gives an "unbiased" computation of variance
00120 
00121   // Make sure we compute mean and meanOfSquares first!
00122 
00123   ComputeMean();
00124   ComputeMeanOfSquares();
00125 
00126   variance =  double(arrayLength)*(meanOfSquares-mean*mean)/ 
00127               double(arrayLength-1.0);
00128 
00129 } // ## end ComputeVariance
00130 
00131 // ########################################################
00132 
00133 void Statistics::ComputeMedian(void)
00134 {
00135   
00136     SelectionSort();
00137 
00138     if(arrayLength%2) // true if arrayLength is odd
00139       median = sortedArray[(arrayLength+1)/2 - 1];
00140     else // arrayLength even
00141       median = (sortedArray[arrayLength/2 - 1] + 
00142                 sortedArray[arrayLength/2])/2.0;
00143 
00144 } // ## end Median
00145 
00146 // #############################################
00147 
00148 void Statistics::SelectionSort(void)
00149 {
00150 
00151   for(int ii=0;ii<arrayLength;ii++)
00152     sortedArray[ii] = array[ii];
00153 
00154   for(int SubLength = arrayLength; SubLength >=1; SubLength --)
00155     {
00156       int Index = MaximumIndex(SubLength);
00157 
00158       if(Index < SubLength - 1) // If Index == SubLength do nothing
00159         {
00160           double Temp = sortedArray[SubLength-1];
00161           sortedArray[SubLength-1] = sortedArray[Index];
00162           sortedArray[Index] = Temp;
00163         } // ## end if
00164 
00165     } // ## end for
00166 
00167 } // ## end SelectionSort
00168 
00169 // ##############################################
00170 
00171 int Statistics::MaximumIndex(int SubLength)
00172 {
00173 
00174   double Max = array[0];
00175   int MaxIndex = 0;
00176 
00177   for(int ii=0;ii<SubLength;ii++)
00178     {
00179       if(array[ii] > Max)
00180         {
00181           Max = array[ii];
00182           MaxIndex = ii;
00183         }
00184     }
00185 
00186   return MaxIndex;
00187 
00188 } // ## end MaximumIndex
00189 
00190 // ##########################################
00191 
00192 
00193 
00194 
00195 
00196 
00197 

Generated at Fri Feb 21 10:29:46 2003 for hello by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000