Transpose of a Matrix: flowchart, algorithm and program
This program reads a 2×3 matrix into array a and prints its transpose, which is a 3×2 matrix. The idea is simple: the element in row i, column j of the original goes to row j, column i of the result.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Print 'Enter elements of 2x3 matrix'.
- Step 3: Set i = 0.
- Step 4: If i < 2, go to step 5; otherwise go to step 10.
- Step 5: Set j = 0.
- Step 6: If j < 3, go to step 7; otherwise go to step 9.
- Step 7: Read a[i][j].
- Step 8: Increase j by 1. Go to step 6.
- Step 9: Increase i by 1. Go to step 4.
- Step 10: Set i = 0.
- Step 11: If i < 2, go to step 12; otherwise go to step 17.
- Step 12: Set j = 0.
- Step 13: If j < 3, go to step 14; otherwise go to step 16.
- Step 14: Set t[j][i] = a[i][j].
- Step 15: Increase j by 1. Go to step 13.
- Step 16: Increase i by 1. Go to step 11.
- Step 17: Print 'Transpose:'.
- Step 18: Set i = 0.
- Step 19: If i < 3, go to step 20; otherwise go to step 26.
- Step 20: Set j = 0.
- Step 21: If j < 2, go to step 22; otherwise go to step 24.
- Step 22: Print t[i][j].
- Step 23: Increase j by 1. Go to step 21.
- Step 24: Print newline.
- Step 25: Increase i by 1. Go to step 19.
- Step 26: Stop.
Pseudocode
BEGIN
PRINT 'Enter elements of 2x3 matrix'
i ← 0
WHILE i < 2 DO
j ← 0
WHILE j < 3 DO
READ a[i][j]
j++
END WHILE
i++
END WHILE
i ← 0
WHILE i < 2 DO
j ← 0
WHILE j < 3 DO
t[j][i] ← a[i][j]
j++
END WHILE
i++
END WHILE
PRINT 'Transpose:'
i ← 0
WHILE i < 3 DO
j ← 0
WHILE j < 2 DO
PRINT t[i][j]
j++
END WHILE
PRINT newline
i++
END WHILE
END
Program in C, Python and Java
Transpose of a Matrix program in C
#include <stdio.h>
int main() {
int a[2][3], t[3][2];
int i, j;
printf("Enter elements of 2x3 matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
t[j][i] = a[i][j];
}
}
printf("Transpose:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
printf("%d ", t[i][j]);
}
printf("\n");
}
return 0;
}Transpose of a Matrix program in Python
a = [[0] * 3 for _ in range(2)]
t = [[0] * 2 for _ in range(3)]
print("Enter elements of 2x3 matrix:")
for i in range(2):
row = input().split()
for j in range(3):
a[i][j] = int(row[j])
for i in range(2):
for j in range(3):
t[j][i] = a[i][j]
print("Transpose:")
for i in range(3):
for j in range(2):
print(t[i][j], end=" ")
print()Transpose of a Matrix program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] a = new int[2][3];
int[][] t = new int[3][2];
int i, j;
System.out.println("Enter elements of 2x3 matrix:");
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
a[i][j] = sc.nextInt();
}
}
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
t[j][i] = a[i][j];
}
}
System.out.println("Transpose:");
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++) {
System.out.print(t[i][j] + " ");
}
System.out.println();
}
}
}
Sample input: 1 2 3
4 5 6
Output: 1 4
2 5
3 6
How it works
- The first nested loop (i from 0 to 1, j from 0 to 2) reads six numbers with scanf and stores them in a[i][j], row by row.
- The second nested loop goes through every element of a and does t[j][i] = a[i][j]. Swapping the two indices is what turns rows into columns.
- The array t is declared as t[3][2] because the transpose of a 2×3 matrix has 3 rows and 2 columns.
- The third nested loop prints t with i from 0 to 2 and j from 0 to 1. After each row of two numbers, printf("\n") moves to the next line.
- For the sample input 1 2 3 / 4 5 6, the first row of a (1 2 3) becomes the first column of t, giving 1 4 / 2 5 / 3 6.
Dry run (trace table)
| Step | Line | Box | i | j | a[0][0] | a[0][1] | a[0][2] | a[1][0] | a[1][1] | a[1][2] | t[0][0] | t[1][0] | t[2][0] | Output |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 3 | Start | ||||||||||||
| 2 | 7 | Print 'Enter elements of 2x3 matrix' | Enter elements of 2x3 matrix | |||||||||||
| 3 | 8 | i = 0 | 0 | |||||||||||
| 4 | 8 | i < 2? | 0 | |||||||||||
| 5 | 9 | j = 0 | 0 | 0 | ||||||||||
| 6 | 9 | j < 3? | 0 | 0 | ||||||||||
| 7 | 10 | Read a[i][j] | 0 | 0 | 1 | |||||||||
| 8 | 9 | j++ | 0 | 1 | 1 | |||||||||
| 9 | 9 | j < 3? | 0 | 1 | ||||||||||
| 10 | 10 | Read a[i][j] | 0 | 1 | 2 | |||||||||
| 11 | 9 | j++ | 0 | 2 | ||||||||||
| 12 | 9 | j < 3? | 0 | 2 | ||||||||||
| 13 | 10 | Read a[i][j] | 0 | 2 | 3 | |||||||||
| 14 | 9 | j++ | 0 | 3 | ||||||||||
| 15 | 9 | j < 3? | 0 | 3 | ||||||||||
| 16 | 8 | i++ | 1 | 3 | ||||||||||
| 17 | 8 | i < 2? | 1 | 3 | ||||||||||
| 18 | 9 | j = 0 | 1 | 0 | ||||||||||
| 19 | 9 | j < 3? | 1 | 0 | ||||||||||
| 20 | 10 | Read a[i][j] | 1 | 0 | 4 |
Time and space complexity
Time: O(m*n) · Space: O(m*n). Each of the m*n matrix elements is read, copied and printed once, and the transpose needs its own m*n array.
Common mistakes
- Declaring t as t[2][3] (same shape as a). The transpose needs t[3][2], otherwise the indices go out of bounds.
- Writing t[i][j] = a[i][j] in the copy loop. That only copies the matrix and doesn't transpose it. The indices must be swapped as t[j][i].
- Using the wrong loop limits when printing. The print loops must run i < 3 and j < 2, matching the shape of t, not a.
Viva questions
Q1. What is the transpose of a matrix, and what are the dimensions of t in your program?
The transpose turns rows into columns and columns into rows. Since a is 2x3, its transpose t is 3x2, which is why t is declared as t[3][2].
Q2. Explain line 16: t[j][i] = a[i][j]. Why are the indices swapped?
The element in row i, column j of a becomes the element in row j, column i of t. Swapping the indices is the definition of transpose.
Q3. Why does the input loop run i up to 2 and j up to 3, while the printing loop runs i up to 3 and j up to 2?
Input and the copy work on a, which has 2 rows and 3 columns. Printing works on t, which has 3 rows and 2 columns, so the loop limits are reversed.
Q4. Why do you use nested loops to read and print a matrix?
A 2D array has two indices. The outer loop picks the row and the inner loop picks each column in that row, so every element is visited once.
Q5. Why is there a printf("\n") after the inner loop in the print section?
It moves the output to a new line after each row of t is printed. Without it, all the elements would appear on one line and the matrix shape would be lost.
Q6. Why does scanf use &a[i][j] but printf uses t[i][j] without &?
scanf needs the address of the variable so it can store the value there. printf only needs the value to display.
Q7. What is the time complexity of this program?
It is O(m*n) for an m x n matrix, since each element is read, copied and printed once. Here that is 2*3 = 6 elements, so it is constant for this fixed size. Extra space is also O(m*n) for t.
Q8. What would change if the matrix were n x n and you wanted to transpose it in place without t?
You could swap a[i][j] with a[j][i] only for j > i, using a temporary variable. Swapping over the whole matrix would swap each pair twice and restore the original. In-place works only for square matrices because a non-square one changes shape.
Frequently asked questions
What is the transpose of a matrix?
It is a new matrix in which rows and columns are exchanged, so the element at position [i][j] moves to [j][i]. A 2×3 matrix becomes a 3×2 matrix.
Why is the result array t[3][2] and not t[2][3]?
Transposing swaps the number of rows and columns. The original has 2 rows and 3 columns, so the transpose has 3 rows and 2 columns.
Can I transpose without a second array?
For a square matrix you can swap a[i][j] with a[j][i] in place. For a non-square matrix like 2×3 the shape changes, so you need a separate array.