-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathmatrixProblems.java
More file actions
184 lines (184 loc) · 5.61 KB
/
matrixProblems.java
File metadata and controls
184 lines (184 loc) · 5.61 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("1. Add matrices\n" + "2. Multiply matrix by a constant\n" + "3. Multiply matrices\n" +"4. Transpose matrix\n"+ "0. Exit");
while(true)
{
switch(sc.nextInt())
{
case 1: addition();
break;
case 2: multiplyConstant();
break;
case 3: multiplyMatrix();
break;
case 4: transposeMatrix();
break;
case 0:return;
}
}
}
public static void addition()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of first matrix:");
int n1=sc.nextInt();
int m1=sc.nextInt();
double[][] one=new double[n1][m1];
int i,j;
System.out.println("Enter first matrix:");
for( i=0;i<n1;i++)
for( j=0;j<m1;j++)
one[i][j]=sc.nextDouble();
System.out.println("Enter size of second matrix:");
int n2=sc.nextInt();
int m2=sc.nextInt();
System.out.println("Enter second matrix:");
double[][] two=new double[n2][m2];
double[][] sum=new double[n2][m2];
for(i=0;i<n2;i++) {
for (j = 0; j < m2; j++) {
two[i][j] = sc.nextDouble();
sum[i][j] = one[i][j] + two[i][j];
}
}
if(n1!=n2||m1!=m2)
{
System.out.println("ERROR");
return;
}
System.out.println("The result is:");
for( i=0;i<n1;i++) {
for( j=0;j<m1;j++) {
System.out.print(sum[i][j]+" ");
}
System.out.println();
}
}
public static void multiplyConstant()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of matrix:");
int n1=sc.nextInt();
int m1=sc.nextInt();
double[][] one=new double[n1][m1];
int i,j;
System.out.println("Enter matrix:");
for( i=0;i<n1;i++)
for( j=0;j<m1;j++)
one[i][j]=sc.nextDouble();
System.out.println("Enter constant:");
double multi=sc.nextInt()*1.0;
System.out.println("The result is:");
for( i=0;i<n1;i++) {
for (j = 0; j < m1; j++)
System.out.print(one[i][j] * multi+" ");
System.out.println();
}
}
public static void multiplyMatrix()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of matrix:");
int n1=sc.nextInt();
int m1=sc.nextInt();
double[][] one=new double[n1][m1];
int i,j,k;
System.out.println("Enter matrix:");
for( i=0;i<n1;i++)
for( j=0;j<m1;j++)
one[i][j]=sc.nextDouble();
System.out.println("Enter size of second matrix");
int n2=sc.nextInt();
int m2=sc.nextInt();
double[][] two=new double[n2][m2];
System.out.println("Enter matrix:");
for( i=0;i<n2;i++)
for( j=0;j<m2;j++)
two[i][j]=sc.nextDouble();
if(m1!=n2)
{
System.out.println("ERROR");
return;
}
System.out.println("The result is:");
double[][] result=new double[n1][m2];
for(i=0;i<n1;i++)
{
for(j=0;j<m2;j++)
{
for(k=0;k<n2;k++)
result[i][j]+=(one[i][k]*two[k][j]);
}
}
for(i=0;i<n1;i++)
{
for(j=0;j<m2;j++)
System.out.print(result[i][j]+" ");
System.out.println();
}
}
public static void transposeMatrix() {
System.out.println("1. Main diagonal\n" + "2. Side diagonal\n" + "3. Vertical line\n" + "4. Horizontal line");
Scanner sc=new Scanner(System.in);
int x = sc.nextInt();
System.out.println("Enter matrix size:");
int row=sc.nextInt();
int col=sc.nextInt();
System.out.println("Enter matrix:");
int i,j;
double[][] arr=new double[row][col];
for(i=0;i<row;i++)
for(j=0;j<col;j++)
arr[i][j]=sc.nextDouble();
System.out.println("The result is:");
switch (x)
{
case 1: mainD(arr,row,col);
break;
case 2: sideD(arr,row,col);
break;
case 3: vertD(arr,row,col);
break;
case 4: horiD(arr,row,col);
}
}
public static void mainD(double[][] arr,int row,int col) {
int i, j;
for (i = 0; i < row; i++) {
for (j = 0; j < col;j++)
System.out.print(arr[j][i]+" ");
System.out.println();
}
}
public static void sideD(double[][] arr,int row,int col) {
int i,j;
for(i=row-1;i>-1;i--)
{
for(j=col-1;j>-1;j--)
System.out.print(arr[j][i]+" ");
System.out.println();
}
}
public static void vertD(double[][] arr,int row,int col)
{
int i,j;
for(i=0;i<row;i++)
{
for(j=col-1;j>-1;j--)
System.out.print(arr[i][j]+" ");
System.out.println();
}
}
public static void horiD(double[][] arr,int row,int col)
{
int i,j;
for(i=row-1;i>-1;i--)
{
for(j=0;j<col;j++)
System.out.print(arr[i][j]+" ");
System.out.println();
}
}
}