Program to Find Median of a Array

Description

For calculation of median, the array must be sorted.

1)  If number of elements of array even then median will be average of middle
    two elements.
    ex : arr[] = {2,5,6,8,9,11}
        Median will be the average of 6 and 8 that is 7.

2)  If number of elements of array odd then median will be middle element.
    ex : arr[] = {1,3,4,5,7}
         Median will be the middle element of array i.e. 4

C/C++

/* C Program to Find Median of a Array */
//Save it as MedianArray.c

#include<stdio.h>
int main(){

    int i, n, first, second, median;

    printf("Enter the size of array : ");
    scanf("%d",&n);

    int arr[n];
    
    
    //Median can only find only if array will sorted
    //If array is not sort, first sort it
    
    printf("Enter the elements in sorted array : ");
    for(i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }

    printf("Entered elements are : ");
    for(i=0;i<n;i++) {
        printf("%d ", arr[i]);
    }
    
    
    //If number of elements of array even then median will be average of middle two elements
    
    if(n%2 == 0) {
        first = arr[(n/2) - 1];
        second = arr[n/2];
        median = (first+second)/2;
        printf("\nThe median is  : %d",median);
    }
    
    
    //If number of elements of array odd then median will be middle element
    
    if(n%2 != 0) {
        median = arr[(n-1)/2];
        printf("\nThe median is  : %d",median);
    }
}
Input:
Enter the size of array : 
6
Enter the elements in sorted array : 
2
5
6
8
9
11

Output:
Entered elements are : 
2 5 6 8 9 11 
The median is  : 7

Java

/* Java Program to Find Median of a Array */
//Save it as MedianArray.java

import java.io.*;
import java.util.Scanner;

public class MedianArray {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the size of array : ");
        int n = scanner.nextInt();
        
        int arr[] = new int[n];
        
        
        //Median can only find only if array will sorted
        //If array is not sort, first sort it
        
        System.out.println("Enter the elements in sorted array : ");
        for(int i=0;i<n;i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("Entered elements are : ");
        for(int i=0;i<n;i++) {
            System.out.print(arr[i]+" ");
        }
        
        int median;
        
        
        //If number of elements of array even then median will be average of middle two elements
        
        if(n%2 == 0) {
            int first = arr[(n/2) - 1];
            int second = arr[n/2];
            median = (first+second)/2;
            System.out.println("\nThe median is  : "+median);
        }
        
        
        //If number of elements of array odd then median will be middle element
        
        if(n%2 != 0) {
            median = arr[(n-1)/2];
            System.out.println("\nThe median is  : "+median);
        }
    }
}
Input:
Enter the size of array : 
6
Enter the elements in sorted array : 
2
5
6
8
9
11

Output:
Entered elements are : 
2 5 6 8 9 11 
The median is  : 7

Related Programs

1) Program to Find Mode of a Array
2) Find the Average of Elements of Array
3) Find Smallest element and it’s position of an Array
4) Program for finding Second Largest Element Of Array
5) Program to form a number using entered digits
6) Program to insert a number at given position of an Array
7) Insert a number in an array sorted in ascending order
8) Program to delete an element from an array sorted in ascending order
9) Program to Reverse an Array using Recursion
10) Program to Find Value equal to index value
11) Program to Check Array is Perfect or Not
12) Remove Duplicate Elements From Array
Share Me

Leave a Reply