Transpose of a Matrix in C Last Updated : 31 Jul, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to write a C program to find the transpose of a matrix. The transpose of a matrix is a new matrix formed by interchanging its rows with columns. In simple words, the transpose of A[][] is obtained by changing A[i][j] to A[j][i]. Note: The transpose of an m × n matrix will result in an n × m matrix. Example Algorithm to Find the Transpose of a Matrix The idea is to run a nested loop and copy the elements of the original matrix A to the resultant matrix B, but with the row and column, indices swapped. B[i][j] = A[j][i]C Program to Find the Transpose of a Square Matrix The below program finds the transpose of A[][] and stores the result in B[][], we can change N for different dimensions. C // C Program to find transpose // of a square matrix #include <stdio.h> #define N 4 // This function stores transpose // of A[][] in B[][] void transpose(int A[][N], int B[][N]) { int i, j; for (i = 0; i < N; i++) for (j = 0; j < N; j++) // Assigns the transpose of element A[j][i] to // B[i][j] B[i][j] = A[j][i]; } // Driver code int main() { int A[N][N] = { { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 3, 3, 3, 3 }, { 4, 4, 4, 4 } }; int B[N][N], i, j; transpose(A, B); printf("Result matrix is \n"); for (i = 0; i < N; i++) { for (j = 0; j < N; j++) printf("%d ", B[i][j]); printf("\n"); } return 0; } OutputResult matrix is 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Complexity AnalysisTime Complexity: O(n2)Auxiliary Space: O(n 2)C Program to find Transpose of a Rectangular Matrix The below program finds the transpose of A[][] and stores the result in B[][]. C // C program to find transpose // of a rectangular matrix #include <stdio.h> #define M 3 #define N 4 // This function stores transpose // of A[][] in B[][] void transpose(int A[][N], int B[][M]) { int i, j; for (i = 0; i < N; i++) for (j = 0; j < M; j++) B[i][j] = A[j][i]; } // Driver code int main() { int A[M][N] = { { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 3, 3, 3, 3 } }; // Note dimensions of B[][] int B[N][M], i, j; transpose(A, B); printf("Result matrix is \n"); for (i = 0; i < N; i++) { for (j = 0; j < M; j++) printf("%d ", B[i][j]); printf("\n"); } return 0; } OutputResult matrix is 1 2 3 1 2 3 1 2 3 1 2 3 Complexity AnalysisTime Complexity: O(n*m)Auxiliary Space: O(n*m) Please refer complete article on Program to find the transpose of a matrix for more details! Comment More infoAdvertise with us Next Article Transpose of a Matrix in C kartik Follow Improve Article Tags : C Language Similar Reads C Program to sort rows of the Matrix Given a matrix arr[][] of dimensions N * M, the task is to sort the matrix such that each row is sorted and the first element of each row is greater than or equal to the last element of the previous row. Examples: Input: N = 3, M = 3, arr[][] = {{7, 8, 9}, {5, 6, 4}, {3, 1, 2}}Output:1 2 34 5 67 8 9 3 min read Graph Representation using Adjacency Matrix in C A graph is a data structure having a set of vertices and a collection of edges that each connect a pair of vertices. There are several ways to represent a graph in computer memory, and one of them is using an adjacency matrix. An adjacency matrix is a 2D array with one row per vertex and one column 4 min read How to Get Value of Multidimensional Array in C? Prerequisite: Array in C An array is a type of data structure where we can store multiple elements of similar data types. A multidimensional array can be termed an array of arrays that stores homogeneous data in tabular form. Data in multidimensional arrays are stored in row-major order. Declaration 8 min read One Dimensional Arrays in C In C, an array is a collection of elements of the same type stored in contiguous memory locations. This organization allows efficient access to elements using their index. Arrays can also be of different types depending upon the direction/dimension they can store the elements. It can be 1D, 2D, 3D, 5 min read Output of C Program | Set 23 Predict the output of following C Program. C #include <stdio.h> #define R 4 #define C 4 void modifyMatrix(int mat[][C]) { mat++; mat[1][1] = 100; mat++; mat[1][1] = 200; } void printMatrix(int mat[][C]) { int i, j; for (i = 0; i < R; i++) { for (j = 0; j < C; j++) printf("%3d " 2 min read Print 2D matrix in different lines and without curly braces in C/C++? Following is a general way of printing 2D matrix such that every row is printed in separate lines. C for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cout << arr[i][j] << " "; } // Newline for new row cout << endl; } How to print without using any curly b 2 min read Multidimensional Arrays in C - 2D and 3D Arrays A multi-dimensional array in C can be defined as an array that has more than one dimension. Having more than one dimension means that it can grow in multiple directions. Some popular multidimensional arrays include 2D arrays which grows in two dimensions, and 3D arrays which grows in three dimension 8 min read Jagged Array or Array of Arrays in C with Examples Prerequisite: Arrays in CJagged array is array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. These type of arrays are also known as Jagged arrays. Example:arr[][] = { {0, 1, 2}, {6, 4}, {1, 7, 6, 8, 9}, 3 min read Add Matrix in C Matrices are the collection of numbers arranged in order of rows and columns. In this article, we will learn to write a C program for the addition of two matrices.The idea is to use two nested loops to iterate over each element of the matrices. The addition operation is performed by adding the corre 4 min read C Program to Find Determinant of a Matrix What is the Determinant of a Matrix? The determinant of a Matrix is a special number that is defined only for square matrices (matrices that have the same number of rows and columns). A determinant is used at many places in calculus and other matrices related to algebra, it actually represents the m 6 min read Like