Skip to content

Commit b2f033b

Browse files
authored
Merge branch 'smv1999:master' into master
2 parents 145815f + 28577da commit b2f033b

27 files changed

+2011
-160
lines changed

2D Arrays/matrices.java

Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
1+
/* Title : Operations on matrices .
2+
3+
Description : User need to enter the elements of the matrix A and B .
4+
Operations will be done on the matrices : 1.To add 2.To Subtract
5+
3.To multiply . User need to choose it.
6+
The maximum limit of total number of rows and columns : 8
7+
You can't generate the matrices having more than 8 number of rows and columns .
8+
9+
Here , if the both matrices are of square matrix only then the multiplication can be
10+
done . i.e Rows(A)==Columns(B) equal fOR BA multiplication
11+
and Rows(B)==Columns(A) equal fOR BA multiplication . multiplication rule.
12+
This is done here to maintain the formate of the code and to maintain the
13+
multiplication of matices .
14+
Addition and subtraction are done as the Matrix A and B both are having same number of rows and columns .
15+
*/
16+
import java.util.Scanner;
17+
public class matrices
18+
{
19+
public final static int MAXLIMIT=8;
20+
public static void main(String[] args)
21+
{
22+
Scanner input=new Scanner (System.in);
23+
int opt ;
24+
25+
do // Enter the data needed .
26+
{
27+
System.out.println(" Enter the number of rows and columns (Maximum limit is 8 ) : \n");
28+
int row=input.nextInt();
29+
int col=input.nextInt();
30+
double a[][]=new double[row][col];
31+
double b[][]=new double[row][col];
32+
33+
34+
if((row>MAXLIMIT)||(col>MAXLIMIT)||(row<1)||(col<1))
35+
{ System.out.println("\n Invalid Enteries !! \n Please try again later .");
36+
System.exit(0);
37+
}
38+
else
39+
{ System.out.println("\n ENTER THE ELEMENTS OF MATRIX A "); // Enter Matrix A
40+
41+
for(int i=0;i<row;i++)
42+
{
43+
for(int j=0;j<col;j++)
44+
45+
{ a[i][j]=input.nextDouble();
46+
47+
} }
48+
49+
50+
System.out.println("\n ENTER THE ELEMENTS OF MATRIX B"); // Enter Matrix B
51+
52+
for(int i=0;i<row;i++)
53+
{
54+
for(int j=0;j<col;j++)
55+
56+
{ b[i][j]=input.nextDouble(); } }
57+
58+
}
59+
60+
System.out.println();
61+
62+
System.out.println(" ELEMENTS OF MATRIX A"); // Matrix A
63+
for(int i=0;i<row;i++)
64+
{
65+
for(int j=0;j<col;j++)
66+
{ System.out.print( a[i][j]+ "\t"); }
67+
System.out.println("\n");
68+
}
69+
70+
System.out.println();
71+
72+
System.out.println(" ELEMENTS OF MATRIX B"); // Matrix B
73+
for(int i=0;i<row;i++)
74+
{
75+
for(int j=0;j<col;j++)
76+
{ System.out.print( b[i][j]+ "\t"); }
77+
System.out.println("\n");
78+
}
79+
80+
81+
82+
int i, j ;
83+
System.out.println("\n1.Add \n2.Subtraction \n3.Multiplication ");
84+
opt=input.nextInt();
85+
System.out.println();
86+
87+
switch(opt)
88+
{
89+
case 1 : double add[][]=new double [row][col]; // Addition of matrices A and B .
90+
System.out.println("Addition of matrix A and B : ");
91+
for(i=0;i<row;i++)
92+
{
93+
for(j=0;j<col;j++)
94+
{ add[i][j]=a[i][j] + b[i][j];
95+
System.out.print(add[i][j] +"\t"); }
96+
System.out.println(); }
97+
System.out.println();
98+
break ;
99+
100+
case 2 : double sub1[][]=new double [row][col]; // Subtraction of matrix A from B .
101+
System.out.println("Subtraction of matrix A from B : ");
102+
for(i=0;i<row;i++)
103+
{
104+
for(j=0;j<col;j++)
105+
{ sub1[i][j]= -a[i][j] + b[i][j];
106+
System.out.print(sub1[i][j] +"\t"); }
107+
System.out.println(); }
108+
109+
System.out.println();
110+
111+
double sub2[][]=new double [row][col]; // Subtraction of matrix B from A .
112+
System.out.println("Subtraction of matrix B from A : ");
113+
for(i=0;i<row;i++)
114+
{
115+
for(j=0;j<col;j++)
116+
{ sub2[i][j]= a[i][j] - b[i][j];
117+
System.out.print(sub2[i][j] +"\t"); }
118+
System.out.println(); }
119+
System.out.println();
120+
break ;
121+
122+
123+
case 3 :
124+
System.out.println("Matrix multiplication is not commutative always.\n i.e A*B is not equals to B*A. \n");
125+
if(row==col)
126+
{
127+
// Multiply matrix A and B . i.e : AB
128+
int k;
129+
double sum=0 ;
130+
double mul1[][]=new double [row][col];
131+
System.out.println(" Multiplication of matrices A and B : AB ");
132+
for(i=0;i<row;i++)
133+
{
134+
for(j=0;j<col;j++)
135+
{
136+
for(k=0;k<col;k++)
137+
{ mul1[i][j]=mul1[i][j] + a[i][k]*b[k][j]; }
138+
}
139+
System.out.println(); }
140+
141+
for(i=0;i<col;i++)
142+
{
143+
for(j=0;j<col;j++)
144+
{ System.out.print(mul1[i][j] +"\t"); }
145+
System.out.println();
146+
}
147+
System.out.println();
148+
149+
150+
// Multiply matrix B and A . i.e : BA
151+
152+
double mul2[][]=new double [row][col];
153+
System.out.println(" Multiplication of matrices A and B : BA ");
154+
for(i=0;i<row;i++)
155+
{
156+
for(j=0;j<col;j++)
157+
{
158+
for(k=0;k<col;k++)
159+
{ mul2[i][j]=mul2[i][j] + b[i][k]*a[k][j]; }
160+
}
161+
System.out.println(); }
162+
163+
for(i=0;i<col;i++)
164+
{
165+
for(j=0;j<col;j++)
166+
{ System.out.print(mul2[i][j] +"\t"); }
167+
System.out.println();
168+
}
169+
}
170+
else
171+
{
172+
System.out.println(" Multiplication is not valid . \n");
173+
}
174+
System.out.println();
175+
break ;
176+
}
177+
178+
System.out.println(" Do you wish to continue ? \n1.Yes 2.No\n" );
179+
opt=input.nextInt();
180+
}while(opt!=2);
181+
182+
}}
183+
184+
185+
/*
186+
187+
Enter the number of rows and columns (Maximum limit is 8 ) :
188+
189+
2
190+
2
191+
192+
ENTER THE ELEMENTS OF MATRIX A
193+
1
194+
2
195+
3
196+
4
197+
198+
ENTER THE ELEMENTS OF MATRIX B
199+
2
200+
2
201+
2
202+
2
203+
204+
ELEMENTS OF MATRIX A
205+
1.0 2.0
206+
207+
3.0 4.0
208+
209+
ELEMENTS OF MATRIX B
210+
2.0 2.0
211+
212+
2.0 2.0
213+
214+
215+
1.Add
216+
2.Subtraction
217+
3.Multiplication
218+
1
219+
220+
Addition of matrix A and B :
221+
3.0 4.0
222+
5.0 6.0
223+
224+
Do you wish to continue ?
225+
1.Yes 2.No
226+
1
227+
228+
Enter the number of rows and columns (Maximum limit is 8 ) :
229+
230+
3
231+
3
232+
233+
ENTER THE ELEMENTS OF MATRIX A
234+
12
235+
13
236+
14
237+
15
238+
16
239+
17
240+
18
241+
19
242+
20
243+
244+
ENTER THE ELEMENTS OF MATRIX B
245+
2
246+
2
247+
2
248+
2
249+
2
250+
2
251+
2
252+
2
253+
2
254+
255+
ELEMENTS OF MATRIX A
256+
12.0 13.0 14.0
257+
258+
15.0 16.0 17.0
259+
260+
18.0 19.0 20.0
261+
262+
ELEMENTS OF MATRIX B
263+
2.0 2.0 2.0
264+
265+
2.0 2.0 2.0
266+
267+
2.0 2.0 2.0
268+
269+
270+
1.Add
271+
2.Subtraction
272+
3.Multiplication
273+
2
274+
275+
Subtraction of matrix A from B :
276+
-10.0 -11.0 -12.0
277+
-13.0 -14.0 -15.0
278+
-16.0 -17.0 -18.0
279+
280+
Subtraction of matrix B from A :
281+
10.0 11.0 12.0
282+
13.0 14.0 15.0
283+
16.0 17.0 18.0
284+
285+
Do you wish to continue ?
286+
1.Yes 2.No
287+
1
288+
289+
Enter the number of rows and columns (Maximum limit is 8 ) :
290+
291+
2
292+
2
293+
294+
ENTER THE ELEMENTS OF MATRIX A
295+
1
296+
2
297+
44
298+
45
299+
300+
ENTER THE ELEMENTS OF MATRIX B
301+
2
302+
2
303+
3
304+
33
305+
306+
ELEMENTS OF MATRIX A
307+
1.0 2.0
308+
309+
44.0 45.0
310+
311+
ELEMENTS OF MATRIX B
312+
2.0 2.0
313+
314+
3.0 33.0
315+
316+
317+
1.Add
318+
2.Subtraction
319+
3.Multiplication
320+
3
321+
322+
Matrix multiplication is not commutative always.
323+
i.e A*B is not equals to B*A .
324+
325+
Multiplication of matrices A and B : AB
326+
327+
328+
8.0 68.0
329+
223.0 1573.0
330+
331+
Multiplication of matrices A and B : BA
332+
333+
334+
90.0 94.0
335+
1455.0 1491.0
336+
337+
Do you wish to continue ?
338+
1.Yes 2.No
339+
2
340+
*/

2D Arrays/sort_matrix_diagonally.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def sortDiagonal(a, M, N):
2+
3+
# Loop to find the ith minimum
4+
# element from the major diagonally
5+
for i in range(M):
6+
sm = a[i][i]
7+
pos = i
8+
9+
# Loop to find the minimum
10+
# element from the unsorted matrix
11+
for j in range(i + 1 , N):
12+
if (sm > a[j][j]):
13+
sm = a[j][j]
14+
pos = j
15+
16+
# Swap to put the minimum
17+
# element at the beginning of
18+
# the major diagonal of matrix
19+
a[i][i], a[pos][pos] = a[pos][pos] , a[i][i]
20+
21+
# Loop to print the matrix
22+
for i in range(M):
23+
for j in range(N):
24+
print(a[i][j],end=" ")
25+
print()
26+
27+
# Driven Code
28+
a = [[4, 2],[3, 1]]
29+
30+
# Sort the major Diagonally
31+
sortDiagonal(a, 2, 2)

0 commit comments

Comments
 (0)