-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path074-Matrix-Transpose.java
More file actions
134 lines (93 loc) · 3.61 KB
/
Copy path074-Matrix-Transpose.java
File metadata and controls
134 lines (93 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* ============================================================================
* Program Name : Matrix Transpose
* File Name : 074-Matrix-Transpose.java
* Class Name : MatrixTranspose
*
* Description:
* This program accepts a matrix from the user and displays its transpose.
* The transpose of a matrix is obtained by interchanging its rows and columns.
*
* Objective:
* - Understand user input using the Scanner class.
* - Learn how to declare and use two-dimensional arrays.
* - Learn how to find and display the transpose of a matrix.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import java.util.Scanner;
public class MatrixTranspose {
// The main() method is the entry point of every Java application.
public static void main(String[] args) {
// Create a Scanner object to read input from the keyboard.
Scanner scanner = new Scanner(System.in);
// Ask the user to enter the number of rows.
System.out.print("Enter the Number of Rows: ");
// Read the number of rows.
int rows = scanner.nextInt();
// Ask the user to enter the number of columns.
System.out.print("Enter the Number of Columns: ");
// Read the number of columns.
int columns = scanner.nextInt();
// Check whether the entered dimensions are valid.
if (rows <= 0 || columns <= 0) {
// Display an error message.
System.out.println("Please enter positive values for rows and columns.");
} else {
// Declare the original matrix.
int[][] matrix = new int[rows][columns];
// Declare the transpose matrix.
int[][] transpose = new int[columns][rows];
// Ask the user to enter the matrix elements.
System.out.println("Enter the Matrix Elements:");
// Read the matrix elements.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
// Store the current element.
matrix[i][j] = scanner.nextInt();
}
}
// Find the transpose of the matrix.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
// Swap rows with columns.
transpose[j][i] = matrix[i][j];
}
}
// Display the original matrix.
System.out.println("Original Matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
// Display the transpose matrix.
System.out.println("Transpose Matrix:");
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
System.out.print(transpose[i][j] + " ");
}
System.out.println();
}
// Example Output:
// Enter the Number of Rows: 2
// Enter the Number of Columns: 3
// Enter the Matrix Elements:
// 1 2 3
// 4 5 6
// Original Matrix:
// 1 2 3
// 4 5 6
// Transpose Matrix:
// 1 4
// 2 5
// 3 6
}
// Close the Scanner object to release system resources.
scanner.close();
}
}