我的任务是打印数组中一组数字的平均值和方差,为此我需要引用我所做的 avg 。但我不明白我现在如何打印 avg 是什么。当我运行程序时,我目前正在打印 0。这是我的代码:
static double varianceValue(ref double avg, params double[] intValues)
{
double sum = 0;
double f = 0;
double number = 0;
double sv = 0;
double variance= 0;
foreach (int value in intValues)
{
sum += value;
}
for (int i = 0; i < intValues.Length; i++)
{
number = intValues[i];
avg = sum / intValues.Length;
f = number - avg;
sv += Math.Pow(f, 2);
}
variance = sv / (intValues.Length - 1);
return variance;
}
static void Main(string[] args)
{
double avg = 0;
Console.WriteLine("The avg of the variables in the array are: {0}", varianceValue(ref avg));
Console.WriteLine("\nThe variance of 11, 13, 17, 19, 23 is: {0}", varianceValue(ref avg, 11.0, 13.0, 17.0, 19.0, 23.0));
Console.ReadKey();
}