简体   繁体   中英

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. 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. 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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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