AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-array: How to transpose a matrix by java code?

Please give me java source code to transpose the input matrix.

corejava x 353
array x 12
Posted On : 2014-04-21 23:31:50.0
profile Saksham Kumar - anyforum.in Saksham Kumar
73433939091
up-rate
4
down-rate

Answers


As we know very well matrix in programming language is represented by 2 dimensional array. So we are providing the java source code which first ask about the no of rows and columns and then input all the elements of the matrix row wise. After that it transposes the i/p matrix.

TransposeMatrix.java:
--------------------------------


package in.anyforum;

import java.io.*;
class TranspMatrix
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int[][] a=new int[5][5];
int i,j,p,q;
System.out.println("Enter order of matrix A: ");
p=Integer.parseInt(br.readLine());
q=Integer.parseInt(br.readLine());
System.out.println("Enter the elements of matrix A:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("The matrix A is:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
System.out.print("\t"+a[i][j]);
}
System.out.println();
}
System.out.println("The transpose matrix A is:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
System.out.print("\t"+a[j][i]);
}
System.out.println();
}
}
}

************************************* Input & Output *************************************

Enter order of matrix A:
4
4
Enter the elements of matrix A:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
The matrix A is:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The transpose matrix A is:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16

Posted On : 2014-04-21 23:53:55
Satisfied : 2 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129558962
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in