Software & Finance





C++ - Calculate Mean, Variance, Standard Deviation





Mean, Variance and Standard Deviation are widely used in statistical application. It is a good idea to start writing program in C++ on this.

Note the difference between sample variance and population variance, similarly sample standard deviation and population standard deviation

The complete program and test run output are given below:



Source Code


#include <iostream>
#include <string>
#include <math.h>

class
StdDeviation
{

private:

    int max;

    double value[100];

 

    double mean;

   

public:

 

    double CalculateMean()

    {

        double sum = 0;

        for(int i = 0; i < max; i++)

            sum += value[i];

        return (sum / max);

    }

 

    double CalculateVariane()

    {

        mean = CalculateMean();

 

        double temp = 0;

        for(int i = 0; i < max; i++)

        {

             temp += (value[i] - mean) * (value[i] - mean) ;

        }

        return temp / max;

    }

 

    double CalculateSampleVariane()

    {

        mean = CalculateMean();

 

        double temp = 0;

        for(int i = 0; i < max; i++)

        {

             temp += (value[i] - mean) * (value[i] - mean) ;

        }

        return temp / (max - 1);

    }

 

    int SetValues(double *p, int count)

    {

        if(count > 100)

            return -1;

        max = count;

        for(int i = 0; i < count; i++)

            value[i] = p[i];

        return 0;

    }

 

    double GetStandardDeviation()

    {

        return sqrt(CalculateVariane());

    }

 

    double GetSampleStandardDeviation()

    {

        return sqrt(CalculateSampleVariane());

    }

 

};

 

int main()

{

    double arrNumbers[] =

    {

        15.17, 16.94, 14.94, 14.99, 13.77, 13.75,

        12.67, 12.14, 12.59, 12.48, 14.81, 14.29,

        12.74, 12.52, 11.65, 12.24, 11.42, 12.25,

        12.72, 11.64, 11.09, 11.22, 11.50, 11.36,

        11.84, 12.18, 11.04, 10.90, 11.80, 11.84,

    };

 

    StdDeviation sd;

 

    sd.SetValues(arrNumbers, sizeof(arrNumbers) / sizeof(arrNumbers[0]));

 

    double mean = sd.CalculateMean();

    double variance = sd.CalculateVariane();

    double samplevariance = sd.CalculateSampleVariane();

    double sampledevi = sd.GetSampleStandardDeviation();

    double devi = sd.GetStandardDeviation();

 

    char buf[1024];

    sprintf(buf, "Total Numbers\t\t\t: %10d\n", sizeof(arrNumbers) / sizeof(arrNumbers[0]));

    std::cout << buf;

    sprintf(buf, "Mean\t\t\t\t: %10.5lf\nPopulation Variance\t\t: %10.4lf\n", mean, variance);

    std::cout << buf;

    sprintf(buf, "Sample variance\t\t\t: %10.4lf\n", samplevariance);

    std::cout << buf;

    sprintf(buf, "Population Standard Deviation\t: %10.4lf\n", devi);

    std::cout << buf;

    sprintf(buf, "Sample Standard Deviation\t: %10.4lf\n", sampledevi);

    std::cout << buf;

       

    return 0;

}

Output


Total Numbers                   :         30

Mean                            :   12.68300

Population Variance             :     2.1607

Sample variance                 :     2.2352

Population Standard Deviation   :     1.4699

Sample Standard Deviation       :     1.4951

 

Press any key to continue . . .