Wednesday, January 16, 2008

[2D array] TransPose of array

transpose

2 3 2 4
4 5 becomes 3 5



import java.io.*;
class Transpose
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[][]=new int[2][3];
int b[][]=new int[3][2];
System.out.println(" Enter 6 elements for array one :");
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println(" Matrix ");
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println(" Trasposed ");
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
b[j][i]=a[i][j];
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
System.out.print(b[i][j]+"\t");
}
System.out.println();
}
}
}

No comments: