📜  平均绝对偏差程序

📅  最后修改于: 2021-04-24 03:42:13             🧑  作者: Mango

给定大小为n的数组,求平均绝对偏差。
先决条件:均值,方差和标准差
例子:

Input : arr[] = {10, 15, 15, 17, 18, 21}
Output : 2.66667

Input : arr[] = {23.54, 56, 34.56, 67, 45.34,  
                              56.78, 78, 21}
Output : 16.6675
C++
// C++ Program to find mean absolute
// deviation of given array.
#include 
using namespace std;
 
// Function to find mean
// of the array elements.
float Mean(float arr[], int n)
{  
    // Calculate sum of all elements.
    float sum = 0;
    for (int i = 0; i < n; i++)
        sum = sum + arr[i];
    return sum / n;
}
 
// Function to find mean absolute
// deviation of given elements.
float meanAbsoluteDeviation(float arr[], int n)
{  
    // Calculate the sum of absolute
    // deviation about mean.
    float absSum = 0;
    for (int i = 0; i < n; i++)
        absSum = absSum + abs(arr[i] - Mean(arr, n));
 
    // Return mean absolute deviation about mean.
    return absSum / n;
}
 
// Driver function.
int main()
{
    float arr[] = { 10, 15, 15, 17, 18, 21 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << meanAbsoluteDeviation(arr, n);
    return 0;
}


Java
// java Program to find mean absolute
// deviation of given array
import java.io.*;
 
class GFG {
         
    // Function to find mean
    // of the array elements.
    static float Mean(float arr[], int n)
    {
        // Calculate sum of all elements.
        float sum = 0;
         
        for (int i = 0; i < n; i++)
            sum = sum + arr[i];
         
        return sum / n;
    }
     
    // Function to find mean absolute
    // deviation of given elements.
    static float meanAbsDevtion(float arr[],
                                       int n)
    {
        // Calculate the sum of absolute
        // deviation about mean.
        float absSum = 0;
         
        for (int i = 0; i < n; i++)
            absSum = absSum + Math.abs(arr[i]
                                - Mean(arr, n));
     
        // Return mean absolute
        // deviation about mean.
        return absSum / n;
    }
     
        // Driver function.
        public static void main (String[] args) {
         
        float arr[] = { 10, 15, 15, 17, 18, 21 };
        int n = arr.length;
         
            System.out.println(meanAbsDevtion(arr, n));
        }
}
 
 
// This code is contributed by vt_m


Python3
# Python 3 Program to find
# mean absolute deviation
# of given array.
 
# Function to find mean
# of the array elements.
def Mean(arr, n) :
 
    # Calculate sum of all
    # elements.
    sm = 0
     
    for i in range(0, n) :
        sm = sm + arr[i]
    return sm // n
     
# Function to find mean
# absolute deviation of
# given elements.
def meanAbsoluteDeviation(arr, n) :
     
    # Calculate the sum of
    # absolute deviation
    # about mean.
    absSum = 0
    for i in range(0, n ):
        absSum = absSum + abs(arr[i] -
                         Mean(arr, n))
 
    # Return mean absolute
    # deviation about mean.
    return absSum / n
 
# Driver function.
arr = [ 10, 15, 15, 17, 18, 21 ]
n = len(arr)
 
print(meanAbsoluteDeviation(arr, n))
 
# This code is contributed by Nikita Tiwari.


C#
// C# Program to find mean absolute
// deviation of given array
using System;
 
class GFG {
         
    // Function to find mean
    // of the array elements.
    static float Mean(float []arr, int n)
    {
        // Calculate sum of all elements.
        float sum = 0;
         
        for (int i = 0; i < n; i++)
            sum = sum + arr[i];
         
        return sum / n;
    }
     
    // Function to find mean absolute
    // deviation of given elements.
    static float meanAbsDevtion(float []arr,
                                    int n)
    {
        // Calculate the sum of absolute
        // deviation about mean.
        float absSum = 0;
         
        for (int i = 0; i < n; i++)
            absSum = absSum + Math.Abs(arr[i]
                                - Mean(arr, n));
     
        // Return mean absolute
        // deviation about mean.
        return absSum / n;
    }
     
        // Driver function.
        public static void Main ()
        {
             
            float []arr = { 10, 15, 15, 17, 18, 21 };
            int n = arr.Length;
         
            Console.WriteLine(meanAbsDevtion(arr, n));
        }
}
 
 
// This code is contributed by vt_m


PHP


Javascript


输出:

2.66667