-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmyMatrix.java
More file actions
125 lines (124 loc) · 3.87 KB
/
myMatrix.java
File metadata and controls
125 lines (124 loc) · 3.87 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
123
124
125
import java.util.Scanner;
class myMatrix {
public static void main(String[] args) {
try {
int ch = 0;
Scanner sc = new Scanner(System.in);
do {
System.out.println("1. Addition ");
System.out.println("2. Subtraction ");
System.out.println("3. Transpose ");
System.out.println("4. Multiplication ");
System.out.println("0. Exit ");
System.out.print("Enter Your Choice :- ");
ch = sc.nextInt();
switch (ch) {
case 1:
System.out.println("Enter the dimension of matrix ");
System.out.println("Rows : ");
int r = sc.nextInt();
System.out.println("Column : ");
int c = sc.nextInt();
int[][] arr1 = new int[r][c];
int[][] arr2 = new int[r][c];
System.out.println("Write the elements for 1st Matrix row wise : ");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
arr1[i][j] = sc.nextInt();
}
}
System.out.println("Write the elements for 2nd Matrix row wise : ");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
arr2[i][j] = sc.nextInt();
}
}
Matrix m = new Matrix();
int[][] arr3 = new int[r][c];
arr3 = m.addition(arr1, arr2, r, c);
m.toString(arr3, r, c);
break;
case 2:
System.out.println("Enter the dimension of matrix ");
System.out.println("Rows : ");
r = sc.nextInt();
System.out.println("Column : ");
c = sc.nextInt();
arr1 = new int[r][c];
arr2 = new int[r][c];
System.out.println("Write the elements for 1st Matrix row wise : ");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
arr1[i][j] = sc.nextInt();
}
}
System.out.println("Write the elements for 2nd Matrix row wise : ");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
arr2[i][j] = sc.nextInt();
}
}
m = new Matrix();
arr3 = new int[r][c];
arr3 = m.subtraction(arr1, arr2, r, c);
m.toString(arr3, r, c);
break;
case 3:
System.out.println("Enter the dimension of matrix ");
System.out.println("Rows : ");
r = sc.nextInt();
System.out.println("Column : ");
c = sc.nextInt();
arr1 = new int[r][c];
System.out.println("Write the elements for the Matrix row wise : ");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
arr1[i][j] = sc.nextInt();
}
}
m = new Matrix();
arr3 = new int[c][r];
arr3 = m.transpose(arr1, r, c);
m.toString(arr3, c, r);
break;
case 4:
System.out.println("Enter the dimension of 1st matrix ");
System.out.println("Rows : ");
r = sc.nextInt();
System.out.println("Column : ");
c = sc.nextInt();
System.out.println("Enter the dimension of 2nd matrix ");
System.out.println("Rows : ");
int r1 = sc.nextInt();
System.out.println("Column : ");
int c1 = sc.nextInt();
arr1 = new int[r][c];
arr2 = new int[r1][c1];
System.out.println("Write the elements for 1st Matrix row wise : ");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
arr1[i][j] = sc.nextInt();
}
}
System.out.println("Write the elements for 2nd Matrix row wise : ");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
arr2[i][j] = sc.nextInt();
}
}
m = new Matrix();
arr3 = new int[r][c1];
arr3 = m.multiplication(arr1, arr2, r, c, r1, c1);
m.toString(arr3, r, c);
break;
default:
System.out.println("Wrong Choice !! Exiting");
break;
}
} while (ch != 0);
sc.close();
} catch (Exception c) {
System.out.println("Sorry! " + c);
}
}
}