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
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:
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Program1.java (code for Exercise 8.5)
import java.util.Scanner;
public class Program1 {
// required method to add two matrices
public static double[][] addMatrix(double[][] a, double[][] b) {
if (a.length != b.length || a[0].length != b[0].length) {
return null; // cannot be added
}
// creating a matrix to store results
double[][] result = new double[a.length][a[0].length];
// looping and adding corresponding elements and saving to result
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}
return result;
}
// a helper method defined to read elements to a matrix
static void readMatrix(double matrix[][]) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = scanner.nextDouble();
}
}
}
// a helper method defined to print elements of a matrix
static void printMatrix(double matrix[][]) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
// creating two matrices
double a[][] = new double[3][3];
double b[][] = new double[3][3];
// asking and reading two matrices
System.out.println("Enter first matrix (3x3)");
readMatrix(a);
System.out.println("Enter second matrix (3x3)");
readMatrix(b);
// finding sum
double c[][] = addMatrix(a, b);
// displaying resultant matrix
System.out.println("\nSum of two matrices:");
printMatrix(c);
}
}
// Program2.java (code for Exercise 8.6)
import java.util.Scanner;
public class Program2 {
// required method to multiply two matrices
public static double[][] multiplyMatrix(double[][] a, double[][] b) {
// finding rows and columns count for both matrices
int row1 = a.length;
int col1 = a[0].length;
int row2 = b.length;
int col2 = b[0].length;
if (col1 != row2) {
// multiplication not possible
return null;
}
// creating a row1 x col2 matrix for storing the result
double result[][] = new double[row1][col2];
//looping and multiplying the matrix
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
for (int k = 0; k < col1; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
return result;
}
// a helper method defined to read elements to a matrix
static void readMatrix(double matrix[][]) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = scanner.nextDouble();
}
}
}
// a helper method defined to print elements of a matrix
static void printMatrix(double matrix[][]) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
// creating two matrices
double a[][] = new double[3][3];
double b[][] = new double[3][3];
// asking and reading two matrices
System.out.println("Enter first matrix (3x3)");
readMatrix(a);
System.out.println("Enter second matrix (3x3)");
readMatrix(b);
// finding product
double c[][] = multiplyMatrix(a, b);
// displaying resultant matrix
System.out.println("\nProduct of two matrices:");
printMatrix(c);
}
}
//OUTPUT OF BOTH PROGRAMS
Enter first matrix (3x3)
1 2 3
4 5 6
7 8 9
Enter second matrix (3x3)
1 2 3
4 5 6
7 8 9
Sum of two matrices:
2.0 4.0 6.0
8.0 10.0 12.0
14.0 16.0 18.0
Enter first matrix (3x3)
1 2 3
4 5 6
7 8 9
Enter second matrix (3x3)
1 1 2
3 3 4
0 0 1
Product of two matrices:
7.0 7.0 13.0
19.0 19.0 34.0
31.0 31.0 55.0