C#计算数组的算术平均数、几何平均数、调和平均数、平方平均数和中位数

1.函数实现

0)打印数组

/// <summary>
/// 打印数组
/// </summary>
/// <param name="arr">数组</param>
/// <param name="numberPerLine">每行打印元素数量</param>
/// <param name="digitAfterDot">小数点后保留位数</param>
private static void PrintArray(double[] arr, int numberPerLine = 7, int digitAfterDot = 2)
{
    //设定小数点后保留位数(函数ToString()的参数)
    string format = "#0.";
    while (true)
    {
        if (digitAfterDot <= 0) break;
        format += '0';
        digitAfterDot--;
    }

    //打印数组
    for (int i = 0; i < (arr.Length - 1) / numberPerLine + 1; i++)
    {
        for (int j = 0; j < numberPerLine; j++)
        {
            int index = i * numberPerLine + j;
            if (index < arr.Length)
            {
                Console.Write(arr[index].ToString(format) + "\t");
            }
        }
        Console.WriteLine();
    }
}

1)算术平均数:(x1+x2+...+xn)/n

/// <summary>
/// 计算算数平均数:(x1+x2+...+xn)/n
/// </summary>
/// <param name="arr">数组</param>
/// <returns>算术平均数</returns>
private static double ArithmeticMean(double[] arr)
{
    double result = 0;
    foreach (double num in arr)
    {
        result += num;
    }
    return result / arr.Length;
}

2)几何平均数:(x1*x2*...*xn)^(1/n)

/// <summary>
/// 几何平均数:(x1*x2*...*xn)^(1/n)
/// </summary>
/// <param name="arr">数组</param>
/// <returns>几何平均数</returns>
private static double GeometricMean(double[] arr)
{
    double result = 1;
    foreach (double num in arr)
    {
        result *= Math.Pow(num, 1.0 / arr.Length);
    }
    return result;
}

3)调和平均数:n/((1/x1)+(1/x2)+...+(1/xn))

/// <summary>
/// 调和平均数:n/((1/x1)+(1/x2)+...+(1/xn))
/// </summary>
/// <param name="arr">数组</param>
/// <returns>调和平均数</returns>
private static double HarmonicMean(double[] arr)
{
    double temp = 0;
    foreach (double num in arr)
    {
        temp += (1.0 / num);
    }
    return arr.Length / temp;
}

4)平方平均数:((x1*x1+x2*x2+...+xn*xn)/n)^(1/2)

/// <summary>
/// 平方平均数:((x1*x1+x2*x2+...+xn*xn)/n)^(1/2)
/// </summary>
/// <param name="arr">数组</param>
/// <returns>平方平均数</returns>
private static double RootMeanSquare(double[] arr)
{
    double temp = 0;
    foreach (double num in arr)
    {
        temp += (num * num);
    }
    return Math.Sqrt(temp / arr.Length);
}

5)中位数

/// <summary>
/// 计算中位数
/// </summary>
/// <param name="arr">数组</param>
/// <returns></returns>
private static double Median(double[] arr)
{
    //为了不修改arr值,对数组的计算和修改在tempArr数组中进行
    double[] tempArr = new double[arr.Length];
    arr.CopyTo(tempArr, 0);

    //对数组进行排序
    double temp;
    for (int i = 0; i < tempArr.Length; i++)
    {
        for (int j = i; j < tempArr.Length; j++)
        {
            if (tempArr[i] > tempArr[j])
            {
                temp = tempArr[i];
                tempArr[i] = tempArr[j];
                tempArr[j] = temp;
            }
        }
    }

    //针对数组元素的奇偶分类讨论
    if (tempArr.Length % 2 != 0)
    {
        return tempArr[arr.Length / 2 + 1];
    }
    else
    {
        return (tempArr[tempArr.Length / 2] +
            tempArr[tempArr.Length / 2 + 1]) / 2.0;
    }
}

2.Main函数调用

static void Main(string[] args)
{
    //一个数组
    double[] arr = new double[] 
    {
        3, 2, 7, 4, 8, 8, 5,
        5, 6, 5, 1, 8, 4, 9
    };

    //打印数组
    PrintArray(arr);

    //调和平均数≤几何平均数≤算术平均数≤平方平均数
    Console.WriteLine("算术平均数:" + ArithmeticMean(arr).ToString("#0.000"));
    Console.WriteLine("几何平均数:" + GeometricMean(arr).ToString("#0.000"));
    Console.WriteLine("调和平均数:" + HarmonicMean(arr).ToString("#0.000"));
    Console.WriteLine("平方平均数:" + RootMeanSquare(arr).ToString("#0.000"));
    Console.WriteLine("中位数:" + Median(arr).ToString("#0.000"));

    Console.ReadLine();
}

3.运行示例

posted @ 2019-12-15 11:36  tianfeng.cc  阅读(2228)  评论(0编辑  收藏  举报