-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransposeMatrix.java
More file actions
62 lines (51 loc) · 1.49 KB
/
TransposeMatrix.java
File metadata and controls
62 lines (51 loc) · 1.49 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
/*
* PROGRAM : To find the transpose of a matrix
* FILE : TransposeMatrix.java
* CREATED BY : Santosh Hembram
* DATE : 12-10-20
*/
import java.util.*;
class TMatrix {
public int[][] transpose(int mat[][],int r,int c) {
int newMat[][] = new int[c][r];
for (int i=0; i<r; i++) {
for (int j=0; j<c; j++) {
newMat[j][i] = mat[i][j];
}
}
return newMat;
}
}
class TransposeMatrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the row size: ");
int r = sc.nextInt();
System.out.print("Enter the coloumn size: ");
int c = sc.nextInt();
int mat[][] = new int[r][c];
System.out.println("---------- Enter the elements of the matrix --------------");
for(int i=0; i<r; i++) {
for (int j=0; j<c; j++) {
System.out.print("Enter the elements for row "+i+" coloumn "+j+": ");
mat[i][j] = sc.nextInt();
}
}
System.out.println("---------- Displaying the matrix --------------");
for(int i=0; i<r; i++) {
for (int j=0; j<c; j++) {
System.out.print(mat[i][j]+" ");
}
System.out.println();
}
TMatrix obj = new TMatrix();
int tmatrix[][] = obj.transpose(mat,r,c);
System.out.println("---------- Displaying the Transpose matrix --------------");
for(int i=0; i<c; i++) {
for (int j=0; j<r; j++) {
System.out.print(tmatrix[i][j]+" ");
}
System.out.println();
}
}
}