C Program to Find the Transpose of a Matrix — Rectangular and In-Place Square

The transpose of a matrix is formed by flipping it over its main diagonal — rows become columns and columns become rows. If the original matrix is of order M×N, its transpose is of order N×M, where element A[i][j] becomes B[j][i].

Matrix A (3×2):        Transpose B (2×3):
  1  2                   1  3  5
  3  4        →          2  4  6
  5  6

Transpose is fundamental in linear algebra: it appears in matrix multiplication, solving linear systems, neural networks (weight matrices), and signal processing.

Algorithm

For an M×N matrix A, the transpose B is an N×M matrix where:

B[j][i] = A[i][j]   for all i in 0..M-1, j in 0..N-1

C Program — Transpose of a Rectangular Matrix (M×N)

#include <stdio.h>

#define MAX 10

void read_matrix(int mat[][MAX], int rows, int cols) {
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            scanf("%d", &mat[i][j]);
}

void print_matrix(int mat[][MAX], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++)
            printf("%4d", mat[i][j]);
        printf("\n");
    }
}

void transpose(int A[][MAX], int B[][MAX], int rows, int cols) {
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            B[j][i] = A[i][j];   /* B is cols×rows */
}

int main(void) {
    int A[MAX][MAX], B[MAX][MAX];
    int M, N;

    printf("Enter number of rows and columns: ");
    scanf("%d %d", &M, &N);

    printf("Enter elements of %d×%d matrix:\n", M, N);
    read_matrix(A, M, N);

    printf("\nMatrix A (%d×%d):\n", M, N);
    print_matrix(A, M, N);

    transpose(A, B, M, N);

    printf("\nTranspose B (%d×%d):\n", N, M);
    print_matrix(B, N, M);   /* note: N rows, M cols */

    return 0;
}

Bonus — In-Place Transpose of a Square Matrix

For an N×N square matrix, the transpose can be done in-place with no extra array — just swap elements above and below the main diagonal:

#include <stdio.h>

#define MAX 10

void transpose_inplace(int mat[][MAX], int n) {
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++) {
            int temp    = mat[i][j];
            mat[i][j]   = mat[j][i];
            mat[j][i]   = temp;
        }
}

void print_matrix(int mat[][MAX], int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            printf("%4d", mat[i][j]);
        printf("\n");
    }
}

int main(void) {
    int mat[MAX][MAX];
    int n;

    printf("Enter matrix size N (for N×N): ");
    scanf("%d", &n);

    printf("Enter %d elements:\n", n * n);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            scanf("%d", &mat[i][j]);

    printf("\nOriginal matrix:\n");
    print_matrix(mat, n);

    transpose_inplace(mat, n);

    printf("\nTransposed matrix:\n");
    print_matrix(mat, n);

    return 0;
}

The inner loop starts at j = i + 1 — this skips the main diagonal (which stays in place) and avoids double-swapping elements below the diagonal.

Code Explanation

  • Rectangular transpose: the output matrix B has dimensions N×M (rows and columns swapped). The assignment B[j][i] = A[i][j] places each element in its transposed position. When printing B, pass N as rows and M as columns — not the original M and N.
  • In-place square transpose: only iterate the upper triangle (j starts at i+1). Each pair (i, j) and (j, i) is swapped exactly once. O(1) extra space.

Sample Output

Enter number of rows and columns: 3 2
Enter elements of 3×2 matrix:
1 2
3 4
5 6

Matrix A (3×2):
   1   2
   3   4
   5   6

Transpose B (2×3):
   1   3   5
   2   4   6

Complexity

Version Time Space
Rectangular (M×N) O(M × N) O(N × M) for output matrix
Square in-place (N×N) O(N²) O(1)

Related Matrix Programs in C


As an Amazon Associate we earn from qualifying purchases.

Further Reading

The definitive reference for C — The C Programming Language by Brian Kernighan and Dennis Ritchie. Covers every concept on this site: pointers, arrays, structs, file I/O, and the standard library. Worth having on your desk.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>