-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTransPose.java
More file actions
62 lines (54 loc) · 1.86 KB
/
TransPose.java
File metadata and controls
62 lines (54 loc) · 1.86 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
package MatrixProb;
import java.util.Scanner;
public class TransPose {
private int row, col;
private int[][] matrix, transpose;
// Constructor to initialize the matrix
public TransPose(int row, int col) {
this.row = row;
this.col = col;
this.matrix = new int[row][col];
this.transpose = new int[col][row]; // Transpose matrix dimensions are swapped
}
// Method to input matrix elements
public void input() {
Scanner sc = new Scanner(System.in);
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
matrix[i][j] = sc.nextInt();
}
}
}
// Method to calculate the transpose of the matrix
public void calcTranspose() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
transpose[j][i] = matrix[i][j];
}
}
}
// Method to display a matrix
public void display(int[][] mat, int r, int c, String message) {
System.out.println(message);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}
// Main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("ENTER THE ROW OF MATRIX");
int row = sc.nextInt();
System.out.println("ENTER THE COLUMN OF MATRIX");
int col = sc.nextInt();
TransPose tp = new TransPose(row, col);
tp.input();
tp.display(tp.matrix, row, col, "ORIGINAL MATRIX:");
tp.calcTranspose();
tp.display(tp.transpose, col, row, "TRANSPOSE OF MATRIX:");
}
}