-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverage and median.c
More file actions
48 lines (42 loc) · 1020 Bytes
/
Copy pathaverage and median.c
File metadata and controls
48 lines (42 loc) · 1020 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
48
#include<stdio.h>
#include<stdlib.h>
int compare(const void *a, const void *b) {
double diff = (*(double*)a - *(double*)b);
if (diff < 0) return -1;
if (diff > 0) return 1;
return 0;
}
int main()
{
printf("NOTE:Give // when you finish giving the inputs\nFUNCTION FOR COUNTING AVERAGE\n\n\nENTER:");
int capacity;double *arr=(double*)malloc(capacity*sizeof(double));
int count=0;
double input;
capacity=2;
while(scanf("%lf",&input)==1)
{
if(capacity==count)
{
arr=(double*)realloc(arr,capacity*2*sizeof(double));
}
arr[count]=input;
count++;}
int i; double sum=0;
for(i=0;i<count;i++)
{sum+=arr[i];
}
double avg=sum/count;
printf("SUM= %.2lf\nAVERAGE= %.2lf",sum,avg);
qsort(arr,count,sizeof(double),compare);
double med;
if (count%2!=0)
{
med=arr[(count/2)];
}
else
{
med=(arr[count/2]+arr[(count/2)-1])/2;
}
printf("\nMEDIAN: %.2lf",med);
free (arr);
}