How to Find the Median of 2D Array in C++? Last Updated : 14 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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: Input: myArray = { {1, 6, 5}, {2, 4, 6}, {7, 22, 9} } Output: Median is: 5Finding Median of 2D Array in C++ To find the median of the 2D array, we first have to convert it to a 1D array by placing row after row. After then we will sort it and find the median in that sorted array. This approach is explained below in detail: Approach Create a 1D array with a size equal to the total number of elements in the 2D array.Traverse the 2D array using nested loops and add each element to flattenedArray.Sort flattenedArray in ascending order.If the total number of elements is even, the median is the average of the two middle numbers.If the total number of elements is odd, the median is the middle number.C++ Program to Find Median of 2D Array C++ // C++ program to find the median of a 2D array #include <algorithm> #include <iostream> using namespace std; // Function to find the median of a 2D array double findMedian(int matrix[3][3], int m, int n) { int mergedArray[m * n]; int count = 0; // Merge elements into a 1D array for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { mergedArray[count++] = matrix[i][j]; } } // Sort the merged array sort(mergedArray, mergedArray + m * n); // Calculate the median if ((m * n) % 2 == 0) { return (mergedArray[(m * n) / 2 - 1] + mergedArray[(m * n) / 2]) / 2.0; } else { return mergedArray[(m * n) / 2]; } } // Main function int main() { // Given 2D array int matrix[3][3] = { { 1, 6, 5 }, { 2, 4, 6 }, { 7, 22, 9 } }; // Find and display the median cout << "Median of the 2D array is " << findMedian(matrix, 3, 3) << endl; return 0; } OutputMedian of the 2D array is 6 Time complexity: O(n*m log(n*m) ) where n is the number of rows and m is the number of columns.Space complexity: O(n*m) Comment More infoAdvertise with us Next Article How to Find the Median of 2D Array in C++? A anushamahajan5 Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP Similar Reads How to Find the Median of Array in C++? 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 th 2 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 Mode in a 2D Array in C++? A mode is a number that occurs most frequently in comparison to other numbers in a given dataset. In this article, we will find the mode in a 2D array of integers in C++. Input:myArray = { {1, 2, 2, 3}, {3, 4, 5, 5}, {5, 6, 7, 8} }Output: 5Mode in 2D ArrayTo find a mode of numbers in a 2D array, we 2 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 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 Variance of Numbers in 2D Array in C++? The variance of numbers is a measure of the spread of a set of values which is used to find how much the values in a dataset differ from mean or average. In this article, we will learn how to find the variance of numbers in a 2D array in C++. Example Input: myMatrix = { { 10, 20, 30 }, { 40, 50, 60 3 min read How to Find the Product of 2D Array Elements in C++? In C++, finding the product of all elements in a 2D array means for a given 2D array of order M*N we have to multiply all the elements together. In this article, we will learn how to find the product of elements in a 2D array in C++. Example Input:myArray[2][2] = {{1, 2}, {3, 4}};Output:Product of E 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 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 Like