-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.c
74 lines (61 loc) · 1.71 KB
/
stats.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* stats.c : Calculates mean, standard deviation for a list of numbers.
Quantiles are also calculated if quantiles are specified by command
line arguments. For example, stats 0.05 0.5 0.95 <datafile
would output the mean, standard deviation (estimated from sample) and
estimates of the 0.5, 0.5 and 0.95th quantile.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main( int argc, char *argv[])
{
double *vec, x, s, *percentiles ;
int c, vecl = 1000 ;
int i, index ;
double p;
int order(int, double *) ;
percentiles = (double *)malloc( (unsigned)argc*sizeof(double) );
vec = (double *)malloc( (unsigned)vecl*sizeof( double) ) ;
for( i=1; i<argc; i++){
percentiles[i] = atof( argv[i] ) ;
}
c = 0 ;
x = s= 0.0 ;
while( scanf(" %lf",vec+c) != EOF ) {
x += vec[c];
s += vec[c]*vec[c] ;
c++;
if( c >= vecl ) {
vecl += 1000;
vec = (double *)realloc( vec, (unsigned)vecl*sizeof(double) ) ;
}
}
order( c, vec);
x /= c ;
s /= c ;
s -= x*x ;
s = sqrt( s*c/(c-1.0) ) ;
printf("%lf\tsd:\t%lf\tn:\t%d", x, s, c);
for( i=1; i<argc; i++){
index = percentiles[i]*c + 0.5 ;
p = vec[index-1]*(index + 0.5 - percentiles[i]*c)
+ vec[index]*(percentiles[i]*c+0.5 -index) ;
printf("\t%5.3lf", percentiles[i]);
if( index < 1 ) printf("\t-");
else printf("\t%lf", p);
}
printf("\n");
}
int
order(int n, double *pbuf)
{
int gap, i, j;
double temp;
for( gap= n/2; gap>0; gap /= 2)
for( i=gap; i<n; i++)
for( j=i-gap; j>=0 && pbuf[j]>pbuf[j+gap]; j -=gap) {
temp = pbuf[j];
pbuf[j] = pbuf[j+gap];
pbuf[j+gap] = temp;
}
}