-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathprogram.java
More file actions
58 lines (39 loc) · 1.21 KB
/
program.java
File metadata and controls
58 lines (39 loc) · 1.21 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
//Java Program to Find Transpose of a Matrix//
import java.util.Arrays;
public class Matrix {
// main method
public static void main(String[] args) {
// declare and initialize a matrix
int a[][] = { { 1, 2 }, { 8, 9 } };
// find row and column size
int row = a.length;
int column = a[0].length;
// declare new matrix to store result
int transpose[][] = new int[row][column];
// Transpose of matrix
transpose = transposeMatrix(a);
// display all matrices
System.out.println("A = " + Arrays.deepToString(a));
System.out.println("Transpose = " +
Arrays.deepToString(transpose));
}
// method to calculate the transpose of a matrix
public static int[][] transposeMatrix(int[][] a) {
// calculate row and column size
int row = a.length;
int column = a[0].length;
// declare a matrix to store resultant
int temp[][] = new int[row][column];
// calculate transpose of matrix
// outer loop for row
for (int i = 0; i < row; i++) {
// inner loop for column
for (int j = 0; j < column; j++) {
// formula
temp[i][j] = a[j][i];
}
}
// return resultant matrix
return temp;
}
}