asked 132k views
4 votes
Write a method that returns the sum of a given row in a two-dimensional array.

asked
User Troas
by
8.3k points

1 Answer

2 votes

Final answer:

To calculate the sum of a given row in a two-dimensional array, you can create a method that takes the array and the row number as parameters.

Step-by-step explanation:

To calculate the sum of a given row in a two-dimensional array, you can create a method that takes the array and the row number as parameters. Here's an example in Java:

public int sumOfRow(int[][] array, int row) {
int sum = 0;
for (int i = 0; i < array[row].length; i++) {
sum += array[row][i];
}
return sum;
}

In this method, we iterate over the elements in the specified row of the array and add them up to calculate the sum. The method then returns the sum as the result.

answered
User Adesh Singh
by
7.2k points

No related questions found

Welcome to Qamnty — a place to ask, share, and grow together. Join our community and get real answers from real people.