|
#include "stdafx.h"
#include "statistics.h"
#include <math.h>
int compare(const void *a,const void *b){
return (*(int*)a-*(int*)b);
}
//median fuction after sorting sequence
double GetMedianFromSortedArray(int arr[],unsigned int size)
{
int middle = size/2;
double average;
if(size == 1)
return arr[0];
if (size%2==0) average = static_cast<float>(arr[middle-1]+arr[middle])/2;
else average = static_cast<float>(arr[middle]);
return average;
}
BOOL CalculateStatistics(int* arr,unsigned int size, double& median, double& mean, double& stddev)
{
switch(size)
{
case 0:
return FALSE;
break;
case 1:
mean = median = arr[0];
stddev = 0;
return TRUE;
break;
default:
qsort(arr,size,sizeof(int),compare);
mean = 0;
size_t i;
for(i = 0; i< size; i++)
{
mean += arr[i];
}
mean /= size;
stddev = 0;
for(i = 0; i< size; i++)
{
double val = arr[i] - mean;
val = val* val;
stddev += val;
}
stddev = sqrt(stddev/size);
median = GetMedianFromSortedArray(arr,size);
return TRUE;
break;
}
return TRUE;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Decebal Mihailescu is a software engineer with interest in .Net, C# and C++.