diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5664805 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,21 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Current File", + "request": "launch", + "mainClass": "${file}" + }, + { + "type": "java", + "name": "MatrixExample", + "request": "launch", + "mainClass": "MatrixExample", + "projectName": "SourceCodeTest_3a85d6c" + } + ] +} \ No newline at end of file diff --git a/MatrixExample.java b/MatrixExample.java index 051e5bc..7cc8a8a 100644 --- a/MatrixExample.java +++ b/MatrixExample.java @@ -20,7 +20,7 @@ public static void main(String[] args) { System.out.println("result length: " + result.length + " x " + result[0].length); for (int i = 0; i < result.length; i++) { - for (int j = 0; j < result[i].length; i++) { + for (int j = 0; j < result[i].length; j++) { System.out.print(result[i][j] + " "); } System.out.println(); @@ -40,13 +40,15 @@ public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) { } // Some more issues here too - int[][] result = new int[rows1+1][cols2+1]; + int r = Math.min(rows1, rows2); + int c = Math.min(cols1, cols2); + int[][] result = new int[r][c]; // Lots of issues with this code, it used to be working perfectly though - for (int i = 0; i < rows1; i++) { - for (int j = 0; j < cols2; j++) { + for (int i = 0; i < r; i++) { + for (int j = 0; j < c; j++) { for (int k = 0; k < cols1; k++) { - result[j][k] += matrix1[i][j] * matrix2[k][j]; + result[i][j] += matrix1[i][k] * matrix2[k][j]; } } }