The transpose of an `M x N` matrix `A` is a new `N x M` matrix `T` obtained by turning every row into a column using the assignment `T[j][i] = A[i][j]`. Because dimensions swap when `M != N`, always declare `int[][] T = new int[c][r];` rather than `[r][c]`. If the matrix is strictly square (`M == N`) and space is constrained, you can swap `A[i][j]` with `A[j][i]` in place across the upper triangle where `j > i`.
- 1. Swapping Dimensions for Rectangular vs. Square Matrices
- 2. Step-by-Step Dry Run on a 2x3 Matrix
- 3. In-Place Transpose for Square Matrices (Bonus Viva Logic)
- 4. MatrixTranspose.java (ICSE BlueJ)
- 5. CISCE Variable Description Table (VDT) & Dry Run
- 6. Common Mistakes & Top Scorer Tips
- 7. Interactive Concept Check Quiz
- 8. Frequently Asked Questions
1. Swapping Dimensions for Rectangular vs. Square Matrices
Many students treat transpose as merely printing `A[j][i]` without storing it, or allocating a second array with identical `new int[r][c]` dimensions. Both habits lose marks in ICSE Section B when a board question specifies a `3 x 2` or `2 x 4` array and requires constructing the transposed matrix in memory. A `2 x 3` original matrix becomes a `3 x 2` transposed matrix: two rows turn into two columns, and three columns become three rows. Declaring `trans = new int[c][r]` maps `trans[j][i] = mat[i][j]` cleanly without overflow.
- Original `mat` has `r` rows and `c` columns (`mat[r][c]`).
- Transposed `trans` must have `c` rows and `r` columns (`trans[c][r]`).
- Display `trans` using outer loop `i < c` and inner loop `j < r`.
- Diagonal elements `mat[i][i]` never change position during transposition.
2. Step-by-Step Dry Run on a 2x3 Matrix
Let `mat` be a `2 x 3` matrix with row 0 `{10, 20, 30}` and row 1 `{40, 50, 60}`. When `i = 0`, `j` iterates `0, 1, 2`, copying `mat[0][0] = 10` to `trans[0][0]`, `mat[0][1] = 20` to `trans[1][0]`, and `mat[0][2] = 30` to `trans[2][0]`. Next, when `i = 1`, `mat[1][0] = 40` moves to `trans[0][1]`, `mat[1][1] = 50` moves to `trans[1][1]`, and `mat[1][2] = 60` moves to `trans[2][1]`. Printing `trans` (`3 x 2`) outputs rows `{10, 40}`, `{20, 50}`, and `{30, 60}`.
- Row 0 `{10, 20, 30}` becomes Column 0 of `trans`.
- Row 1 `{40, 50, 60}` becomes Column 1 of `trans`.
- Notice `trans.length` equals `3` while `trans[0].length` equals `2`.
3. In-Place Transpose for Square Matrices (Bonus Viva Logic)
During ICSE practical viva or Section A output prediction, examiners often test whether you can transpose an `N x N` square array in place without creating `trans[][]`. If you run `j` from `0` to `N - 1` and swap `mat[i][j]` with `mat[j][i]`, every pair gets swapped twice and the matrix reverts to its original form! The fix is starting the inner loop at `j = i + 1` so only elements strictly above the main diagonal swap with their mirror partners below it.
- Upper-triangle loop: `for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++)`.
- Three-line swap uses `int temp = mat[i][j]; mat[i][j] = mat[j][i]; mat[j][i] = temp;`.
- In-place transposition fails for rectangular matrices (`r != c`) because row/column sizes differ.
4. MatrixTranspose.java (ICSE BlueJ)
import java.util.*;
public class MatrixTranspose {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows and columns: ");
int r = sc.nextInt();
int c = sc.nextInt();
int[][] mat = new int[r][c];
int[][] trans = new int[c][r];
System.out.println("Enter " + (r * c) + " elements:");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
mat[i][j] = sc.nextInt();
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
trans[j][i] = mat[i][j];
}
}
System.out.println("\nOriginal Matrix (" + r + "x" + c + "):");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
System.out.print(mat[i][j] + "\t");
}
System.out.println();
}
System.out.println("\nTransposed Matrix (" + c + "x" + r + "):");
for (int i = 0; i < c; i++) {
for (int j = 0; j < r; j++) {
System.out.print(trans[i][j] + "\t");
}
System.out.println();
}
sc.close();
}
}
Verified Console / Dry-Run Output:
Enter rows and columns: 2 3
Enter 6 elements:
10 20 30 40 50 60
Original Matrix (2x3):
10 20 30
40 50 60
Transposed Matrix (3x2):
10 40
20 50
30 60
5. CISCE Variable Description Table (VDT) & Dry Run
| Variable | Data Type | Exam Purpose & Sample Value |
|---|---|---|
| sc | Scanner | Reads matrix dimensions and element inputs |
| r, c | int | Row and column counts of original matrix (r = 2, c = 3) |
| mat[][] | int[][] | Original 2x3 array storing input values |
| trans[][] | int[][] | Transposed 3x2 array storing mat[i][j] at trans[j][i] |
| i, j | int | Nested loop indices used to copy and display both matrices |
Fix Matrix Indexing Bugs with Live ICSE Java Practice
Book a 1-on-1 BlueJ session to master rectangular transposes, upper-triangle swaps, and full-mark VDT writing.
6. Common Mistakes & Top Scorer Tips
- Avoid Mistake: Allocating `new int[r][c]` for `trans` instead of `new int[c][r]`, which throws `ArrayIndexOutOfBoundsException` on any non-square matrix.
- Avoid Mistake: Swapping `mat[i][j]` and `mat[j][i]` across the entire `0..n-1` grid during an in-place transpose, accidentally restoring the original matrix.
- Avoid Mistake: Printing `trans` using `i < r` and `j < c` instead of `i < c` and `j < r`, truncating or crashing the output loop.
- Examiner / Mentor Tip: If a Section B question specifies a fixed size like `4 x 4`, declare `final int N = 4;` or hardcode `4` cleanly as directed, and still construct the transposed array unless asked only to print.
- Examiner / Mentor Tip: Write a clear comment explaining why `trans` uses `[c][r]` dimensions—examiners appreciate explicit clarity on rectangular arrays.
- Examiner / Mentor Tip: Know both approaches: auxiliary `trans[c][r]` works for all matrices, while `j = i + 1` in-place swap works only for square matrices.
7. Interactive Concept Check Quiz
Question: A student runs an in-place transpose on a `3 x 3` array `a` using `for(i=0; i<3; i++) for(j=0; j<3; j++) { swap a[i][j], a[j][i]; }`. What will `a` contain after the loops finish?
8. Frequently Asked Questions
Do I need a second array to find the transpose of a matrix in Java?
What is the difference between printing mat[j][i] and storing trans[j][i] = mat[i][j]?
What happens if you transpose a matrix twice in a row?
Explore Related Lessons & Free Interactive Tools
- All 110 Guides in ICSE Java BlueJ Library
- Symmetric & Skew-Symmetric Matrix Check in Java
- Display Boundary Elements & Sort Inner Matrix in Java
- ISBN-10 Code Validation Program in Java for ICSE
- Interactive Tool: ICSE Class 10 Java BlueJ Output Quiz
- Interactive Tool: ICSE Variable Description Table (VDT) Auto-Formatter
