In this blog post, we learn how to write a C program to find the median of two sorted arrays of the same size? So here we will write the C program to find the median of two sorted arrays of the same size. We will also see how to display the median of two given sorted arrays arr1 and arr2 of size N using C programming.
But before starting the programming I want to explain the median. Basically median is an element that divides the array into two parts left and right. So let’s see how we can find the median of an array.
1. Arrange the array elements from smallest to largest (Here, array already sorted so don’t need to this).
2. If the number of elements in the array is odd, the median is the middle element in the list.
Example,
//Given input integer sorted array Input: arr[] = {0, 1, 2, 3, 4}; Output: 2 Explanation: There is an odd number of elements (5), so the median is the middle element that is 2
In general, for an array of n elements, where n is an odd number median will be:
ArrayMedian =>Â (n+1)/2Â Â
Â
3. If the number of elements in the array is even, the median is the average of the middle two elements.
Example,
//Given input integer sorted array Input: arr[] = {0, 1, 2, 3}; Output: (1+2)/2 = > 1.5 Explanation: The median is the mean of the middle two numbers 1 and 2.
In general, for an array of n elements, where n is an even number median will be:
ArrayMedian =Â (m1+m2)/2 element, where m1 and m2 two middle number.
Â
Note: Both arrays are equal in size so we need to calculate the median using even techniques. Because 2n is always an even number, so the median will be the average of the middle two numbers.
If you want to learn more about the C language, you can check this course, Free Trial Available.
C program to find a median of two sorted arrays of the same size
#include <stdio.h> #include <stdlib.h> //Calculate array size #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0]) //return median of two sorted array of same size int findMedianTwoSameSizeSortedArray(int arr1[], int arr2[], int n) { int arr1Index = 0; int arr2Index = 0; int cnt; int median1 = -1, median2 = -1; for (cnt = 0; cnt <= n; cnt++) { if (arr1Index == n) { //all elements of arr1[] are //smaller than arr2[0] median1 = median2; median2 = arr2[0]; break; } else if (arr2Index == n) { //all elements of arr2[] are //smaller than arr1[0] median1 = median2; median2 = arr1[0]; break; } if (arr1[arr1Index] <= arr2[arr2Index]) { //Store the prev median median1 = median2; median2 = arr1[arr1Index]; arr1Index++; } else { //Store the prev median median1 = median2; median2 = arr2[arr2Index]; arr2Index++; } } return (median1 + median2)/2; } int main() { int arr1[] = {1, 12, 15, 26, 38}; int arr2[] = {2, 13, 17, 30, 45}; const int N1 = ARRAY_SIZE(arr1); const int N2 = ARRAY_SIZE(arr2); if (N1 == N2) { printf("Median is %d", findMedianTwoSameSizeSortedArray(arr1, arr2, N1)); } else { printf("Doesn't work for arrays of unequal size"); } return 0; }
Recommended Articles for you:
- Best gift for programmers.
- Best electronic kits for programmers.
- C Program to find the length of longest consecutive elements sequence from the given unsorted array of integers.
- C Program to find first and last position of element in sorted array
- Write C program to find the missing number in a given integer array of 1 to n
- C program to find the most popular element in an array
- Find the largest and smallest element in an array using C programming.
- C program to find even occurring elements in an array of limited range
- Find sum of all sub-array of a given array.
- C program to segregate even and odd numbers
- Find an element in array such that sum of left array is equal to sum of right array.
- C Program to find the count of even and odd elements in the array.
- Write C program to find the sum of array elements.
- C program to find odd occurring elements in an array of limited range
- Find the sum of array elements using recursion
- C Program to reverse the elements of an array
- C Program to find the maximum and minimum element in the array
- Calculate size of an array in without using sizeof in C
- How to create a dynamic array in C?
- How to access 2d array in C?
- Dangling, Void, Null and Wild Pointers