Calculate median of the array

C program to calculate median of the array.

Median is the middle value of a set of data. To calculate the median first we need to sort the list in ascending or descending order.
  • If the number of elements are even, then the median is the average of two numbers in the middle.
  • If the number of elements are odd then the middle element of the array after sorting will be considered as the median.

    For example,
    Array elements: 2, 5, 4, 3, 1
    Sorted array: 1, 2, 3, 4, 5
    Median: 3.000000 (middle element)

    Array elements: 2, 5, 4, 6, 3, 1
    Sorted array: 1, 2, 3, 4, 5, 6
    Median: 3.500000 ((3 + 4)/2)
  • Program

    #include<stdio.h>
    #include<conio.h>
    #define MAX 50
    
    void read_array(int a[MAX], int n)
    {
        int i;
        for(i=0;i<n;i++)
        {
            printf("Element %d\t:",i+1);
            scanf("%d",&a[i]);
        }
    }
    
    void print_array(int a[MAX], int n)
    {
        int i;
        for(i=0;i<n;i++)
        {
            printf("Element %d\t:%d\n",i+1,a[i]);
        }
    }
    
    void sort(int a[MAX], int n)
    {
        int i, j, temp;
        for(i=0;i<n;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(a[i] > a[j])
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    }
    
    float median(int a[MAX], int n)
    {
        float med;
        if(n%2 == 0)
        {
            med = (a[(n-1)/2] + a[n/2]) / 2.0;
        }
        else
        {
            med = a[n/2];
        }
        return med;
    }
    
    void main()
    {
        int arr[MAX];
        int i, j, n, temp;
        float med;
    
        printf("Enter the number of elements in the array\t:");
        scanf("%d",&n);
    
        printf("Enter the elements of the array\n");
        read_array(arr, n);
    
        sort(arr, n);
    
        printf("\nSorted array\n");
        print_array(arr, n);
    
        med = median(arr, n);
        printf("\nMedian of the above array is %f",med);
    
        getch();
    }
    

    Output

    ********** Run1 ********** 
    
    Enter the number of elements in the array       :7
    Enter the elements of the array
    Element 1       :12
    Element 2       :25
    Element 3       :54
    Element 4       :89
    Element 5       :65
    Element 6       :23
    Element 7       :57
    
    Sorted array
    Element 1       :12
    Element 2       :23
    Element 3       :25
    Element 4       :54
    Element 5       :57
    Element 6       :65
    Element 7       :89
    
    Median of the above array is 54.000000
    
    
    ********** Run2 ********** 
    
    Enter the number of elements in the array       :8
    Enter the elements of the array
    Element 1       :12
    Element 2       :56
    Element 3       :45
    Element 4       :78
    Element 5       :36
    Element 6       :59
    Element 7       :95
    Element 8       :84
    
    Sorted array
    Element 1       :12
    Element 2       :36
    Element 3       :45
    Element 4       :56
    Element 5       :59
    Element 6       :78
    Element 7       :84
    Element 8       :95
    
    Median of the above array is 57.500000