about summary refs log tree commit diff
path: root/usth/ICT2.2/labwork/4/Transpose.java
blob: 597a7837e04cdd87d304a81b9c11b2170a0892a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Transpose
{
  public static void main(String... args)
  {
    int[][] m = {{7, 8, 9},
                 {4, 5, 6},
                 {1, 2, 3}};
    int n = 3;  // some sort of abstraction
    System.out.println("Original matrix:");
    for (int i = 0; i < n; ++i)
      System.out.printf("%d %d %d\n", m[i][0], m[i][1], m[i][2]);

    for (int i = 1; i < n; ++i)
      for (int j = 0; j < i; ++j)
        {
          m[i][j] ^= m[j][i];
          m[j][i] ^= m[i][j];
          m[i][j] ^= m[j][i];
        }
    System.out.println("Transposed matrix:");
    for (int i = 0; i < n; ++i)
      System.out.printf("%d %d %d\n", m[i][0], m[i][1], m[i][2]);
  }
}