- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSub_Matrix.java
More file actions
Latest commit
43 lines (37 loc) · 1.31 KB
/
Copy pathSub_Matrix.java
File metadata and controls
43 lines (37 loc) · 1.31 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
publicclassSub_Matrix
{
publicstaticvoidmain(String[] args) {
introws, cols;
//Initialize matrix a
inta[][] = {
{4, 5, 6},
{3, 4, 1},
{1, 2, 3}
};
//Initialize matrix b
intb[][] = {
{2, 0, 3},
{2, 3, 1},
{1, 1, 1}
};
//Calculates number of rows and columns present in given matrix
rows = a.length;
cols = a[0].length;
//Array diff will hold the result
intdiff[][] = newint[rows][cols];
//Performs subtraction of matrices a and b. Store the result in matrix diff
for(inti = 0; i < rows; i++){
for(intj = 0; j < cols; j++){
//(+ for add)./(- for sub)/(* for multiplication)/(/ for division)
diff[i][j] = a[i][j] - b[i][j];
}
}
System.out.println("Subtraction of two matrices: ");
for(inti = 0; i < rows; i++){
for(intj = 0; j < cols; j++){
System.out.print(diff[i][j] + " ");
}
System.out.println();
}
}
}