You are required to fix all syntactical errors in the given code. You can click on Compile & Run anytime to check the compilation/execution status of the program. You can use System.out.print() to debug your code. The submitted code should be logically/syntactically correct and pass all testcases. Do not write the main() function as it is not required.
Code Approach: For this question, you will need to correct the given implementation. We do not expect you to modify the approach or incorporate any additional library methods.
The function/method matrixSum returns an integer representing the sum of elements of the input matrix. The function/method matrixSum accepts three arguments – rows, an integer representing the number of rows of the input matrix, columns, an integer representing the number of columns of the input matrix and matrix, a two-dimensional array representing the input matrix.
The function/method matrixSum compiles unsuccessfully due to syntactical error. Your task is to debug the program so that it passes all test cases.
Solution
class Solution
{
int matrixSum(int rows, int columns, int[][] matrix)
{
int i, j, sum=0;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
sum += matrix[i][j];
}
return sum;
}
}
Happy Learning – If you require any further information, feel free to contact me.