-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.c
More file actions
57 lines (48 loc) · 1.34 KB
/
Copy path10.c
File metadata and controls
57 lines (48 loc) · 1.34 KB
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
/*
Write a function to accept a sequence of real number,
Find the mean,median, variance and standard deviation of the sequence.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float *x, mean = 0, median, sd = 0, var = 0;
int n, i, j, temp;
printf("Enter the no of entries:");
scanf("%d", &n);
x = (float *)malloc(sizeof(float) * n);
printf("Enter your inputs:\n");
for (i = 0; i < n; i++)
scanf("%f", &x[i]);
for (i = 0; i < n; i++) //calc mean
mean = mean + x[i];
mean = mean / n;
for (i = 0; i < n; i++) // calc var
var = var + pow((x[i] - mean), 2);
var = var / n;
sd = sqrt(var); // calc sd
for (i = 0; i < n - 1; i++) //shorting for median
for (j = i; j < n; j++)
{
if (x[i] > x[j])
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
if ((n + 1) % 2 == 0) // calc median
{
median = x[((n + 1) / 2) - 1];
}
else
{
median = (x[((n + 1) / 2) - 1] + x[((n + 2) / 2) - 1]) / 2;
}
printf("Mean : %f\n", mean);
printf("Median: %f\n", median);
printf("Variance: %f\n", var);
printf("Standard deviation: %f\n", sd);
return 0;
}