-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandard_deviation.c
52 lines (48 loc) · 1.1 KB
/
standard_deviation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <inttypes.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include "standard_deviation.h"
double
calculateSD (double *data, uint64_t dataSize)
{
double sum = 0.0, mean, SD = 0.0;
uint64_t i;
for (i = 0; i < dataSize; i++)
{
sum += data[i];
}
mean = sum / (double)dataSize;
for (i = 0; i < dataSize; i++)
{
SD += pow (data[i] - mean, 2);
}
printf ("mean: %lf / %" PRId64 " = %lf\n", sum, dataSize, mean);
return sqrt (SD / dataSize);
}
void
getMeanAndSD (double *data, uint64_t dataSize, double *_mean, double *_SD)
{
double sum = 0.0, mean, SD = 0.0;
uint64_t i;
for (i = 0; i < dataSize; i++)
{
sum += data[i];
}
mean = sum / (double)dataSize;
for (i = 0; i < dataSize; i++)
{
SD += pow (data[i] - mean, 2);
}
#ifdef DEBUG
printf ("mean: %lf / %" PRId64 " = %lf\n", sum, dataSize, mean);
#endif
*_mean = round (mean * 100) / 100;
*_SD = round (sqrt (SD / dataSize) * 100) / 100;
}
void
testCalculateSD ()
{
double test[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
printf ("Standard deviation of test array: %lf\n", calculateSD (test, 10));
}