-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedian
64 lines (59 loc) · 1.23 KB
/
median
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
#include <stdio.h>
#include<malloc/malloc.h>
void read1(int *p,int size) // input reading function
{
int i;
printf("Enter the array:");
for(i=0;i<size;i++)
{
scanf("%d",p+i);
}
}
float median(int *a, int *b,int size1,int size2)
{
int size=size1+size2;
int mid=size/2;
int *c=(int*)malloc(sizeof(int)* size);
int i=0,j=0,k=0;
while(i<size1 && j<size2) //Merge sort
{
if(a[i]<b[j])
{
c[k]=a[i];
i++;
}
else
{
c[k]=b[j];
j++;
}
k++;
}
while(i<size1)
{
c[k++]=a[i++];
}
while(j<size2)
{
c[k++]=b[j++];
}
if(size%2==0)
return ((c[mid]+c[mid-1])/2.0); // for even median
else
return c[mid]; //for odd median
}
int main(int argc, const char * argv[])
{
int size1,size2;
int *a,*b;
printf("Enter the size of 1st array:");
scanf("%d",&size1);
a=(int*)malloc(sizeof(int)* size1);
read1(a,size1);
printf("Enter the size of 2nd array:");
scanf("%d",&size2);
b=(int*)malloc(sizeof(int)* size2);
read1(b,size2);
printf( "Median : %f", median(a, b, size1, size2));
return 0;
}