-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathExercise08_06.java
106 lines (76 loc) · 3.01 KB
/
Exercise08_06.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package ch_08;
import java.util.Scanner;
/**
* 8.6 (Algebra: multiply two matrices)
* Write a method to multiply two matrices. The header of the method is:
* <p>
* public static double[][] multiplyMatrix(double[][] a, double[][] b)
* <p>
* 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 + � + ain � bnj. For example, for two
* 3 � 3 matrices a and b, c is..............
* <p>
* Write a test program that prompts the user
* to enter two 3 � 3 matrices and displays
* their product.
*/
public class Exercise08_06 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double[][] matrix1 = new double[3][3];
System.out.println("Enter the values for the first 3 x 3 matrix: ");
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[i].length; j++) {
matrix1[i][j] = input.nextDouble();
}
}
double[][] matrix2 = new double[3][3];
System.out.println("Enter the values for the second 3 x 3 matrix: ");
for (int i = 0; i < matrix2.length; i++) {
for (int j = 0; j < matrix2[i].length; j++) {
matrix2[i][j] = input.nextDouble();
}
}
double[][] newMatrix = multiplyMatrix(matrix1, matrix2);
System.out.println("The multiplication of the two matrices is as follows:");
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[i].length; j++) {
System.out.print(matrix1[i][j] + " ");
if (i == 1 && j == 2) {
System.out.print(" * ");
} else if (i != 1 && j == 2) {
System.out.print(" ");
}
}
for (int j = 0; j < matrix2[i].length; j++) {
System.out.print(matrix2[i][j] + " ");
if (i == 1 && j == 2) {
System.out.print(" = ");
} else if (i != 1 && j == 2) {
System.out.print(" ");
}
}
for (int j = 0; j < newMatrix[i].length; j++) {
System.out.print(newMatrix[i][j] + " ");
if (i != 1 && j == 2) {
System.out.print(" ");
}
}
System.out.println();
}
}
public static double[][] multiplyMatrix(double[][] a, double[][] b) {
double[][] result = new double[3][3];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
for (int z = 0; z < a.length; z++) {
result[i][j] += a[i][z] * b[z][j];
}
}
}
return result;
}
}