From 98e766ca33371f0cb78b6441228dbb6393b88960 Mon Sep 17 00:00:00 2001 From: Natalie Lim Date: Tue, 29 Aug 2023 12:33:15 -0700 Subject: [PATCH 1/2] not done sorry! --- .vscode/launch.json | 21 +++++++++++++++++++++ MatrixExample.java | 8 ++++---- 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 .vscode/launch.json 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..dbf8b3d 100644 --- a/MatrixExample.java +++ b/MatrixExample.java @@ -12,8 +12,8 @@ public static void main(String[] args) { { 73, 18, 2, 2, 5, 11 } }; - int numRows = 6; - int numCols = 7; + int numRows = 7; + int numCols = 6; int[][] matrix2 = generateRandomMatrix(numRows, numCols); int[][] result = multiplyMatrices(matrix, matrix2); @@ -40,13 +40,13 @@ public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) { } // Some more issues here too - int[][] result = new int[rows1+1][cols2+1]; + int[][] result = new int[rows1][cols2]; // 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 k = 0; k < cols1; k++) { - result[j][k] += matrix1[i][j] * matrix2[k][j]; + result[i][j] += matrix1[i][k] * matrix2[k][j]; } } } From bc5004d3c52313eb121302986b307c99412f3401 Mon Sep 17 00:00:00 2001 From: Natalie Lim Date: Wed, 30 Aug 2023 07:09:11 -0700 Subject: [PATCH 2/2] Finished? I think I fixed the bugs but I also have no idea if these numbers are correct! --- MatrixExample.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/MatrixExample.java b/MatrixExample.java index dbf8b3d..7cc8a8a 100644 --- a/MatrixExample.java +++ b/MatrixExample.java @@ -12,15 +12,15 @@ public static void main(String[] args) { { 73, 18, 2, 2, 5, 11 } }; - int numRows = 7; - int numCols = 6; + int numRows = 6; + int numCols = 7; int[][] matrix2 = generateRandomMatrix(numRows, numCols); int[][] result = multiplyMatrices(matrix, matrix2); 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,11 +40,13 @@ public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) { } // Some more issues here too - int[][] result = new int[rows1][cols2]; + 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[i][j] += matrix1[i][k] * matrix2[k][j]; }