sum of columns in a 2 dimensional array

后端 未结 5 1022
太阳男子
太阳男子 2020-12-18 10:54
static double [][] initialArray = {{7.432, 8.541, 23.398, 3.981}, {721.859, 6.9211, 29.7505, 53.6483}, {87.901, 455.72, 91.567, 57.988}};

public double[] columnSum(         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-18 11:45

    Problem with your code is when it tries to fetch arr[3].length as there does not exist simple solution like sum = sum+arr[i][j] where i refers to row and j refers to column.

    int row = arr.length;
    int col = arr[0].length;
    for(int j = 0; j < cols; j++)
    {
                int sum = 0;
                for(int i = 0; i < rows; i++)
                {
                    sum = sum + input[i][j];
                }
    
            }
    

提交回复
热议问题