/* svuk002@ec.auckland.ac.nz */ class MMul { /* * Your task is to complete the matrixMultiply method below. * * Here is the pseudocode: * * define matrixMultiply (A, B): * { Multiply two n by n matrices and store the product in matrix C } * * Construct matrix C * * for each row i of C: * for each column j of C: * sumSoFar = 0 * for each k in [1..n]: * sumSoFar = sumSoFar + Entry of A at (i, k) x * Entry of B at (k, j) * Entry of C at (i, j) = sumSoFar * * return C */ static int[][] matrixMultiply (int[][] a, int[][] b) { /* You need to complete this method */ return null; } static void testCaseOne () { /* A simple test case */ int[][] a = {{2, -1}, {1, 3}}; int[][] b = {{1, 1},{0, -1}}; int[][] c = matrixMultiply (a, b); String result = "2 3\n1 -2"; System.out.print ("Test Case One:\n\n"); System.out.print ("The result should be:\n" + result); System.out.print ("\nYour result is:\n" + printMatrix (c) + "\n"); } static void testCaseTwo () { /* Another simple test case */ int[][] a = {{10, -19, 34}, {1, 3, 23}, {67, 45, -9}}; int[][] b = {{14, 21, -9}, {0, -1, 16}, {58, 3, -8}}; int[][] c = matrixMultiply (a, b); String result = "2112 331 -666\n1348 87 -145\n416 1335 189"; System.out.print ("Test Case Two:\n\n"); System.out.print ("The result should be:\n" + result); System.out.print ("\nYour result is:\n" + printMatrix (c) + "\n"); } static String printMatrix (int[][] c) { /* Print matrix c to a string */ String toRet = ""; for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[i].length; j++) toRet += c[i][j] + " "; toRet += "\n"; } return toRet; } public static void main (String[] args) { /* * Entry point. * You can test your matrixMultiply () method by uncommenting * the test cases below. * */ //testCaseOne (); //testCaseTwo (); } }