Find Median of 1D Array Using Functions in C

The midway value that splits the items into two portions with an equal number of items on either side is known as the median if the list's contents are ordered in order. Even numbers of things have two middle values, while odd numbers of items have just one. Therefore, the median is defined as the average of the two middle values when there are even numbers of elements.

Algorithm

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.

Following is the reasoning behind sorting the data before determining a median:-

The following reasoning was used to determine the list's median:-

Example: C program to determine the median of supplied integers is provided below.

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 above example of a program in C++, we have demonstrated how we can find the median of the supplied intergers.

Another Example:

Output:

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

Explanation

In the above example of a program in C, Take the list 3, 5, 2, 7, 3 as an example for our input list. First, we reorder it as 2, 3, 3, 5, 7 to determine the median and we discover that ((5+1)/2) is 3 at position 3. Therefore, the median value in this list is 3.






Latest Courses