Wednesday, 9 October 2013

Program to display transpose of a matrix

//Program to display transpose of a matrix
//Program by Ahlam Ansari
import java.util.*;
class Transpose
{
    public static void main(String [] ahlam)
    {
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter the no of Rows");
        int row=scan.nextInt();
        System.out.println("Enter the no of Columns");
        int col=scan.nextInt();
        int arr[][]=new int [row][col];
        System.out.println("Enter the values");
        for(int i=0;i<arr.length;i++)
        {
            System.out.println("enter the values of row "+ row);
            for(int j=0;j<arr[i].length;j++)
            {
                arr[i][j]=scan.nextInt();
            }
        }
        //transpose of arr
        int [][] arrtrans=new int [col][row];
        for(int i=0;i<arr.length;i++)
        {
            System.out.println("enter the values of row "+ row);
            for(int j=0;j<arr[i].length;j++)
            {
                arrtrans[j][i]=arr[i][j];
            }
        }
        /*display*/
        System.out.println("the orignal matrix is :");
        for(int i=0;i<arr.length;i++)
        {
            for(int j=0;j<arr[i].length;j++)
            {
                System.out.print(" "+arr[i][j]);
            }
        System.out.println();
        }


        System.out.println("the transpose is :");
        for(int i=0;i<arrtrans.length;i++)
        {
            for(int j=0;j<arrtrans[i].length;j++)
            {
                System.out.print(" "+arrtrans[i][j]);
            }
        System.out.println();
        }

    }
}   
   
/*
C:\Documents and Settings\AHLAM\My Documents\Google Drive\My Lectures\Fall\OOPM\
Programs>javac Transpose.java

C:\Documents and Settings\AHLAM\My Documents\Google Drive\My Lectures\Fall\OOPM\
Programs>java Transpose
Enter the no of Rows
3
Enter the no of Columns
3
Enter the values
enter the values of row 3
1
0
1
enter the values of row 3
2
0
2
enter the values of row 3
3
0
3
enter the values of row 3
enter the values of row 3
enter the values of row 3
the orignal matrix is :
 1 0 1
 2 0 2
 3 0 3
the transpose is :
 1 2 3
 0 0 0
 1 2 3

C:\Documents and Settings\AHLAM\My Documents\Google Drive\My Lectures\Fall\OOPM\
Programs>
*/

No comments:

Post a Comment