# Find median of 1D array using function in C

**Introduction: **Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted array list. The list must first be sorted in ascending or descending order to calculate the median. If the number of elements is even, the median is the average of the two numbers in the middle. However, if the number is odd, the middle element of the array is taken as the sorted median.

The median is the average that divides the items into two parts with equal numbers of items on the left and right when the list contents are ordered. Even items have two intermediate values, and odd items have only one. Consequently, if the range of elements is even, then the median is defined. Because of the average of the two means.

**Algorithm**: See the algorithm below to calculate the median.

Step 1 - Read the elements into the array while counting the elements.

Step 2 - Sort items in ascending order.

Step 3 - Calculate the median.

So, this is the algorithm to calculate the median.

**The following are reasons to sort the data before determining the median:**

```
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 following reasoning was used to determine the median of the list:**

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

**Example 1: **Here we give the example of median of 1D array using function in C.

```
#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 ;
printf ( " Median = % d " , a [ n ] ) ;
return 0 ;
}
```

**Result: **So, we compile this above program and run it. Then give the output in the below section.

Median = 6

**Example 2: **Here, we give another example of the median of a 1D array using a function in C.

```
#include < stdio.h >
#define N 10
void main()
{
int i, j, n;
float median, a[N], t;
printf (“Enter the number of items: \n”);
scanf (“%d”, &n);
printf (“Input %d values\n”, n);
for (i=0; i<=n; i++)
scanf(“%f”, &a[i]);
for (i=0; i<=n-1; i++)
{
for (j=0; j<=n-1; j++)
{
if (a[j] <= a[j+1]
{
t = a[j];
a[j+1] = t;
a[j] = a[j+1];
}
else
continue;
}
}
if (n % 2==0)
median = (a[n/2] + a [n/2+1])/2.0;
else
median = a[n/2 + 1];
for (i=1; i<=n; i++)
printf (“%f”, a[i]);
printf (“\n\nThe Median value is %f\n”, median);
}
```

**Result: **So, we compile this above program and run this also. Then give the output in below comes which is given below.

```
Enter the number of items: 7
Input 7 values
1.3
4.5
2.3
5.5
6.7
8.9
2.9
2.900000 8.900000 6.700000 5.500000 2.300000 4.500000 1.300000
The Median value is 5.500000
```

So, in this article we are discuss about the how to find the median of a 1D array using a function in C. We also give two examples and share the result of the given examples.