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:

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)

{

. . .

}

}

Tester File:

RowSumTester.java

Solution

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)
{
int sum = 0;
   for(int i=0; i<values[row].length; i++){
       sum = sum + values[row][i];
   }
   return sum;
}
}

RowSumTester.java

Output:

2
Expected: 2
34
Expected: 34
34
Expected: 34
34
Expected: 34
34
Expected: 34

 Write a method that returns the sum of a given row in a two-dimensional array. Complete this code: public class ArrayUtil { /** Computes the sum of a given row
 Write a method that returns the sum of a given row in a two-dimensional array. Complete this code: public class ArrayUtil { /** Computes the sum of a given row

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site