This is a C Program to find the median of the elements after merging these 2 sorted arrays with same size.
This C Program finds median of the elements after merging 2 sorted arrays with same size.
Prints the median of the elements after merging these 2 sorted arrays with a same size as shown in the program below.
Here is source code of the C Program to find the median of the elements after merging 2 sorted arrays with the same size. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* * C Program to Find the Median of the Elements after Merging these 2 Sorted Arrays with Same Size */ #include <stdio.h> int getMedian(int array1[], int array2[], int n) { int i = 0; /* Current index of i/p array array1[] */ int j = 0; /* Current index of i/p array array2[] */ int count; int m1 = -1, m2 = -1; for (count = 0; count <= n; count++) { if (i == n) { m1 = m2; m2 = array2[0]; break; } else if (j == n) { m1 = m2; m2 = array1[0]; break; } if (array1[i] < array2[j]) { m1 = m2; /* Store the prev median */ m2 = array1[i]; i++; } else { m1 = m2; /* Store the prev median */ m2 = array2[j]; j++; } } return (m1 + m2)/2; } int main() { int array1[] = {20, 25, 35, 30, 38}; int array2[] = {22, 53, 65, 72, 45}; int n1 = sizeof(array1) / sizeof(array1[0]); int n2 = sizeof(array2) / sizeof(array2[0]); if (n1 == n2) printf("Median is %d", getMedian(array1, array2, n1)); else printf("not possible to findout"); getchar(); return 0; }
This C Program we have defined the array1[] and array2[] variable values. If else condition statement is used to check both the size of an array variable values is equal.
If the condition is true then execute if condition statement. Then pass the ‘array1’, ‘array2’ and ‘n1’ variable value as an argument to getMedian() function. Otherwise, if the condition is false, then execute the else statement. Print not possible to find out the median of the elements.
In getMedian() initialize the values of ‘i’ and ‘j’ variables as 0. For loop is used to find the median of the elements. Nested if else condition statement is used to merge 2 sorted arrays with same size. Check if the value of ‘i’ variable and the value of ‘n’ variable are equal. If the condition is true then execute the statement.
Assign the value of ‘m2’ variable to ‘m1’ variable and array2[] variable with base index 0 value to ‘m2’ variable.
Otherwise, if the condition is false. Then execute the else if condition statement, if the value of ‘j’ variable and the value of ‘n’ variable are equal. If the condition is true, then execute the statement. Assign the value of ‘m2’ variable to ‘m1’ variable, and the value of ‘array1[]’ variable to ‘m2’ variable.
If condition statement is used to check the value of ‘array1[]’ variable with base index ‘i’ is less than the array2[] variable value with base index ‘j’. If the condition is true, then execute the statement. Assign the value of ‘m2’ variable to ‘m1’ variable, and the value of ‘array1[]’ variable to ‘m2’ variable.
If condition statement is used to check the value of ‘array1[]’ variable with base index ‘i’. Otherwise, if the condition is false then execute the else statement. Assign the value of ‘m2’ variable to ‘m1’ variable, and the value of ‘array2[]’ variable with base index 0 value to ‘m2’ variable. Return the summation of the value of ‘m1’ and ‘m2’ variables and divide the resulted value by 2. Print the median of the elements after merging these 2 sorted arrays with same size.
$ cc pgm91.c $ a.out Median is 34
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship
- Practice BCA MCQs
- Apply for C Internship
- Check Computer Science Books