Write a Java program that invokes a method to transposes a square matrix (conver
ID: 3693179 • Letter: W
Question
Write a Java program that invokes a method to transposes a square matrix (convert the rows into columns and vice-versa). For example the transposed matrix for
2 5 7
3 9 6
4 2 1
is
2 3 4
5 9 2
7 6 1
Your program should also have methods to generate (random numbers, range 1-50) and print the matrix. Use dynamic arrays and ask the user for size. Write your program so that it will generate output similar to the sample output below:
SAMPLE OUTPUT:
Do you want to start(Y/N): y
Enter array size: -5
ERROR! Should be positive. REENTER: 5
The original matrix is:
6 9 3 12 41
27 44 12 21 18
1 48 39 49 27
23 46 48 31 31
4 15 8 12 42
The transposed matrix is:
6 27 1 23 4
9 44 48 46 15
3 12 39 48 8
12 21 49 31 12
41 18 27 31 42
Do you want to continue(Y/N): y
Enter array size: 4
The original matrix is:
27 8 33 20
34 22 45 31
41 14 49 12
42 30 41 4
The transposed matrix is:
27 34 41 42
8 22 14 30
33 45 49 41
20 31 12 4
Do you want to continue(Y/N): y
Enter array size: 3
The original matrix is:
39 32 19
18 30 4
20 42 48
The transposed matrix is:
39 18 20
32 30 42
19 4 48
Do you want to continue(Y/N): n
Explanation / Answer
TransposeMatrix.java
import java.util.Random;
import java.util.Scanner;
public class TransposeMatrix {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("Do you want to start(Y/N):");
int matrix[][];
int n = 0;
char c = scan.next().charAt(0);
if(c == 'y' || c == 'Y'){
boolean flag = true;
while(true){
if(flag)
System.out.println("Enter array size: ");
n = scan.nextInt();
flag = true;
if(n > 0){
matrix = new int[n][n];
transposeMatrix(matrix);
System.out.println("Do you want to continue(Y/N):");
c = scan.next().charAt(0);
if(c == 'n' || c == 'N') break;
}
else{
System.out.println("ERROR! Should be positive. REENTER: ");
flag = false;
}
}
}
}
public static void transposeMatrix(int matrix[][]) {
fillMatrixWithRandonNumbers(matrix);
System.out.println("The original matrix is: ");
displayOriginalMatrix(matrix);
System.out.println("The transposed matrix is: ");
displayTransposeMatrix(matrix);
}
public static void fillMatrixWithRandonNumbers(int matrix[][]) {
Random r = new Random();
for(int i=0; i<matrix.length; i++){
for(int j=0; j<matrix[i].length; j++){
matrix[i][j] = r.nextInt(51);
}
}
}
public static void displayOriginalMatrix(int matrix[][]) {
for(int i=0; i<matrix.length; i++){
for(int j=0; j<matrix[i].length; j++){
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}
}
public static void displayTransposeMatrix(int matrix[][]) {
for(int i=0; i<matrix.length; i++){
for(int j=0; j<matrix[i].length; j++){
System.out.print(matrix[j][i]+" ");
}
System.out.println();
}
}
}
Output:
Do you want to start(Y/N):
y
Enter array size:
-5
ERROR! Should be positive. REENTER:
5
The original matrix is:
49 39 10 12 16
1 40 22 48 22
44 5 38 28 6
19 19 6 35 38
45 3 25 42 32
The transposed matrix is:
49 1 44 19 45
39 40 5 19 3
10 22 38 6 25
12 48 28 35 42
16 22 6 38 32
Do you want to continue(Y/N):
y
Enter array size:
4
The original matrix is:
50 28 6 33
1 49 32 14
44 22 29 39
6 30 6 3
The transposed matrix is:
50 1 44 6
28 49 22 30
6 32 29 6
33 14 39 3
Do you want to continue(Y/N):
y
Enter array size:
3
The original matrix is:
49 6 46
36 3 4
11 13 45
The transposed matrix is:
49 36 11
6 3 13
46 4 45
Do you want to continue(Y/N):
n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.