Question: Write a method to multiply two matrices. The header of the method is: public static double[][] multiplyMatrix(double[][] a, double [][] b) To multiply a matrix

Write a method to multiply two matrices. The header of the method is: public static double[][] multiplyMatrix(double[][] a, double [][] b)

To multiply a matrix a by matrix b, the number of columns in a must be the same as the number of rows in b, and the two matrices must have elements of the same or compatible types. Let c be the result of the multiplication. Assume that a has n columns. Each element cij is ai1 b1j + ai2 b2j + ... + ain bnj.

Your program, will prompt the user for the number of rows and columns for the first matrix, then its contents. It will then prompt the user for the number of rows and columns for the second matrix, then its contents. It will call your multiplyMatrix() function and print the resulting matrix. If the matrices are not compatible, your program should give an error message. Checking for compatibility is the responsibility of main(), not multiplyMatrix().

Here is an example of a run of the program: Matrix A Enter number of rows: 2 Enter number of columns: 3 Enter contents by rows: 1 2 3 4 5 6

Matrix B Enter number of rows: 3 Enter number of columns: 2 Enter contents by rows: 7 10 8 11 9 12 Matrix C is 50.0 68.0 122.0 167.0

Hints: Write a separate function for doing the input of a matrix rather than duplicating that code in main(). You will have to get the number of rows and columns in the matrices in main() to check for compatibility. You will have to do it again in multiplyMatrix() to create the resulting array and do the calculations. Dont worry about this duplication of code. In this particular program, you can use variable names a, b, and c for your matrices because that is the terminology that mathematicians are used to.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!