# Mean and Median of a matrix

Given a sorted matrix of size n*n. Calculate the mean and median of the matrix .

Examples:

```Input : 1 2 3
4 5 6
7 8 9
Output :Mean: 5
Median: 5

Input : 1 1 1
2 2 2
4 4 4
Output :Mean: 2
Median: 2
```
```Mean of matrix is =
(sum of all elements of matrix)/
(total elements of matrix)
Note that this definition doesn't require
matrix to be sorted and works for all
matrices.

Median of a sorted matrix is calculated as:
1. When n is odd
median is mat[n/2][n/2]
2. When n is even, median is average
of middle two elements.
Middle two elements can be found at indexes
a[(n-2)/2][n-1] and a[n/2][0]
```

If given matrix is unsorted, we can find its median by first sorting the matrix.

## C++

 `// CPP program to find mean and median ` `// of sorted square matrix. ` `#include ` `using` `namespace` `std; ` ` `  `const` `int` `N = 4; ` ` `  `// Returns mean of a given matrix of ` `// size n x n. ` `double` `findMean(``int` `a[][N]) ` `{ ` `    ``int` `sum = 0; ` ` `  `    ``// total sum calculation of matrix ` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to find mean and median ` `// of sorted square matrix. ` `import` `java.io.*; ` ` `  `class` `GFG ` `{ ` `     `  `// Returns mean of a given  ` `// matrix of size n x n. ` `static` `double` `findMean(``int` `a[][],  ` `                    ``int` `n) ` `{ ` `    ``int` `sum = ``0``; ` `    ``int` `N=n; ` ` `  `    ``// total sum calculation of matrix ` `    ``for` `(``int` `i = ``0``; i < N; i++) ` `    ``for` `(``int` `j = ``0``; j < N; j++) ` `        ``sum += a[i][j]; ` ` `  `    ``return` `(``double``)sum / (N * N); ` `} ` ` `  `// Function for calculating median ` `static` `double` `findMedian(``int` `a[][], ``int` `n) ` `{  ` `    ``int` `N = n; ` `     `  `    ``if` `(N % ``2` `!= ``0``) ` `    ``return` `a[N / ``2``][N / ``2``]; ` ` `  `    ``if` `(N % ``2` `== ``0``) ` `    ``return` `(a[(N - ``2``) / ``2``][ N - ``1``] +  ` `            ``a[ N / ``2``][``0``]) / (``2.0``); ` `    ``return` `0``; ` `} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `        ``int` `a[][]= {{``1``, ``2``, ``3``, ``4``}, ` `                    ``{``5``, ``6``, ``7``, ``8``}, ` `                    ``{``9``, ``10``, ``11``, ``12``}, ` `                    ``{``13``, ``14``, ``15``, ``16``}}; ` `             `  `        ``int` `n = a.length; ` `        ``System.out.println(``"Mean   : "` `+  ` `                            ``findMean(a, n)); ` `        ``System.out.println(``"Median : "` `+  ` `                            ``findMedian(a, n)); ` `    ``} ` `         `  `         `  `} ` ` `  `// This code is contributed by KRV. `

## C#

 `// C# program to find mean and median ` `// of sorted square matrix. ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Returns mean of a given  ` `    ``// matrix of size n x n. ` `    ``static` `double` `findMean(``int` `[,]a, ``int` `n) ` `    ``{ ` `        ``int` `sum = 0; ` `        ``int` `N = n; ` `     `  `        ``// total sum calculation of matrix ` `        ``for` `(``int` `i = 0; i < N; i++) ` `            ``for` `(``int` `j = 0; j < N; j++) ` `                ``sum += a[i,j]; ` `     `  `        ``return` `(``double``)sum / (N * N); ` `    ``} ` `     `  `    ``// Function for calculating median ` `    ``static` `double` `findMedian(``int` `[,]a, ``int` `n) ` `    ``{  ` `        ``int` `N = n; ` `         `  `        ``if` `(N % 2 != 0) ` `        ``return` `a[N / 2,N / 2]; ` `     `  `        ``if` `(N % 2 == 0) ` `        ``return` `( a[(N - 2) / 2, (N - 1)] + ` `                     ``a[ N / 2, 0] ) / (2.0); ` `         `  `        ``return` `0; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main () ` `    ``{ ` `        ``int` `[,]a= { { 1,  2,  3,  4}, ` `                    ``{ 5,  6,  7,  8}, ` `                    ``{ 9, 10, 11, 12}, ` `                    ``{13, 14, 15, 16} }; ` `             `  `        ``int` `n = a.GetLength(0); ` `         `  `        ``Console.WriteLine(``"Mean : "` `+  ` `                            ``findMean(a, n)); ` `                             `  `        ``Console.WriteLine(``"Median : "` `+  ` `                            ``findMedian(a, n)); ` `    ``} ` `         `  `} ` ` `  `// This code is contributed by Sam007. `

## PHP

 ` `

Output:

```Mean : 8.5
Median : 8.5
```

This article is contributed by Himanshu Ranjan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Improved By : KRV

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.