×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to calculate median of an array

In this C program, we are going to learn how to find the median of an array? Here, we are reading N elements and finding their median element.
Submitted by IncludeHelp, on April 13, 2018

What is median of an array?

Basically a median is the value present at the centre of a sorted array list. 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 will the average of two numbers in the middle.

But the number is odd then the middle element of the array after sorting will be considered as the median.

Problem statement

Given N elements of integer array and we have to find its median in C.

Example

Input:
Array elements are: 
3, 2, 1, 4, 6

Sorted array is:
1, 2, 3, 4, 6

Output:
The median is : 3.000000

Program to find median of an array in C

/** C program to calculate the median of 
 * an array.
*/

/** THE CONCEPT OF MEDIAN ----------------------------------
 "Basically a median is the value present at the center of a 
 sorted array list. 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 
 will the average of two numbers in the middle.
 
 But the number is odd then the middle element 
 of the array after sorting will be considered as the median."
 */

#include <stdio.h>

// function to sort the array in ascending order
void Array_sort(int *array , int n)
{ 
    // declare some local variables
    int i=0 , j=0 , temp=0;

    for(i=0 ; i<n ; i++)
    {
        for(j=0 ; j<n-1 ; j++)
        {
            if(array[j]>array[j+1])
            {
                temp        = array[j];
                array[j]    = array[j+1];
                array[j+1]  = temp;
            }
        }
    }

    printf("\nThe array after sorting is..\n");
    for(i=0 ; i<n ; i++)
    {
        printf("\narray_1[%d] : %d",i,array[i]);
    }
}

// function to calculate the median of the array
float Find_median(int array[] , int n)
{
    float median=0;
    
    // if number of elements are even
    if(n%2 == 0)
        median = (array[(n-1)/2] + array[n/2])/2.0;
    // if number of elements are odd
    else
        median = array[n/2];
    
    return median;
}

int main()
{
    // declare two int arrays
    int array_1[30] = {0};
    // declare some local variables
    int i=0 ,n=0;
    float median=0;

    printf("\nEnter the number of elements for the array : ");
    scanf("%d",&n);
   
    printf("\nEnter the elements for array_1..\n");
    for(i=0 ; i<n ; i++)
    {
        printf("array_1[%d] : ",i);
        scanf("%d",&array_1[i]);
    }
    
    // Sort the array in ascending order
    Array_sort(array_1 , n);
    
    // Now pass the sorted array to calculate
    // the median of your array.
    median = Find_median(array_1 , n);
    
    printf("\n\nThe median is : %f\n",median);
    return 0;
}

Output

Run 1 :

Enter the number of elements for the array : 6
Enter the elements for array_1..  
array_1[0] : 3 
array_1[1] : 2 
array_1[2] : 4 
array_1[3] : 5 
array_1[4] : 1 
array_1[5] : 6 

The array after sorting is..

array_1[0] : 1 
array_1[1] : 2 
array_1[2] : 3 
array_1[3] : 4 
array_1[4] : 5 
array_1[5] : 6 
 
The median is : 3.500000

Run 2: 

Enter the number of elements for the array : 5

Enter the elements for array_1..  
array_1[0] : 3 
array_1[1] : 2 
array_1[2] : 1 
array_1[3] : 4 
array_1[4] : 6 

The array after sorting is..

array_1[0] : 1 
array_1[1] : 2 
array_1[2] : 3 
array_1[3] : 4 
array_1[4] : 6 

The median is : 3.000000

C One-Dimensional Array Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.