How to Find the Median of Array in C++? Last Updated : 21 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, the array is a collection of elements of the same type, In this article, we will learn how to find the median of the array in C++. The median of the array will be the middle element if the number of elements is odd or the average of two middle elements if the number of elements is even in the sorted array. Example: Input:myArray = [5, 2, 7, 3, 1, 6, 4]Output:median = 4Finding Median of Array in C++To find the median of an element, we will first sort the elements of the array in ascending order using std::sort() function. We then return the middle element as the median of the array if the number of elements in the array is odd, orreturn the average of two middle elements if the number of elements in the array is even. C++ Program to Find the Median of Array C++ // C++ Program to Find the Median of Numbers in an Array #include <algorithm> #include <iostream> using namespace std; double findMedian(int arr[], int n) { // first sort the array sort(arr, arr + n); // Check if the number is even or odd if (n % 2 != 0) { // If number of element is odd, return the middle // element return arr[n / 2]; } else { // If number of element is even, return the average // of the two middle elements return (arr[(n - 1) / 2] + arr[n / 2]) / 2.0; } } int main() { int arr[] = { 5, 2, 7, 3, 1, 6, 4 }; int n = sizeof(arr) / sizeof(arr[0]); // call the functiion double median = findMedian(arr, n); cout << "Median of the array is: " << median << endl; return 0; } OutputMedian of the array is: 4 Time Complexity : O(nlogn), where n is the number of elements in the array.Space Complexity : O(1) Comment More infoAdvertise with us Next Article How to Find the Median of Array in C++? bug8wdqo Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP Similar Reads How to Find the Median of 2D Array in C++? The median can be defined as the middle element of a sorted array in the case of an odd number of elements in an array and the average of the middle two elements when the number of elements in an array is even. In this article, we will see how to calculate the median of a 2D array in C++. Example: I 3 min read How to Find the Median of a Sorted Array in C++? In C++, the median of a sorted array is the middle element if the array size is odd, or the average of the two middle elements if the array size is even. In this article, we will learn how to find the median of a sorted array in C++. Example Input: myArray: {1, 2, 3, 4, 5} Output: Median of the Arra 2 min read How to Find the Length of an Array in C++? In C++, the length of an array is defined as the total number of elements present in the array. In this article, we will learn how to find the length of an array in C++.The simplest way to find the length of an array is by using the sizeof operator. First calculate the size of the array in bytes and 2 min read How to Find the Mode in a Sorted Array in C++? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will discuss how to calculate the mode of the numbers in a sorted array in C++. Example: Input: myVector = {1, 2, 2, 3, 3, 3, 4, 4, 5} Outp 3 min read How to Find the Sum of Elements in an Array in C++? In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the sum of elements in an array in C++. Example:Input: myVector = {1, 2, 3, 4, 5} O 2 min read How to Find the Median of Sorted Vector in C++? In C++, vectors are dynamic arrays and the median is a middle value when data is sorted in ascending order. In this article, we will learn how to find the median of all elements in a sorted vector in C++. Example Input: myVector = {10, 20, 30, 40, 50}; Output: Median is 30Finding Median of Sorted Ve 2 min read How to Find the Median of All Elements in a List in C++? In C++, a list is a container that allows us to store data in non-contiguous memory locations. The median of a list is defined as the middle element when the list size is odd, and the average of the two middle elements when the list size is even. In this article, we will learn how to find the median 3 min read How to Find the Range of Values in a 2D Array in C++? In C++, 2D arrays are also known as a matrix, and the range of numbers in a 2D array means the maximum and the minimum value in which the numbers lie in the given 2D array. In this article, we will learn how to find the range of numbers in a 2D array in C++. For Example, Input: my2DArray= {{80, 90, 2 min read How to Find the Median of Vector Elements in C++? Median of a dataset is defined as the middle element when N(size of vector) is odd and the average of the middle two elements when N is even in the sorted dataset. In this article, we will learn how to find the median of all the elements of a vector in C++. For Example Input:myVector = {3, 1, 6, 2, 2 min read How to Find the Smallest Number in an Array in C++? In C++, arrays are the data types that store the collection of the elements of other data types such as int, float, etc. In this article, we will learn how to find the smallest number in an array using C++. For Example,Input: myVector = {10, 3, 10, 7, 1, 5, 4} Output: Smallest Number = 1Find the Sma 2 min read Like