Write the method:
public int sumRow(int[][] matrix, int row)
that sums row row
in the 2D array called matrix.
Given:
public void run()
{
System.out.println(sumRow(new int[][]{{70,93,68,78,83},{68,89,91,93,72},{98,68,69,79,88}}, 2));
System.out.println(sumRow(new int[][]{{1,1,1}, {2,2,2}, {3,3,3}}, 0));
System.out.println(sumRow(new int[][]{{2,4,6,8,10}, {1,2,3,4,5}, {10,20,30,40,50}}, 2));
}
So far I have:
public int sumRow(int[][] matrix, int row)
{
int sum = 0;
for(int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix.length; j++)
{
sum = sum + matrix[j][i];
}
}
return sum;
}
The outputs I get are 714, 18, and 78 when they should be 402, 3, and 150. What am I doing wrong?