Skip to content

Commit 43117f1

Browse files
authored
Merge pull request #774 from Mayuri-cell/master
Transpose of a matrix
2 parents dbbe530 + ec3344a commit 43117f1

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

2D Arrays/transpose.java

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
Title : Find the transpose of the matrix .
3+
User needs to provide the number of rows and columns .
4+
And the user needs to provide the elements of the matrix .
5+
User will get the transpose of the matrix .
6+
i.e Row elements become the column elements and vice- versa .
7+
**** Maximum 10 number of rows and columns can be only entered .
8+
*/
9+
import java.util.Scanner;
10+
import java.lang.*;
11+
class transpose
12+
{
13+
final static int MAXLIMIT=10 ;
14+
private int row;
15+
private int col;
16+
int i ,j ;
17+
private double a[][];
18+
19+
transpose(int rows , int cols) //Constructor
20+
{
21+
row=rows ;
22+
col=cols ;
23+
a=new double[row][col] ;
24+
25+
}
26+
27+
Scanner in=new Scanner(System.in);
28+
29+
public void get ()
30+
{
31+
System.out.println("\n Enter the elements : \n");
32+
for(i=0;i<row;i++)
33+
{
34+
for(j=0;j<col;j++)
35+
{
36+
a[i][j]=in.nextDouble();
37+
}
38+
}
39+
40+
}
41+
42+
public void display ()
43+
{
44+
System.out.println( "\n Matrix A " + row + " * " + col + " : \n") ;
45+
46+
for(i=0;i<row;i++)
47+
{
48+
for(j=0;j<col;j++)
49+
{
50+
System.out.print(a[i][j]+ "\t") ;
51+
}
52+
System.out.println("\n") ;
53+
}
54+
}
55+
56+
public void trans()
57+
{
58+
System.out.println( "\n\n The transpose of the given matrix : \n");
59+
System.out.println( "\n Matrix AT " + col + " * " + row+ " : \n") ;
60+
61+
for(i=0;i<col;i++)
62+
{
63+
for(j=0;j<row;j++)
64+
{
65+
System.out.print(a[j][i]+ "\t") ;
66+
}
67+
System.out.println("\n") ;
68+
}
69+
}
70+
71+
public static void main (String [] args )
72+
{
73+
int rows , cols ;
74+
int opt;
75+
Scanner in=new Scanner(System.in);
76+
do
77+
{ opt=0;
78+
System.out.println("\n Enter the total number of rows and columns respectively : \n");
79+
rows=in.nextInt();
80+
cols=in.nextInt();
81+
if(rows>MAXLIMIT|| cols>MAXLIMIT|| rows<1||cols<1)
82+
{
83+
System.out.println("\n Invalid enteries ! \n Remember , Maximum limit of rows and columns is 10 .\n");
84+
System.out.println(" Do you wish to continue ? \n Press 1 if yes otherwise any to stop . \n");
85+
opt=in.nextInt();
86+
}
87+
88+
else
89+
{
90+
91+
transpose A = new transpose(rows , cols);
92+
A.get();
93+
A.display();
94+
A.trans();
95+
96+
97+
}
98+
} while(opt==1);
99+
100+
}
101+
102+
}
103+
104+
/*
105+
106+
Enter the total number of rows and columns respectively :
107+
108+
13
109+
1
110+
111+
Invalid enteries !
112+
Remember , Maximum limit of rows and columns is 10 .
113+
114+
Do you wish to continue ?
115+
Press 1 if yes otherwise any to stop .
116+
117+
1
118+
119+
Enter the total number of rows and columns respectively :
120+
121+
3
122+
2
123+
124+
Enter the elements :
125+
126+
1
127+
2
128+
3
129+
5
130+
5
131+
5
132+
133+
Matrix A 3 * 2 :
134+
135+
1.0 2.0
136+
137+
3.0 5.0
138+
139+
5.0 5.0
140+
141+
142+
The transpose of the given matrix : \n");
143+
144+
Matrix AT 2 * 3 :
145+
146+
1.0 3.0 5.0
147+
148+
2.0 5.0 5.0
149+
150+
*/

0 commit comments

Comments
 (0)