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

您所在的位置:网站首页 怎么求数组中的数的平均数 C#计算数组的算术平均数、几何平均数、调和平均数、平方平均数和中位数

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

2024-07-09 21:36| 来源: 网络整理| 查看: 265

1.函数实现

0)打印数组

/// /// 打印数组 /// /// 数组 /// 每行打印元素数量 /// 小数点后保留位数 private static void PrintArray(double[] arr, int numberPerLine = 7, int digitAfterDot = 2) { //设定小数点后保留位数(函数ToString()的参数) string format = "#0."; while (true) { if (digitAfterDot 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.运行示例

162430_WJmA_1425762.png



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3