12,632,072 members (26,484 online)
Rate this:
See more:
can anyone have the code for this kind of output? where the user enter a desired number of rows equal to cols and displays the matrix and the transposed matrix.

enter rows: 3
enter cols: 3

Normal Matrix:
1 2 3
4 5 6
7 8 9

Transposed Matrix:
1 4 7
2 5 8
3 6 9

if you do, can you give the code?

or try to rewrite the code I have?

```import java.util.*;

public class transpose1 {
public static void main(String[] args) throws Exception {
int rows, cols;
int[][] matrix, tmatrix;
Scanner input = new Scanner(System.in);
System.out.print("Enter number of rows: ");
rows = input.nextInt();
System.out.print("Enter number of columns: ");
cols = input.nextInt();
matrix = new int[rows][cols];
tmatrix = new int[cols][rows];
int x = rows * cols;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
tmatrix[i][j] = matrix[j][i];
}
}
System.out.println("Matrix is:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + " ");                        }
System.out.println();
}
System.out.println("Its transpose is: ");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(tmatrix[i][j] + " ");
}
System.out.println();
}
}
}```
Posted 26-Nov-12 20:48pm
Updated 26-Nov-12 22:34pm
TorstenH.34.9K
v4
TorstenH. 27-Nov-12 2:04am

We do not provide codes. Please try to write it yourself.
You can ask anything in the procerss of coding. We will be delighted to help in a specific thingy.
Manfred R. Bihy 27-Nov-12 2:20am

Actually OP only asked if anyone could have this code and going by my standards this can be correctly answered with yes. Anyone who can do only the tiniest bit of research can indeed have this code.

;)

Rate this:

## Solution 1

We do not provide code. You'll have to use your code.
Anyway: The teacher will figure quite easily if it's your code or antoher ones. I can easily identify who wrote a code when I see it here in our company.

- Classes are named with capital Letter:
```public class Transpose1 {
// code
}```

- your values in `matrix `and `tmatrix` are initalized with 0.
To get them filled:

```int x = 0; // start at 0
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = x++; // increment after value set
}
}```

Then it looks fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 265 Richard MacCutchan 118 Peter Leow 85 Admire Mhlaba 75 F. Xaver 65
 OriginalGriff 1,999 ppolymorphe 1,242 Peter Leow 804 John Simmons / outlaw programmer 665 CPallini 571

Advertise | Privacy | Mobile
Web02 | 2.8.161208.2 | Last Updated 27 Nov 2012