1. C Program To Find Mean Variance And Standard Deviation Equation
  2. C Program To Find Mean Variance And Standard Deviation Formula
  3. C Program To Find Mean Median Mode Standard Deviation And Variance
  4. C Program To Find Mean Variance And Standard Deviation Of One
  5. C Program To Find Mean Variance And Standard Deviation Calculator


  1. Write 80387 ALP to obtain: i) Mean ii) Variance iii) Standard Deviation Also plot the histogram for the data set. The data elements are available in a text file Code.
  2. This C program calculates the Mean, Variance, and Standard Deviation of the given numbers in an array using Functions. We have already seen in the above example. There are a lot of for loops and many calculations. It is often confusing to put it all together, so in this program, we separated the calculation using the Function concept.

Here in this c program, we need to find out mean variance and standard deviation of n numbers, for that we need to know what is meant by mean, standard deviation and variance. Mean: it is the average of a number of elements in a set of values. Which means just add the values in a set and divide the sum with the number of elements in the set.

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

#include<stdlib.h>

#include<stdio.h>

#include<math.h>

typedefstruct _Pair

{

int key;

int value;

} Pair;

typedefstruct _Collection

{

Pair m_pair[100];

int m_maxElements;

} Collection;

constchar* CalculateMode(float *arrValue, int max)

{

staticchar 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);

Variance

map.m_maxElements = 0;

if(max > 100)

Deviation

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];/canon-driver-ir-2520/.

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;

}

constchar* CalculateRange(float *arrValue, int max)

{

staticchar 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;

constchar *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);

C Program To Find Mean Variance And Standard Deviation Equation

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

C Program To Find Mean Variance And Standard Deviation Formula

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

C Program To Find Mean Median Mode Standard Deviation And Variance

Population Standard Deviation: 2.334523


C Program To Find Mean Variance And Standard Deviation Of One




C Program To Find Mean Variance And Standard Deviation Calculator