C Program to Find Median of Two Arrays of Different Sizes

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.

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<time.h>
  4. #include<stdlib.h>
  5.  
  6. int N = 10, M = 5;
  7. int sequence1[10];
  8. int sequence2[5];
  9. int sequence[15];
  10.  
  11. void sort() {
  12.     int i, j, temp;
  13.     for (i = 1; i < N + M; i++) {
  14.         j = i;
  15.         temp = sequence[i];
  16.         while (j > 0 && temp < sequence[j - 1]) {
  17.             sequence[j] = sequence[j - 1];
  18.             j = j - 1;
  19.         }
  20.         sequence[j] = temp;
  21.     }
  22. }
  23.  
  24. int main(int argc, char **argv) {
  25.     int i;
  26.     time_t seconds;
  27.     time(&seconds);
  28.     srand((unsigned int) seconds);
  29.     for (i = 0; i < N; i++)
  30.         sequence1[i] = rand() % (100 - 1 + 1) + 1;
  31.     for (i = 0; i < M; i++)
  32.         sequence2[i] = rand() % (100 - 1 + 1) + 1;
  33.     for (i = 0; i < N; i++)
  34.         printf("%d ", sequence1[i]);
  35.     printf("\n");
  36.  
  37.     for (i = 0; i < M; i++)
  38.         printf("%d ", sequence2[i]);
  39.     printf("\n");
  40.  
  41.     int j = 0;
  42.     for (i = 0; i < N + M; i++) {
  43.         if (i >= N && j < M)
  44.             sequence[i] = sequence2[j++];
  45.         else
  46.             sequence[i] = sequence1[i];
  47.     }
  48.     sort();
  49.  
  50.     if (N + M % 2 == 0)
  51.         printf("The Median is : %d",
  52.                 (sequence[(N + M) / 2 - 1] + sequence[(N + M) / 2]) / 2);
  53.     else
  54.         printf("The Median is : %d", sequence[(N + M) / 2]);
  55.     return 0;
  56. }

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.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.