diff --git a/MatrixExample.java b/MatrixExample.java index 051e5bc..5084e41 100644 --- a/MatrixExample.java +++ b/MatrixExample.java @@ -1,33 +1,6 @@ import java.util.Random; public class MatrixExample { - public static void main(String[] args) { - int[][] matrix = { - { 1, 2, 3, 4, 5, 6 }, - { 4, 5, 6, 3, 7, 2 }, - { 27, 8, 9, 5, 3, 21 }, - { 73, 2, 19, 5, 1, 8 }, - { 47, 9, 9, 5, 0, 22 }, - { 78, 86, 1, 4, 1, 21 }, - { 73, 18, 2, 2, 5, 11 } - }; - - 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++) { - 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; @@ -37,21 +10,26 @@ public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) { 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]; + 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]; + for (int i = 0; i < rows1; i++) { // matrix 1 rows + for (int j = 0; j < cols1; j++) { // columns + for (int k = 0; k < cols2; k++) { + result[i][k] += matrix1[i][j] * matrix2[j][k]; + } + } + } return result; + } public static int[][] generateRandomMatrix(int numRows, int numCols) { @@ -61,10 +39,13 @@ public static int[][] generateRandomMatrix(int numRows, int numCols) { 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; + } } diff --git a/MatrixExampleTester.java b/MatrixExampleTester.java new file mode 100644 index 0000000..8ec254b --- /dev/null +++ b/MatrixExampleTester.java @@ -0,0 +1,82 @@ +public class MatrixExampleTester { + public static void main(String[] args) { + int[][] matrix = { + { 1, 2, 3, 4, 5, 6 }, + { 4, 5, 6, 3, 7, 2 }, + { 27, 8, 9, 5, 3, 21 }, + { 73, 2, 19, 5, 1, 8 }, + { 47, 9, 9, 5, 0, 22 }, + { 78, 86, 1, 4, 1, 21 }, + { 73, 18, 2, 2, 5, 11 } + }; + + int[][] test1 = { + { 1, 2, 3 }, + { 4, 5, 6 }, + { 7, 8, 9 } + }; + + int[][] test2 = { + { 1, 2, 3 }, + { 4, 5, 6 }, + { 7, 8, 9 } + }; + + // GenerateRandomMatrix + System.out.println("==generateRandomMatrix()=="); + int numRows = 6; + int numCols = 7; + + int[][] matrix2 = MatrixExample.generateRandomMatrix(numRows, numCols); + System.out.println(matrix2); + + // print it out + System.out.println("result length: " + matrix2.length + " x " + matrix2[0].length); + for (int i = 0; i < matrix2.length; i++) { + for (int j = 0; j < matrix2[i].length; j++) { + System.out.print(matrix2[i][j] + " "); + + } + + System.out.println(); + + } + + System.out.println(""); + + // multiplyMatricies + System.out.println("==multiplyMatricies()=="); + int[][] result1 = MatrixExample.multiplyMatrices(matrix, matrix2); + int[][] result2 = MatrixExample.multiplyMatrices(test1, test2); + + // result 1 + System.out.println("result length: " + result1.length + " x " + result1[0].length); + for (int i = 0; i < result1.length; i++) { + for (int j = 0; j < result1[i].length; j++) { + System.out.print(result1[i][j] + " "); + + } + + System.out.println(); + + } + + System.out.println(); + + // result 2 + System.out.println("result length: " + result2.length + " x " + result2[0].length); + for (int i = 0; i < result2.length; i++) { + for (int j = 0; j < result2[i].length; j++) { + System.out.print(result2[i][j] + " "); + + } + + System.out.println(); + + } + + System.out.println(); + + } + +}