Media y mediana de una array

Dada una array ordenada de tamaño n*n . Calcular la media y la mediana de la array.

Ejemplos: 

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]    

Si la array dada no está ordenada, podemos encontrar su mediana ordenando primero la array .

Implementación:

C++

// CPP program to find mean and median
// of sorted square matrix.
#include <bits/stdc++.h>
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<N; i++)
       for (int j=0; j<N; j++)
          sum += a[i][j];
 
    return (double)sum/(N*N);
}
 
// Function for calculating median
double findMedian(int a[][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;
}
 
// Driver program
int main()
{
    int a[N][N]= {{1, 2, 3, 4},
                   {5, 6, 7, 8},
                   {9, 10, 11, 12},
                   {13, 14, 15, 16}};
    cout << "Mean : " << findMean(a) << endl
         << "Median : "<< findMedian(a) << endl;
    return 0;
}

C

// C program to find mean and median
// of sorted square matrix.
#include <stdio.h>
 
#define 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<N; i++)
       for (int j=0; j<N; j++)
          sum += a[i][j];
 
    return (double)sum/(N*N);
}
 
// Function for calculating median
double findMedian(int a[][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;
}
 
// Driver program
int main()
{
    int a[N][N]= {{1, 2, 3, 4},
                   {5, 6, 7, 8},
                   {9, 10, 11, 12},
                   {13, 14, 15, 16}};
    printf("Mean : %f\n",findMean(a));
    printf("Median : %f\n",findMedian(a));
    return 0;
}
 
// This code is contributed by kothavvsaakash.

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.

Python3

# Python3 program to find mean and median
# of sorted square matrix.
N = 4
 
# Returns mean of a given matrix of
# size n x n.
def findMean(a):
     
    summ = 0
     
    # total sum calculation of matrix
    for i in range(N):
        for j in range(N):
            summ += a[i][j]
     
    return summ/(N*N)
 
# Function for calculating median
def findMedian(a):
    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
 
# Driver program
a = [[1, 2, 3, 4],[5, 6, 7, 8],
    [9, 10, 11, 12],[13, 14, 15, 16]]
print("Mean :", findMean(a))
print("Median :",findMedian(a))
 
# This code is contributed by shubhamsingh10

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

<?php
// PHP program to find
// mean and median
// of sorted square
// matrix.
 
$N = 4;
 
// Returns mean of
// a given matrix of
// size n x n.
function findMean($a)
{
    global $N;
    $sum = 0;
 
    // total sum calculation
    // of matrix
    for ($i = 0; $i < $N; $i++)
    for ($j = 0; $j < $N; $j++)
        $sum += $a[$i][$j];
 
    return (double)$sum / ($N * $N);
}
 
// Function for calculating median
function findMedian($a)
{
    global $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;
}
 
    // Driver Code
    $a= array(array(1, 2, 3, 4),
              array(5, 6, 7, 8),
              array(9, 10, 11, 12),
              array(13, 14, 15, 16));
        echo "Mean : " , findMean($a),"\n",
             "Median : ", findMedian($a);
     
// This code is contributed by vt_m.
?>

Javascript

<script>
 
// Javascriptprogram to find mean and median
// of sorted square matrix.
 
// Returns mean of a given
// matrix of size n x n.
function findMean(a, n)
{
    var sum = 0;
    var N = n;
 
    // Total sum calculation of matrix
    for(var i = 0; i < N; i++)
        for(var j = 0; j < N; j++)
            sum += a[i][j];
 
    return sum / (N * N);
}
 
// Function for calculating median
function findMedian(a, n)
{
    var 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
var a = [ [ 1, 2, 3, 4 ],
          [ 5, 6, 7, 8 ],
          [ 9, 10, 11, 12 ],
          [ 13, 14, 15, 16 ] ];
             
var n = a.length;
document.write("Mean   : " +
               findMean(a, n) + "<br>");
document.write("Median : " +
               findMedian(a, n) + "<br>");
 
// This code is contributed by Kirti
 
</script>
Producción

Mean : 8.5
Median : 8.5

Este artículo es una contribución de Himanshu Ranjan . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks. 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *