two dimensional array horizontal average output

后端 未结 7 484
故里飘歌
故里飘歌 2021-01-24 05:20

I am stuck with a problem and i don\'t know how to put this in a for loop. I need the hotizontal average of the next matrix:

1 2 3 4 5

5 4 3 2 1

3 2 1 4 5
         


        
7条回答
  •  有刺的猬
    2021-01-24 05:50

    Do this in the language you want. Either Java, or Javascript. Pick one.

    for (int i = 0; i < rows; ++i)
    {
       double sum = 0;
       for (int j = 0; j < columns; ++j)
       {
          sum = sum + matrix[i][j];
       }
       double avg = sum / columns;
       print(avg);
    }
    

    Basically, this is: for each row in the matrix, create a sum of all elements, and then divide the sum by the number of columns to find the average of the row.

提交回复
热议问题