0
0
CProgramBeginner · 2 min read

C Program to Transpose Matrix with Output and Explanation

A C program to transpose a matrix uses nested 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

InputMatrix: 1 2 3 4
OutputTransposed Matrix: 1 3 2 4
InputMatrix: 5 6 7 8 9 10
OutputTransposed Matrix: 5 8 6 9 7 10
InputMatrix: 1
OutputTransposed Matrix: 1
🧠

How to Think About It

To transpose a matrix, think of flipping it over its diagonal. This means the element at row 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

1
Get the number of rows and columns from the user.
2
Read the matrix elements from the user.
3
Create a new matrix to hold the transpose.
4
For each element in the original matrix at position (i, j), assign it to the transpose matrix at position (j, i).
5
Print the transpose matrix.
💻

Code

c
#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;
}
Output
Enter rows and columns: 2 3 Enter matrix elements: 1 2 3 4 5 6 Transposed Matrix: 1 4 2 5 3 6
🔍

Dry Run

Let's trace the example matrix [[1, 2, 3], [4, 5, 6]] through the code.

1

Input matrix

rows = 2, cols = 3 matrix = [[1, 2, 3], [4, 5, 6]]

2

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

3

Print transpose

transpose = [[1, 4], [2, 5], [3, 6]] Printed row-wise as: 1 4 2 5 3 6

ijmatrix[i][j]transpose[j][i]
001transpose[0][0] = 1
012transpose[1][0] = 2
023transpose[2][0] = 3
104transpose[0][1] = 4
115transpose[1][1] = 5
126transpose[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

In-place transpose for square matrix
c
#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;
}
This method modifies the original matrix without extra space but works only for square matrices.
Using dynamic memory allocation
c
#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;
}
This approach uses dynamic memory to handle matrices of any size but requires careful memory management.

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.

ApproachTimeSpaceBest For
New matrix transposeO(rows * cols)O(rows * cols)Any matrix size
In-place transposeO(n^2)O(1)Square matrices only
Dynamic memory allocationO(rows * cols)O(rows * cols)Large or variable size matrices
💡
Always check matrix dimensions before transposing to avoid out-of-bound errors.
⚠️
Beginners often forget to swap row and column indices when assigning values to the transpose matrix.