Matrix - the final step - Multiplying - April 20th Module
Creating the multiplyArray( ) method
Today you will create your multiplyArray(int[][]array1, int[][]array2, int[][] array3) method. Unlike the add and subtract array methods that made use of nested for loops this method requires that you hard code the calculations. Here are the instructions from Oracle:
Multiplication of matrices appears to be confusing since it does not follow the positional method used in addition and subtraction. The answer is achieved by taking the row from the first matrix and the column from the second matrix and multiplying the respective values and then taking the sum of the products.
The answer below was achieve as follows:
3(1)+4(-2)=-5 3(0)+4(3)=12
5(1)+6(-2)=-7 5(0)+6(3)=18
The first step for solving this problem is to write down the array and index values for each number shown above. Here are the index values for a two by two array:
[0][0], [0][1]
[1][0], [1][1]
Using
these index values the number 3 above comes from array1[0][0]
The 1 comes from array2[0][0]. These two values would be multiplied together and then added to the product of the next two numbers 4 and -2. The final answer is then stored in array3. Setup your first equation and see if the answer matches the -5 that is provided. If it does then your first line works. Now copy the line but change the index values so the correct numbers are pulled from array1 and array2. Repeat this process until you have all four calculations completed.
Use the values shown below to test your code:
One last method and your Matrix program will be complete.
Write a program that take in two matrices and then allow the user to choose to add, subtract, or multiply them and display the answer. The program will display the following menu:
1. Enter Matrix A - display the Matrix so the output look like the actual matrix, not just a list of numbers.
2. Enter Matrix B - display the Matrix so the output look like the actual matrix, not just a list of numbers.
3. Display A + B - display the output so it is in the form of the actual matrix answer, not just a list of numbers.
4. Display A - B
5. Display A * B
6. Exit
The program should loop and allow the user to continue to choose different options until they choose quit. The well written program will modularize the process into different methods.
Suggested methods:
fillArray(Scanner input, int[][] array)
printArray(int
[][] array)
addArray(int[][]array1, int[][]array2, int[][] array3)
subtractArray(int[][]array1, int[][]array2, int[][] array3)
multiplyArray(int[][]array1, int[][]array2, int[][] array3)