I want to add two matrices together and return a new matrix. Example:
matrixAdd({{1, 2, 3}, {4, 4, 4}}, {{5, 5, 6}, {0, -1, 2}})
is expected to return:
{{6, 7, 9}, {4, 3, 6}}
I can't understand why my code generates index out of bounds exception... Btw, arrays passed as parameters will always have the same dimensions so i don't need to worry about that.
public static int[][] matrixAdd(int[][] a, int[][] b) {
int[][] matrix = new int[a.length][b.length];
for (int r = 0; r < a.length; r++) {
for (int c = 0; c < a[r].length; c++) {
matrix[r][c] = a[r][c] + b[r][c];
}
}
return matrix;
}