Broad Network


8.3 Produce the transpose of a matrix in C.

8. Basic Matrix Programming in C

Full Course on Data Structures and Algorithms in C

By: Chrysanthus Date Published: 3 Feb 2026

The reader is advised to read all the lessons (tutorials) in this full course, in the order presented.

The transpose of a matrix is a new matrix where the rows of the original matrix become the columns of the new matrix. So the first row becomes the first column, the second row becomes the second column, the third row becomes the third column, and so on.

The transpose can be seen the other way round, that is, the first column becomes the first row, the second column becomes the second row, the third column becomes the third row, and so on.

If the name of the original matrix is A, then the transpose of the matrix is written as At or AT .

Transpose of a 3-by-2 Matrix

The following diagram shows the transpose of a 3 x 2 matrix:

Transpose of a Square Matrix

The following diagram shows the transpose of a 4 x 4 square matrix:

With a square matrix, the elements of the major diagonal, stay the same, in the transposed matrix.

Transpose of a 2-by-3 Matrix

The following diagram shows the transpose of a 2 x 3 matrix:

Program to transpose a Matrix

The program just sends each row element to its new column position; in that way, swapping the rows and columns. The program is (read through the code and comments):

#include <stdio.h>

void transpose(int mat[][2], int rows, int cols) {
    //exchange rows and columns lengths for transposed matrix
    int matT[cols][rows];
    
    // Loop through each row of mat
    for (int i=0; i < rows; i++) {    
        //Loop through each column of mat
        for (int j=0; j < cols; j++) {    
            // Transposing values
            matT[j][i] = mat[i][j];  
        }
    }
    
    //printing
    for (int i=0; i < cols; i++) {    
        for (int j=0; j < rows; j++) {    
            printf("%i, ", matT[i][j]);
        }
        printf("\n");
    }
}

int main() {

    int mat[3][2];
    mat[0][0] = 1; mat[0][1] = 2;
    mat[1][0] = 3; mat[1][1] = 4;
    mat[2][0] = 5; mat[2][1] = 6;
    
    int rows=3, cols=2;

    transpose(mat, rows, cols);

    return 0;
}

The output is:

1, 3, 5, 
2, 4, 6, 

The time complexity is actually O(3 x n x m), where 'n x m' refers to the time to iterate over all the elements in the matrix (2D array), in the called function. The same amount of time is used to print the result in the called function. This same amount of time is needed to populate the matrix with numbers, one-by-one, in the calling function area of the program. And so that makes '3 x n x m'. However, the coefficient (multiplicand of 3) is normally omitted when quoting the time complexity. The time complexity here is quoted as O(n x m).

The space complexity is actually O(2 x n x m) for the matrix. The 2D array in the calling function area in the code and the 2D array in the called function, are the same array (same reference). There is the new result 2D array in the called function; and that gives rise to the 2 in O(2 x n x m). However, the coefficient (multiplicand of 2) is normally omitted when quoting the space complexity. The space complexity here is quoted as O(n x m).

Thanks for reading.





Related Links

More Related Links

Cousins

BACK NEXT

Comments


Note: You can use the Search Box above to find articles and discussions of interest.