×

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

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Variables in C

A variable can be defined as a name allocated to a storage space that can be manipulated by our programs. Every variable in C arbitrates about the overall layout and...

5 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

Built-in functions in C

The function is a set of instructions and statements enclosed in the "{}" delimiter. In c, there are two types of functions. Pre-define functions/ Built-in functionsUser define function. Built-in functions in C:- These...

8 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 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.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

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...

2 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.