-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart_3.java.java
More file actions
122 lines (98 loc) · 3.17 KB
/
Copy pathPart_3.java.java
File metadata and controls
122 lines (98 loc) · 3.17 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Part_3 {
public static void main(String[] args) {
// Declaring File objects
File file = null;
File outputFile = null;
// File input and output names
file = new File("C:\\Users\\Paul\\Desktop\\Program1\\Part3\\file6.txt");
outputFile = new File("C:\\Users\\Paul\\Desktop\\Program1\\Part3\\Output.txt");
// Declaring input and output objects
FileWriter fw = null;
PrintWriter pw = null;
FileInputStream fis = null;
try {
fw = new FileWriter(outputFile);
} catch (IOException e1) {
System.out.println("Could not find file");
e1.printStackTrace();
}
pw = new PrintWriter(fw);
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.out.println("Could not find file");
e.printStackTrace();
}
Scanner input = new Scanner(fis);
// m is the length of matrix 1 and matrix x is size m*m
int m = input.nextInt();
int[][] x = new int[m][m];
// Enter values of matrix 1 into matrix x
for (int i = 0; i < x.length; i++) {
for (int j = 0; j < x.length; j++) {
x[i][j] = input.nextInt();
}
}
// n is the length of matrix 2 and matrix y is size n*n
int n = input.nextInt();
int[][] y = new int[n][n];
// Enter values of matrix 2 into matrix y
for (int i = 0; i < y.length; i++) {
for (int j = 0; j < y.length; j++) {
y[i][j] = input.nextInt();
}
}
// ====================Calculate the sum of the matrices====================
// Create a matrix u of size m*m
int[][] u = new int[m][m];
// Loop through each row and column of matrix x and add to the
// respective value in matrix y
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
u[i][j] = x[i][j] + y[i][j];
}
}
// Return m and matrix u (the sum of the matrices)
pw.println("m: " + m);
for (int i = 0; i < u.length; i++) {
for (int j = 0; j < u[i].length; j++) {
pw.print(u[i][j] + " ");
}
pw.println();
}
// formatting
pw.println();
pw.println();
// ====================Calculate the product of the matrices====================
// Create a matrix v of size m*m
int[][] v = new int[m][m];
// Loop through each row and column of matrix x and multiply by the
// respective value in matrix y
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
int temp = 0;
for (int k = 0; k < m; k++) {
temp = temp + x[i][k] * y[k][j];
v[i][j] = temp;
}
}
}
// Return m and matrix v (the product of the matrices)
pw.println("m: " + m);
for (int i = 0; i < v.length; i++) {
for (int j = 0; j < v[i].length; j++) {
pw.print(v[i][j] + " ");
}
pw.println();
}
pw.close();
input.close();
}
}