Find Median Of 1D Array Using Functions In C

To determine the median, use the procedure below.

  • First, read the items into an array while maintaining an item count.
  • The objects are sorted in step two by increasing value.
  • Calculate the median.

The rationale for sorting the data prior to calculating a median is as follows:

Example

for(i=1 ; i <= n-1 ; i++) {
   for (j=1 ; j <= n-i ; j++) {
      if ( a[j] <= a [j+1] ) {
         t = a[j];
         a[j] = a[j+1];
         a[j+1] = t;
      } else
      continue;
   }
}

The subsequent rationale was utilized to calculate the median value of the list: -

Example

if ( n % 2 = = 0 )
   median = ( a [ n / 2 ] + a [ n / 2 + 1 ] ) / 2 . 0 ;
else
   median = a [ n / 2 + 1 ] ;

An illustration of a C program to calculate the median of given integers is presented here.

Example

#include < stdio.h >
#include < conio.h >
#include < sdtlib >
#define N 10
main ( ) {
   int i , j , n ;
   float median , a [ N ] , t ;
   printf ( " ?nter the number of items \ n " ) ;
   scanf ( " % d " , & n ) ;
   /* Reading items into array a */
   printf ( " Input % d values \ n " , n ) ;
   for ( i = 1 ; i < = n ; i + + )
   scanf ( " % f " , & a [ i ] ) ;
   /* Sorting begins */
   for ( i = 1 ; i < = n - 1 ; i + + ) { / * Trip - i begins * /
      for ( j = 1 ; j < = n - i ; j + + ) {
         if ( a [ j ] < = a [ j + 1 ] ) { / * Interchanging values * /
            t = a [ j ] ;
            a [ j ] = a [ j + 1 ] ;
            a [ j + 1 ] = t ;
         }
         else
         continue ;
      }
   } / * sorting ends * /
   / * calculation of median * /
   if ( n % 2 = = 0 )
      median = ( a [ n / 2 ] + a [ n / 2 + 1 ] ) / 2.0 ;
   else
   median = a [ n / 2 + 1 ] ;
   / * Printing * /
   for ( i = 1 ; i < = n ; i + + )
   printf ( " % f " , a [ i ] ) ;
   printf ( " \ n \ n Median is % f \ n " , median ) ;
}

Output:

Output

?nter the number of items
5
Input 5 values
2.3
1.2
3.8
4.6
8.9
8.900000 4.600000 3.800000 2.300000 1.200000
Median is 3.800000
.......................................................
Process executed in 1.22 seconds
Press any Key to continue.

Explanation

In the previously illustrated C++ program example, we have showcased the process of determining the median value from the given integers.

Another Example:

Example

#include < stdio.h >
void swap ( int * p , int * q ) {
   int t ; 
   t = * p ; 
   * p = * q ; 
   * q = t ;
}
void sort ( int a [ ] , int n ) { 
   int i , j , temp ; 
   for ( i = 0 ; i < n - 1 ; i + + ) {
      for ( j = 0 ; j < n - i - 1 ; j + + ) {
         if ( a [ j ] > a [ j + 1 ] )
            swap ( & a [ j ] , & a [ j + 1 ] ) ;
      }
   }
}
int main ( ) {
   int a [ ] = { 6 , 3 , 8 , 5 , 1 } ;
   int n = 5 ;
   int sum , i ; 
   sort ( a , n ) ; 
   n = ( n + 1 ) / 2 - 1 ;      // -1 as array indexing in C starts from 0
   printf ( " Median = % d " , a [ n ] ) ;
   return 0 ;
}

Output:

Output

Median = 5
..................................
Process executed in 1.11 seconds 
Press any key to continue.

Explanation

In the provided C program example, consider the list 3, 5, 2, 7, 3 as our sample input. Initially, we rearrange it as 2, 3, 3, 5, 7 to ascertain the median value. By calculating ((5+1)/2), we determine that the median is 3, located at position 3 within the sorted list. Hence, the median value for this list is 3.

Input Required

This code uses input(). Please provide values below: