Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Homework array
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 19:48pm
Edited 26-Nov-12 21:34pm
TorstenH.32.7K
v4
Comments
TorstenH. at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

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.
 
Your code:
 
- 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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 474
1 OriginalGriff 388
2 Maciej Los 385
3 CPallini 194
4 CHill60 180
0 OriginalGriff 6,152
1 Sergey Alexandrovich Kryukov 5,312
2 Maciej Los 3,279
3 Peter Leow 3,184
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100