Saturday, February 14, 2015

Transpose in a java pgm



Transpose.java

public class Transpose
{
               public static void main(String[] args)
               {
        int N = Integer.parseInt(args[0]);
        int[][] a = new int[N][N];
        for (int i = 0; i < N; i++)
   {
            for (int j = 0; j < N; j++)
    {
                a[i][j] = N*i + j;
            }
        }
        System.out.println("Before");
        System.out.println("------");
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
        {
                System.out.printf("%4d", a[i][j]);
            }
            System.out.println();
        }

        for (int i = 0; i < N; i++)
         {
            for (int j = i+1; j < N; j++)
        {
                int temp = a[i][j];
                a[i][j] = a[j][i];
                a[j][i] = temp;
            }
        }

        System.out.println();
        System.out.println("After");
        System.out.println("------");
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
         {
                System.out.printf("%4d", a[i][j]);
            }
            System.out.println();
        }
    }
}

No comments:

Post a Comment