Wednesday, August 17, 2011

Transpose two dimensional array in java


import java.io.*;
class arraytranspose
{
 public static void main(String[] arg)
  {
   int m,n;
   BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
   int arr[ ][ ]=new int[30][30];
    try
    {
     System.out.print("Enter the number of row: ");
      m=Integer.parseInt(in.readLine());
     System.out.print("Enter the number of column: ");
      n=Integer.parseInt(in.readLine());
    System.out.print("Enter the value of elements of array: ");    
      for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        arr[i][j]=Integer.parseInt(in.readLine());
        }
      }
System.out.println("Two dimensionl array before transpose:");
       for(int i=0;i< m;i++)
      {
       for(int j=0;j<n;j++)
       {      
        System.out.print(arr[i][j]+" ");
        }
        System.out.println();
      }
System.out.println("Two dimensionl array after transpose:");
       for(int i=0;i<n;i++)
       {
         for(int j=0;j<m;j++)
         {      
           System.out.print(arr[j][i]+" ");
          }
        System.out.println();
      }
    }
    catch(Exception e){ }
   }
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More