Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
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?

Java
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
Updated 26-Nov-12 21:34pm
v4
Comments
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 Rudolf 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.

;)

1 solution

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.

Your code:

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


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

Java
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900