-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest5-Matrix Class
150 lines (141 loc) · 2.96 KB
/
Test5-Matrix Class
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
Write a code to perform different operations on matrix.
1. Addition
You are given two matrices return the addition of these two matrices.
2. Multiplication
Given two matrices return the matrix multiplication of them.(Both the matrices will always be multiplicable).
3. Transpose
Given a Matrix calculate the transpose of the matrix and return it. (Number of rows and columns will be same)
4. Rotate by 90
Given a Matrix, rotate the matrix by 90 degree in anticlockwise direction.
Input format :
Line 1: Operation to be performed
Line 2 : No of rows(n1) & No of columns(m1) (separated by space)
Line 3 : Row 1 elements (separated by space)
Line 4 : Row 2 elements (separated by space)
Line 5 : and so on
(If needed)
Line n1+2 : No of rows(n2) & No of columns(m2)(separated by space)
Line n1+3 : Row 1 elements (separated by space)
Line n1+4 : Row 2 elements (separated by space)
Line n1+5 : and so on
Sample Input 1 :
1
2 2
1 2
1 3
2 2
4 3
1 5
Sample output 1 :
5 5
2 8
Sample Input 2 :
2
2 2
1 2
1 3
2 2
4 3
1 5
Sample output 2 :
6 13
7 18
Sample Input 3 :
3
2 2
1 2
1 3
Sample output 3 :
1 1
2 3
Sample Input 4 :
4
2 2
1 2
1 3
Sample output 4 :
2 3
1 1
Note : Partial marking is there, so implementation of each function will give you some marks.
********************************************Solution**********************************************
public class mat{
int [][] matrix;
mat(int [][] mat)
{
matrix=mat;
}
public static mat add(mat a,mat b)
{
for(int i = 0; i < a.matrix.length; i++)
{
for(int j = 0; j < a.matrix[0].length; j++)
{
a.matrix[i][j] =a.matrix[i][j]+ b.matrix[i][j];
}
}
return a;
}
public static mat multiply(mat a,mat b)
{
int c[][]=new int[a.matrix.length][b.matrix.length];
int n=a.matrix.length;
int i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]+=a.matrix[i][k]*b.matrix[k][j];
}
}
}
a.matrix=c;
return a;
}
public static mat transpose(mat m)
{
int n=m.matrix.length;
int transpose[][]=new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
transpose[i][j]=m.matrix[j][i];
}
}
m.matrix=transpose;
return m;
}
public static mat rotate(mat m)
{
m=transpose(m);
int n=m.matrix.length;
for(int i=0;i<n;i++)
{
int low=0;
int high=n-1;
while(low<high)
{
int temp=m.matrix[low][i];
m.matrix[low][i]=m.matrix[high][i];
m.matrix[high][i]=temp;
low++;
high--;
}
}
return m;
}
public void print()
{
for(int i=0;i<matrix.length;i++)
{
for(int j=0;j<matrix[0].length;j++)
{
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
}
}