-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathtranspose.java
150 lines (114 loc) · 3.07 KB
/
transpose.java
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
/*
Title : Find the transpose of the matrix .
User needs to provide the number of rows and columns .
And the user needs to provide the elements of the matrix .
User will get the transpose of the matrix .
i.e Row elements become the column elements and vice- versa .
**** Maximum 10 number of rows and columns can be only entered .
*/
import java.util.Scanner;
import java.lang.*;
class transpose
{
final static int MAXLIMIT=10 ;
private int row;
private int col;
int i ,j ;
private double a[][];
transpose(int rows , int cols) //Constructor
{
row=rows ;
col=cols ;
a=new double[row][col] ;
}
Scanner in=new Scanner(System.in);
public void get ()
{
System.out.println("\n Enter the elements : \n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
a[i][j]=in.nextDouble();
}
}
}
public void display ()
{
System.out.println( "\n Matrix A " + row + " * " + col + " : \n") ;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
System.out.print(a[i][j]+ "\t") ;
}
System.out.println("\n") ;
}
}
public void trans()
{
System.out.println( "\n\n The transpose of the given matrix : \n");
System.out.println( "\n Matrix AT " + col + " * " + row+ " : \n") ;
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
{
System.out.print(a[j][i]+ "\t") ;
}
System.out.println("\n") ;
}
}
public static void main (String [] args )
{
int rows , cols ;
int opt;
Scanner in=new Scanner(System.in);
do
{ opt=0;
System.out.println("\n Enter the total number of rows and columns respectively : \n");
rows=in.nextInt();
cols=in.nextInt();
if(rows>MAXLIMIT|| cols>MAXLIMIT|| rows<1||cols<1)
{
System.out.println("\n Invalid enteries ! \n Remember , Maximum limit of rows and columns is 10 .\n");
System.out.println(" Do you wish to continue ? \n Press 1 if yes otherwise any to stop . \n");
opt=in.nextInt();
}
else
{
transpose A = new transpose(rows , cols);
A.get();
A.display();
A.trans();
}
} while(opt==1);
}
}
/*
Enter the total number of rows and columns respectively :
13
1
Invalid enteries !
Remember , Maximum limit of rows and columns is 10 .
Do you wish to continue ?
Press 1 if yes otherwise any to stop .
1
Enter the total number of rows and columns respectively :
3
2
Enter the elements :
1
2
3
5
5
5
Matrix A 3 * 2 :
1.0 2.0
3.0 5.0
5.0 5.0
The transpose of the given matrix : \n");
Matrix AT 2 * 3 :
1.0 3.0 5.0
2.0 5.0 5.0
*/