-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqrt.c
More file actions
47 lines (40 loc) · 798 Bytes
/
sqrt.c
File metadata and controls
47 lines (40 loc) · 798 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int *create_array(int n)
{
int i, *p;
if (0==n) return NULL;
p = malloc((sizeof(int))*n);
srand((unsigned int)time(NULL));
for (i=0; p && i<n; i++) {
p[i] = rand()%100;
}
return p;
}
void print_array(int *p, int n)
{
for (p;n>0;n--,p++) {
printf("%.2d%s", *p, ((n-1)>0)? "+":"=\n");
}
}
int sum(int *p, int n)
{
int i, s;
for (i=0,s=0; i<n; i++) s=s+p[i];
return s;
}
int main(int argc, char **argv)
{
int n, *p, s, a, i;
double d;
n = (argc>1)? atoi(argv[1]) : 10;
p = create_array(n);
print_array(p, n);
s = sum(p, n);
a = s/n;
for (i=0,d=0; i<n; i++) d+=(double)(p[i]-a)*(double)(p[i]-a);
d = sqrt(d/n);
printf("%d\navg=%d sd=%f\n", s, s/n, d);
return 0;
}