This is a C Program to find median of elements where elements are stored in two different arrays.
Here is source code of the C Program to Find Median of Elements where Elements are Stored in 2 Different Arrays. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
int N = 10, M = 5;
int sequence1[10];
int sequence2[5];
int sequence[15];
void sort() {
int i, j, temp;
for (i = 1; i < N + M; i++) {
j = i;
temp = sequence[i];
while (j > 0 && temp < sequence[j - 1]) {
sequence[j] = sequence[j - 1];
j = j - 1;
}
sequence[j] = temp;
}
}
int main(int argc, char **argv) {
int i;
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
for (i = 0; i < N; i++)
sequence1[i] = rand() % (100 - 1 + 1) + 1;
for (i = 0; i < M; i++)
sequence2[i] = rand() % (100 - 1 + 1) + 1;
for (i = 0; i < N; i++)
printf("%d ", sequence1[i]);
printf("\n");
for (i = 0; i < M; i++)
printf("%d ", sequence2[i]);
printf("\n");
int j = 0;
for (i = 0; i < N + M; i++) {
if (i >= N && j < M)
sequence[i] = sequence2[j++];
else
sequence[i] = sequence1[i];
}
sort();
if (N + M % 2 == 0)
printf("The Median is : %d",
(sequence[(N + M) / 2 - 1] + sequence[(N + M) / 2]) / 2);
else
printf("The Median is : %d", sequence[(N + M) / 2]);
return 0;
}
Output:
$ gcc MedianOfTwoArray.c $ ./a.out 36 67 31 8 89 14 74 10 24 18 61 36 45 74 80 The Median is : 36
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
Related Posts:
- Practice Computer Science MCQs
- Apply for C Internship
- Apply for Computer Science Internship
- Practice BCA MCQs
- Watch Advanced C Programming Videos