diff --git a/MatrixExTest.java b/MatrixExTest.java new file mode 100644 index 0000000..63b53a0 --- /dev/null +++ b/MatrixExTest.java @@ -0,0 +1,68 @@ +import java.util.Random; +public class MatrixExTest { + + public static void main(String[] args) { + int[][] matrix = { + { 1, 2, 3, 4}, + { 4, 5, 6, 2}, + { 7, 8, 9, 2} + }; + + int numRows = 3; + int numCols = 4; + + int[][] matrix2 = generateRandomMatrix(numCols, numRows); + 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; j++) { + System.out.print(result[i][j] + " "); + } + System.out.println(); + } + + } + + public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) { + int rows1 = matrix1.length; + int cols1 = matrix1[0].length; + int rows2 = matrix2.length; + int cols2 = matrix2[0].length; + + if (cols1 != rows2) { + throw new IllegalArgumentException( + "Number of columns in the first matrix must be equal to the number of rows in the second matrix."); + } + + // Some more issues here too + int[][] result = new int[rows1+1][cols2+1]; + + // 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 < cols1; j++) { + for (int k = 0; k < cols1; k++) { + result[j][k] += matrix1[i][j] * matrix2[k][j]; + } + } + } + + return result; + } + + public static int[][] generateRandomMatrix(int numRows, int numCols) { + int[][] matrix = new int[numRows][numCols]; + Random random = new Random(); + + for (int i = 0; i < numRows; i++) { + for (int j = 0; j < numCols; j++) { + matrix[i][j] = random.nextInt(100); // Generates random values between 0 and 99 + } + } + + return matrix; + } + +} + +