×

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

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

Actual and Formal Parameters

Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of...

5 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

function pointer as argument in C

Pointers are considered difficult to understand for beginners but pointers can be made to work if you fiddle with them long enough. So, let’s understand this step by step. What are...

3 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.