C Program to Transpose Matrix with Output and Explanation
for loops to swap rows and columns, storing the result in a new matrix; for example, transpose[j][i] = matrix[i][j]; inside loops over rows and columns.Examples
How to Think About It
i and column j in the original matrix becomes the element at row j and column i in the new matrix. You read each element and place it in the new position accordingly.Algorithm
Code
#include <stdio.h> int main() { int rows, cols; printf("Enter rows and columns: "); scanf("%d %d", &rows, &cols); int matrix[rows][cols], transpose[cols][rows]; printf("Enter matrix elements:\n"); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { scanf("%d", &matrix[i][j]); } } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { transpose[j][i] = matrix[i][j]; } } printf("Transposed Matrix:\n"); for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { printf("%d ", transpose[i][j]); } printf("\n"); } return 0; }
Dry Run
Let's trace the example matrix [[1, 2, 3], [4, 5, 6]] through the code.
Input matrix
rows = 2, cols = 3 matrix = [[1, 2, 3], [4, 5, 6]]
Transpose assignment
transpose[0][0] = matrix[0][0] = 1 transpose[1][0] = matrix[0][1] = 2 transpose[2][0] = matrix[0][2] = 3 transpose[0][1] = matrix[1][0] = 4 transpose[1][1] = matrix[1][1] = 5 transpose[2][1] = matrix[1][2] = 6
Print transpose
transpose = [[1, 4], [2, 5], [3, 6]] Printed row-wise as: 1 4 2 5 3 6
| i | j | matrix[i][j] | transpose[j][i] |
|---|---|---|---|
| 0 | 0 | 1 | transpose[0][0] = 1 |
| 0 | 1 | 2 | transpose[1][0] = 2 |
| 0 | 2 | 3 | transpose[2][0] = 3 |
| 1 | 0 | 4 | transpose[0][1] = 4 |
| 1 | 1 | 5 | transpose[1][1] = 5 |
| 1 | 2 | 6 | transpose[2][1] = 6 |
Why This Works
Step 1: Reading matrix
We read the matrix elements row by row using nested loops to store them in matrix.
Step 2: Swapping indices
To transpose, we assign transpose[j][i] = matrix[i][j], flipping rows and columns.
Step 3: Printing result
We print the transpose matrix row-wise, which shows the original matrix flipped over its diagonal.
Alternative Approaches
#include <stdio.h> int main() { int n; printf("Enter size of square matrix: "); scanf("%d", &n); int matrix[n][n]; printf("Enter matrix elements:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &matrix[i][j]); } } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } printf("Transposed Matrix:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } return 0; }
#include <stdio.h> #include <stdlib.h> int main() { int rows, cols; printf("Enter rows and columns: "); scanf("%d %d", &rows, &cols); int **matrix = malloc(rows * sizeof(int*)); int **transpose = malloc(cols * sizeof(int*)); for (int i = 0; i < rows; i++) matrix[i] = malloc(cols * sizeof(int)); for (int i = 0; i < cols; i++) transpose[i] = malloc(rows * sizeof(int)); printf("Enter matrix elements:\n"); for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) scanf("%d", &matrix[i][j]); for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) transpose[j][i] = matrix[i][j]; printf("Transposed Matrix:\n"); for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) printf("%d ", transpose[i][j]); printf("\n"); } for (int i = 0; i < rows; i++) free(matrix[i]); for (int i = 0; i < cols; i++) free(transpose[i]); free(matrix); free(transpose); return 0; }
Complexity: O(rows * cols) time, O(rows * cols) space
Time Complexity
The program uses nested loops over all elements, so time grows with the number of elements, which is rows times columns.
Space Complexity
A new matrix of size cols by rows is created to store the transpose, so space also grows with the number of elements.
Which Approach is Fastest?
In-place transpose is fastest and uses less space but only works for square matrices; otherwise, creating a new matrix is necessary.
| Approach | Time | Space | Best For |
|---|---|---|---|
| New matrix transpose | O(rows * cols) | O(rows * cols) | Any matrix size |
| In-place transpose | O(n^2) | O(1) | Square matrices only |
| Dynamic memory allocation | O(rows * cols) | O(rows * cols) | Large or variable size matrices |