×

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.


Related Topics

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

How to convert a string to hexadecimal in C

Converting a character array or any string to its respective hexadecimal form is simple. The only thing we have to do is to follow the below steps. Take each character from...

3 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

4 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

Free() Function in C

Free() function is a built-in function which is define in the stdlib.h header file. If we want to use this function in our program, we must include the stdlib.h header...

4 minutes read.