×

C Program for Mean and Median of an Unsorted Array

In this tutorial, we will look at how to determine the mean and median of a given unsorted array.

To determine the Mean:

To get the average, mean is determined. The formula provided can be used to get the mean

Mean of a given array = summation of all the element / Total number of element

To determine the Median:

If an array is sorted, the median is the middle element when there are odd numbers of items, and when there are even numbers of elements, it is the average of two middle elements.

The array must first be sorted if it is not already sorted, and only then may the provided logic be used.

  • If the total number is odd type,
For example, given numbers are 1, 2, 3, 4, 5
Median will be 3.
  • If the total number is even type,
For example, given numbers are 4, 5, 7, 8
Median will be ( (5 + 7) / 2 ) = 6.

C++ Program:

// Using CPP, determine the mean and median of the given array
#include <bits/stdc++.h>
using namespace std;


// Using function to find mean
double findingMean ( int array [], int num )
{
	int sum = 0;
	for (int index = 0; index < num; index++)
		sum += array [index];


	return (double) sum / (double) num ;
}


// Using function to find median
double findingMedian ( int array [], int num )


{
// Initially we have to do array sorting
	sort ( array, array + num );


// Checkinng whether it is even or not
	if ( num % 2 != 0 )
		return ( double ) array [ num / 2 ];


	return (double) ( array [ ( num - 1 ) / 2 ] + array [ num / 2 ] ) / 2.0;
}


// Main function
int main ()
{
	int array [] = { 2, 4, 5, 3, 8, 6, 9, 7 };
	int num = sizeof (array) / sizeof (array [0] );


// Calling functions
	cout << "Mean is: " << findingMean ( array, num ) << endl;
	cout << "Median is: " << findingMedian ( array, num ) << endl;
	return 0;
}

Output:

The output that will be produced if the code above is run is as follows:

Mean is: 5.5
Median is: 5.5
  • Time Complexity: O (n) [For mean]
  • Time Complexity: O (n Log n) [For Median]

[so we must first sort the array. Consider that approaches allow us to find the median in O (n) time.]

  • Space Complexity for both: O (1)

Related Topics

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C. Function Operation fopen() It is...

4 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

Evaluation of Arithmetic Expression in C

We can use the "eval" function in C to evaluate an arithmetic expression stored as a string. However, using "eval" is generally considered bad practice and can lead to security...

4 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

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.

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Floyd’s triangle in C

Floyd’s triangle in C Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming...

4 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

Infix to Postfix program in C

Infix Expression: In infix expression, an operator is placed between the two operands. Example: x + y, here operator + is placed between operands x and y. Postfix Expression: In...

3 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.