Transpose elements in an array

import java.io.*;
class Transpose
{
public static void main(String a[])throws Exception
{
tmp ob=new tmp();
ob.transpose();
}
}
class arr
{
int a[][],n,i,j,k,l,m;
public void inp()throws Exception
{
DataInputStream dd= new DataInputStream(System.in);
System.out.println("Enter a value");
n=Integer.parseInt(dd.readLine());
a=new int[n][n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
a[i][j]=Integer.parseInt(dd.readLine());
}

public void display()
{

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
System.out.println();
System.out.println();

}
}

class tmp extends arr
{
public void transpose()throws Exception
{
super.inp();
super.display();
j=0;
for(i=0;i<super.n;i++)
{j=j+1;
for(;j<super.n;j++)
ex(i,j,j,i);
display();
}
}

private void ex(int b,int c,int d,int e)
{
int t=super.a[b][c];
System.out.println(super.a[b][c]);
System.out.println(super.a[d][e]);

super.a[b][c]=super.a[d][e];
super.a[d][e]=t;
}

}

Post a Comment