Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Find the median of the two given sorted arrays which are not empty

C Programming Practice: Exercise-3 with Solution

Write a C programming to find the median of the two given sorted arrays which are not empty.

C Code:

#include <stdio.h>
#include <stdlib.h>
static double find_kth(int a[], int a_len, int b[], int b_len, int k)  
{  
    if (a_len > b_len) {
        return find_kth(b, b_len, a, a_len, k);
    }

    if (a_len == 0) {
        return b[k - 1];
    }

    if (k == 1) {
        return a[0] < b[0] ? a[0] : b[0];
    }

    int ia = k / 2 < a_len ? k / 2 : a_len;
    int ib = k - ia;  
    if (a[ia - 1] < b[ib - 1]) {
 
        return find_kth(a + ia, a_len - ia, b, b_len, k - ia);
    } 
	 else if (a[ia - 1] > b[ib - 1]) {  
        return find_kth(a, a_len, b + ib, b_len - ib, k - ib);
    } else {
        return a[ia - 1];
    }
}

static double find_median_sorted_arrays(int* num1, int num1_size, int* num2, int num2_size)
{
    int half = (num1_size + num2_size) / 2;
    if ((num1_size + num2_size) & 0x1) {
        return find_kth(num1, num1_size, num2, num2_size, half + 1);
    } else {
        return (find_kth(num1, num1_size, num2, num2_size, half) + find_kth(num1, num1_size, num2, num2_size, half + 1)) / 2;
    }
}
int main(void)
{
    int num1[] = {1, 2, 5, 6, 7 };
    int num2[] = {10, 14, 19, 35, 45, 50};
    //int num1[] = {1, 3};
    //int num2[] = {2, 4};
   int s1 = sizeof(num1)/sizeof(num1[0]);
    int s2 = sizeof(num2)/sizeof(num2[0]);
    printf("Median of the above two sorted arrays is: %f\n", find_median_sorted_arrays(num1, s1, num2, s2));  
    return 0;
}

Sample Output:

Median of the above two sorted arrays is: 10.000000

Flowchart:

C Programming Flowchart: Find the median of the two given sorted arrays which are not empty

C Programming Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C program to find length of the longest substring of a given string without repeating characters.
Next: Write a C program to find the longest palindromic substring of a given string. Maximum length of the given string is 1000.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP