C Program to Find the Median of Two Sorted Arrays of Same Size

«
»

This is a C Program to find the median of the elements after merging these 2 sorted arrays with same size.

Problem Description

This C Program finds median of the elements after merging 2 sorted arrays with same size.

Problem Solution

Prints the median of the elements after merging these 2 sorted arrays with a same size as shown in the program below.

Program/Source Code

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;
}
Program Explanation

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

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.

Runtime Test Cases
 
$ cc pgm91.c
$ a.out
Median is 34

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement

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 & technical discussions at Telegram SanfoundryClasses.