Software & Finance - Monthly Magazine Online

Volume 2 - Issue 3

March 2012

March 2012 - Technology - Calculate Standard Deviation


I have given the simple C program for calculating sample and population standard deviation for a set of numbers. The other related linkes are,

Calculate Mean

Calculate Median

Calculate Mode

Calculate Range

Calculate Variance

Calculate Standard Deviation


Source Code Listing for Calculating Standard Deviation

#include 
#include 
#include 
 
 
typedef struct _Pair
{
    int key;
    int value;
} Pair;
 
typedef struct _Collection
{
    Pair m_pair[100];
    int m_maxElements;
} Collection;
 
const char* CalculateMode(float *arrValue, int max)
{
    static char mode[256] = "";
    char buf[32];
    float value[100];
    int i, j;
    int highcount = 0;
    float temp;
    int nIndex = -1;
    Collection map;
    Pair ptemp;
 
    memset(map.m_pair, 0, sizeof(Pair) * 100);
    map.m_maxElements = 0;
 
    if(max > 100)
        return 0;
 
    for(i = 0; i < max; i++)
        value[i] = arrValue[i];
    for(i = 0; i < max; i++)
    {           
        for(j = 0; j < max - i - 1; j++)
        {
                if(value[j] > value[j + 1])
                {
                    temp = value[j];
                    value[j] = value[j + 1];
                    value[j + 1] = temp;
                }
        }
    }
 
    for(i = 0; i < max; i++)
    {
        // Insert arrValue[i] to the map
        nIndex = -1;
        for(j = 0; j < map.m_maxElements; j++)
        {
            if(map.m_pair[j].key == value[i])
            {
                nIndex = j;
                break;
            }
        }
        if(nIndex == -1)
        {
            map.m_pair[map.m_maxElements].key = value[i];
            map.m_pair[map.m_maxElements].value = 1;
            map.m_maxElements++;
        }
        else
        {
            // map.m_pair[nIndex].key = value[i]; // alreday written
            map.m_pair[nIndex].value++;
        }
    }
   
    for(i = 0; i < map.m_maxElements; i++)
    {           
        for(j = 0; j < map.m_maxElements - i - 1; j++)
        {
            if(map.m_pair[j].value < map.m_pair[j + 1].value)
                {
                ptemp = map.m_pair[j];
                    map.m_pair[j] = map.m_pair[j + 1];
                    map.m_pair[j + 1] = ptemp;
                }
        }
    }
 
    highcount = map.m_pair[0].value;
 
    for(i = 0; i < map.m_maxElements; i++)
    {
        if(highcount == map.m_pair[i].value)
        {
            sprintf(buf, "%d ", map.m_pair[i].key);
            strcat(mode, buf);
        }
        //std::cout << map.m_pair[i].key << " " << map.m_pair[i].value << "\n";
    }
 
    return mode;
}
 
 
 
float CalculateMedian(float *arrValue, int max)
{
    float median = 0;
    float value[100];
    int i, j;
    float temp;
    if(max > 100)
        return 0;
    for(i = 0; i < max; i++)
        value[i] = arrValue[i];
    for(i = 0; i < max; i++)
    {           
        for(j = 0; j < max - i - 1; j++)
        {
                if(value[j] > value[j + 1])
                {
                    temp = value[j];
                    value[j] = value[j + 1];
                    value[j + 1] = temp;
                }
        }
    }
    if( (max % 2) == 1)
    {
        median =  value[ (max + 1) / 2 - 1];
    }
    else
    {
        median = (value[max / 2] + value[max / 2 - 1]) / 2;
    }
    return median;
}
 
const char* CalculateRange(float *arrValue, int max)
{
    static char range[128];
    int i, small, big;
    small = big = arrValue[0];
    for(i = 0; i < max; i++)
    {           
        if(arrValue[i] > big)
            big = arrValue[i];
        if(arrValue[i] < small)
            small = arrValue[i];
    }
   
    sprintf(range, "%d - %d", small, big);
    return range;
}
 
 
 
 
 
float CalculateMean(float *value, int max)
{
    int i;
    float sum = 0;
 
    for( i = 0; i < max; i++)
        sum = sum + value[i];
    return (sum / max);
}
 
 
 
float CalculateVariane(float *value, int max)
{
    float mean = CalculateMean(value, max);
    int i = 0;
    float temp = 0;
 
    for(i = 0; i < max; i++)
         temp += (value[i] - mean) * (value[i] - mean) ;
 
    return temp / max;
}
 
float CalculateSampleVariane(float *value, int max)
{
    float mean = CalculateMean(value, max);
    float temp = 0;
    int i = 0;
 
    for(i = 0; i < max; i++)
         temp += (value[i] - mean) * (value[i] - mean) ;
 
    return temp / (max - 1);
}
 
 
 
float GetStandardDeviation(float *value, int max)
{
    return sqrt(CalculateVariane(value, max));
}
 
 
float GetSampleStandardDeviation(float *value, int max)
{
    return sqrt(CalculateSampleVariane(value, max));
}
 
int main()
{
    float arrNumbers[100];
    int i, max;
    float mean, devi, sampledevi;
    float median;
    const char *mode, *range;
    char buf[1024];
    float variance, samplevariance;
 
 
    printf("Total Number of Elements: ");
    scanf("%d", &max);
 
    for(i = 0; i < max; i++)
    {
       printf("Enter [%d] Number: ", i + 1);
       scanf("%f", &arrNumbers[i]);
    }
 
    printf("\nTotal Numbers: %d", max);
 
    mean = CalculateMean(arrNumbers, max);
    mode = CalculateMode(arrNumbers, max);
    median = CalculateMedian(arrNumbers, max);
    range = CalculateRange(arrNumbers, max);
    variance = CalculateVariane(arrNumbers, max);
    samplevariance = CalculateSampleVariane(arrNumbers, max);
    devi = GetStandardDeviation(arrNumbers, max);
    sampledevi = GetSampleStandardDeviation(arrNumbers, max);
 
    printf("\nMean: %f", mean);
    printf("\nMedian: %f", median);
    printf("\nMode: %s", mode);
    printf("\nRange: %s", range);
    printf("\nSample Variance: %f", samplevariance);
    printf("\nPopulation Variance: %f",  variance);
    printf("\nSample Stdandrd Deviation: %f", sampledevi);
    printf("\nPopulation Standard Deviation: %f", devi);
 
    return 0;
}

Output


Total Number of Elements: 10

Enter [1] Number: 6

Enter [2] Number: 7

Enter [3] Number: 8

Enter [4] Number: 7

Enter [5] Number: 6

Enter [6] Number: 5

Enter [7] Number: 2

Enter [8] Number: 2

Enter [9] Number: 9

Enter [10] Number: 3

 

Total Numbers: 10

 

Mean: 5.500000

Median: 6.000000

Mode: 2 6 7

Range: 2 - 9

Sample Variance: 6.055555

Population Variance: 5.450000

Sample Stdandrd Deviation: 2.460804

Population Standard Deviation: 2.334523