简体   繁体   English

计算双数组行的平均值

[英]Calculate the average of an double array rows

I'm trying to figure out how to calculate the average value of each row in my array, but I've only found answers on how to do it with int arrays.我试图弄清楚如何计算数组中每一行的平均值,但我只找到了关于如何使用 int arrays 进行计算的答案。 I'm super beginner in programming, but I think I got some of it right in my code, there's just one thing I don't understand how to fix.我是编程的超级初学者,但我认为我的代码中有一些正确,只有一件事我不明白如何解决。 I get some average values when I print this, but they're way off.当我打印这个时,我得到了一些平均值,但它们还差得很远。

    static void Main(string[] args)
    {
        double[,] dTaulu = new double[5, 4]; //<--- this is my issue I think, why are those numbers Int32?
        double i;
        double j;

        for (i = 0; i < 1; i++)
        {
            for(j = 0; j < 4; j++)
            {
                Console.Write("Anna maanantain {0} sademäärä: ", j + 1);
                dTaulu[(int)i,(int)j] = double.Parse(Console.ReadLine());      
            }
        }
        for ( i = 1; i < 2; i++)
        {
            for( j = 0; j < 4; j++)
            {
                Console.Write("Anna tiistain {0} sademäärä: ", j + 1);
                dTaulu[(int)i, (int)j] = double.Parse(Console.ReadLine());
            }
        }
        for ( i = 2; i < 3; i++)
        {
            for ( j = 0; j < 4; j++)
            {
                Console.Write("Anna keskiviikon {0} sademäärä: ", j + 1);
                dTaulu[(int)i,(int)j] = double.Parse(Console.ReadLine());
            }
        }
        for ( i = 3; i < 4; i++)
        {
            for ( j = 0; j < 4; j++)
            {
                Console.Write("Anna torstain {0} sademäärä: ", j + 1);
                dTaulu[(int)i,(int) j] = double.Parse(Console.ReadLine());
            }
        }
        for (i = 4; i < 5; i++)
        {
            for(j = 0; j < 4; j++)
            {
                Console.Write("Anna perjantain {0} sademäärä: ", j + 1);
                dTaulu[(int)i, (int)j] = double.Parse(Console.ReadLine());
            }
        }
        double average1 = dTaulu[0,0] + dTaulu[0,1] + dTaulu[0,2] + dTaulu[0,3] / 4;
        double average2 = dTaulu[1,0] + dTaulu[1,1] + dTaulu[1,2] + dTaulu[1,3] / 4;
        double average3 = dTaulu[2,0] + dTaulu[2,1] + dTaulu[2,2] + dTaulu[2,3] / 4;
        double average4 = dTaulu[3,0] + dTaulu[3,1] + dTaulu[3,2] + dTaulu[3,3] / 4;
        double average5 = dTaulu[4,0] + dTaulu[4,1] + dTaulu[4,2] + dTaulu[4,3] / 4;  //I couldn't figure out how to use dTaulu.Average

        
        Console.WriteLine("Maanantai: {0} mm\nTiistai: {1} mm\nKeskiviikko: {2} mm\nTorstai: {3} mm\nPerjantai: {4} mm",average1,average2,average3,average4,average5);
        Console.ReadKey();                    
    }

The problem doesn't have to do with integers.这个问题与整数无关。 All of your variables are properly double and the expressions they are involved in will automatically promote integer literals to double as well.您的所有变量都是正确的double并且它们所涉及的表达式将自动将 integer 文字也提升为 double。 The problem is here:问题在这里:

double average1 = dTaulu[0,0] + dTaulu[0,1] + dTaulu[0,2] + dTaulu[0,3] / 4;

Remember the order of operator precedence here.请记住此处的运算符优先顺序。 / binds tighter than + so this is wrong. /+更紧密,所以这是错误的。 You must write:你必须写:

double average1 = (dTaulu[0,0] + dTaulu[0,1] + dTaulu[0,2] + dTaulu[0,3]) / 4;

I recommend learning to use loops.我建议学习使用循环。 They will make your work easier, your code more readable, and the code will then work with any size of array.它们将使您的工作更轻松,您的代码更具可读性,然后代码将适用于任何大小的数组。 If you write the code like below, then if you want to change the dimensions of the array to say [10,10] then all you have to do is change the first line instead of rewriting the whole thing.如果您编写如下代码,那么如果您想将数组的维度更改为 [10,10],那么您所要做的就是更改第一行而不是重写整个内容。

double[,] dTaulu = new double[5, 4];

for (int row = 0; row < dTaulu.GetLength(0); row++)
{
    for (int col = 0; col < dTaulu.GetLength(1); col++)
    {
        Console.Write($"Anna maanantain {col + 1} sademäärä: ");
        dTaulu[row, col] = double.Parse(Console.ReadLine());
    }
}

double[] averages = new double[dTaulu.GetLength(0)];
for (int row = 0; row < dTaulu.GetLength(0); row++)
{
    double sum = 0;
    for (int col = 0; col < dTaulu.GetLength(1); col++)
    {
        sum += dTaulu[row, col];
    }
    averages[row] = sum / dTaulu.GetLength(1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM