how to use three two dimensional arrays. how to get this program : � Ask size (rows and columns) of matrix A (using message dialog box) � Ask size of Matrix B � Check the sizes of A and B, if not compatible display waning message and exit the program � Input matrix A (using message dialog box), � Input matrix B � Multiply A and B giving matrix C � Display matrix A and matrix B to command window � Display matrix C to command window The output looks like Matrix A: 1.0 2.0 3.0 4.0 5.0 6.0 Matrix B: 7.0 8.0 9.0 0.0 9.0 8.0 7.0 6.0 5.0 Product of Matrix A and B: 28.0 44.0 40.0 70.0 113.0 106.0
thank you // Perform matrix multiplication
public class MatrixOps {
public static void print_array(double[][] numbers) { for (int i=0; i<numbers.length; i++) { for (int j=0; j<numbers[i].length; j++) System.out.print(numbers[i][j] + " "); System.out.println(); } }
public static double[][] multiply(double[][] a, double[][] b) { // determine sizes of matrices int m = a.length; int n = a[0].length; int p = b[0].length; int q = b.length;
// create array to hold product double[][] c = new double[m][p]; // elements initialized to 0
// test for valid multiplication if (n != q) { System.out.println("Number of rows of second matrix must match " + "number of colums of first matrix"); return c; }
// create the product row by row for (int i=0; i<m; ++i) // produce the current row for (int j=0; j < p; ++j) for (int k=0; k<n; ++k) c[i][j] += a[i][k] * b[k][j];
return c; }
public static double[][] add(double[][] a, double[][] b) { // create array to hold result // elements initialized to 0 double[][] c = new double[a.length][a[0].length];
// ensure correct matrix dimensions if (a.length != b.length || a[0].length != b[0].length) { System.out.println("Number of rows and columns must be equal"); return c; }
// add contents of corresponding elements for (int i=0; i<a.length; i++) for (int j=0; j<a[0].length; j++) c[i][j] = a[i][j] + b[i][j];