In: Computer Science
Lab # 4 Multidimensional Arrays
Please write in JAVA.
Programming Exercise 8.5 (Algebra: add two matrices)
Write a method to add two matrices. The header of the method is as follows:
public static double[][] addMatrix(double[][] a, double[][] b
In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each element cij is aij + bij. For example, for two 3 * 3 matrices a and b, c is
Write a test program that prompts the user to enter two 3 * 3 matrices and displays their sum. Here is a sample run
Example run:
Enter matrix1: 1 2 3 4 5 6 7 8 9 (enter)
Enter martix2: 0 2 4 1 4.5 2.2 1.1 4.3 5.2 (enter)
The matrices are added as follows:
1.0 2.0 3.0 0.0 2.0 4.0 1.0 4.0 7.0
4.0 5.0 6.0 + 1.0 4.5 2.2 = 5.0 9.5 8.2
7.0 8.0 9.0 1.1 4.3 5.2 8.1 12.3 14.2
Programming Exercise 8.6 (Algebra: multiply two matrices)
Write a method to multiply two matrices. The header of the method is:
public static double[][] multiplyMatrix(double[][] a, double[][] b)
To multiply 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 the column size of matrix a is n. Each element cij is ai1 * b1j + ai2 * b2j + c + ain * bnj. For example, for two 3 * 3 matrices a and b, c is
where cij = ai1 * b1j + ai2 * b2j + ai3 * b3j. Write a test program that prompts the user to enter two 3 * 3 matrices and displays their product. Here is a sample run:
Example run:
Enter matrix1: 1 2 3 4 5 6 7 8 9 (enter)
Enter matrix2: 0 2 4 1 4.5 2.2 1.1 4.3 5.2 (enter)
The multiplication of the matrices is
1 2 3 0 2.0 4.0 5.3 23.9 24
4 5 6 * 1 4.5 2.2 = 11.6 56.3 58.2
7 8 9 1.1 4.3 5.2 17.9 88.7 92.4
import java.util.Scanner; public class AddMatrix { public static double[][] addMatrix(double[][] a, double[][] b) { double[][] result = new double[a.length][a[0].length]; for(int i = 0; i < a.length; ++i) { for(int j = 0; j < a[i].length; ++j) { result[i][j] = a[i][j] + b[i][j]; } } return result; } public static void main(String[] args) { Scanner in = new Scanner(System.in); double[][] matrix1 = new double[3][3]; double[][] matrix2 = new double[3][3]; System.out.print("Enter matrix1: "); for(int i = 0; i < matrix1.length; ++i) { for(int j = 0; j < matrix1[i].length; ++j) { matrix1[i][j] = in.nextDouble(); } } System.out.print("Enter matrix2: "); for(int i = 0; i < matrix2.length; ++i) { for(int j = 0; j < matrix2[i].length; ++j) { matrix2[i][j] = in.nextDouble(); } } double[][] matrix3 = addMatrix(matrix1, matrix2); System.out.println("\nThe addition of the matrices is"); double minSum = 0; int minRow = 0; for(int i = 0; i < 3; ++i) { for(int j = 0; j < 3; ++j) { System.out.printf("%5.1f\t", matrix1[i][j]); } if(i == 1) { System.out.printf(" + "); } else { System.out.printf(" "); } for(int j = 0; j < 3; ++j) { System.out.printf("%5.1f\t", matrix2[i][j]); } if(i == 1) { System.out.printf(" = "); } else { System.out.printf(" "); } for(int j = 0; j < 3; ++j) { System.out.printf("%5.1f\t", matrix3[i][j]); } double sum = 0; for(int j = 0; j < 3; ++j) { sum += matrix3[i][j]; } if (i == 0 || sum < minSum) { minSum = sum; } System.out.printf("\n"); } } }
import java.util.Scanner; public class MultiplyMatrix { public static double[][] multiplyMatrix(double[][] a, double[][] b) { double[][] result = new double[a.length][b[0].length]; for(int i = 0; i < a.length; ++i) { for(int j = 0; j < b[i].length; ++j) { double total = 0; for(int k = 0; k < a[i].length; ++k) { total += (a[i][k] * b[k][j]); } result[i][j] = total; } } return result; } public static void main(String[] args) { Scanner in = new Scanner(System.in); double[][] matrix1 = new double[3][3]; double[][] matrix2 = new double[3][3]; System.out.print("Enter matrix1: "); for(int i = 0; i < matrix1.length; ++i) { for(int j = 0; j < matrix1[i].length; ++j) { matrix1[i][j] = in.nextDouble(); } } System.out.print("Enter matrix2: "); for(int i = 0; i < matrix2.length; ++i) { for(int j = 0; j < matrix2[i].length; ++j) { matrix2[i][j] = in.nextDouble(); } } double[][] matrix3 = multiplyMatrix(matrix1, matrix2); for(int i = 0; i < 3; ++i) { for(int j = 0; j < 3; ++j) { System.out.printf("%5.1f\t", matrix1[i][j]); } if(i == 1) { System.out.printf(" * "); } else { System.out.printf(" "); } for(int j = 0; j < 3; ++j) { System.out.printf("%5.1f\t", matrix2[i][j]); } if(i == 1) { System.out.printf(" = "); } else { System.out.printf(" "); } for(int j = 0; j < 3; ++j) { System.out.printf("%5.1f\t", matrix3[i][j]); } System.out.printf("\n"); } } }