Write a method that returns the sum of a given row in a twod
Write a method that returns the sum of a given row in a two-dimensional array. Complete this code:
Use the following file:
RowSumTester.java
Complete the following file:
ArrayUtil.java
public class ArrayUtil
{
/**
Computes the sum of a given row in a two-dimensional array.
@param values the array
@param the row whose sum to compute
@return the sum of the given row
*/
public static int rowSum(int[][] values, int row)
{
values = values[row];
return values;
}
}
Solution
public class ArrayUtil
{
/**
Computes the sum of a given row in a two-dimensional array.
@param values the array
@param the row whose sum to compute
@return the sum of the given row
*/
public static int rowSum(int[][] values, int row)
{
int sum = 0;
for(int i = 0; i <values[row].length; i++)
{
sum += values[row][i];
}
return sum;
}
}
