-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranpose.java
More file actions
54 lines (53 loc) · 1.48 KB
/
Tranpose.java
File metadata and controls
54 lines (53 loc) · 1.48 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
// Program: Transpose of a Square Matrix
// Topic: 2D Arrays and Matrix Manipulation
// Description: Reads an m×m matrix from user input and computes its transpose by interchanging rows and columns.
// Demonstrates use of nested loops for matrix traversal and creation of a new transposed matrix.
package Array2D;
import java.util.*;
/**
*
* @author Samim
*/
public class Tranpose {
int ar[][];
int arr[][];
int m;
public Tranpose(int mm) {
m=mm;
ar=new int[m][m];
arr=new int[m][m];
}
void fillarray(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter Elements of Array :");
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
ar[i][j]=sc.nextInt();
}
}
}
void transpose(){
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
arr[j][i]=ar[i][j];
arr[i][j]=ar[j][i];
}
}
}
void display(){
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of matrix");
Tranpose obj=new Tranpose(sc.nextInt());
obj.fillarray();
obj.transpose();
obj.display();
}
}