# Transpose matrix in C

Written by

Pooja Rao

### Transpose matrix in C Program

• Transpose of the matrix means to the matrix obtained after interchanging the rows and columns of the original matrix.
• The transpose of a matrix is an operator that flips a matrix over its diagonal, that is it switches the row and column indices of the matrix by producing another matrix denoted as Aᵀ.
• Thus, if the first matrix has m rows and n columns its transpose matrix should have n rows and m columns.
• Thus,  the elements can be calculated using the following formula: Bi,j = Ai,j, where i is the number of rows and j is the number of columns. B is the transpose of original matrix A.

Approach:

• First we will take the first matrix as our input.
• Next, using the above mentioned formula we calculate the transpose of the matrices.
• We will be using the pointer to array method to pass array to the function.
• We will also see how to return a 2D array to  the main function.
• We usually do not return an array because, in most of the cases, there is no need of array to be returned from a function. Since, on passing the array by its name, the address of its first member is passed and any changes made on its formal arguments reflects on actual arguments.
• But sometimes, there might be a situation, where an array has to be returned from a function, for example, transposing the matrix and  updating the result to another matrix. This can be done by creating a two-dimensional array inside a function, allocating a memory and returning that array.
• We need to allocate memory using malloc because otherwise the function on return will free its stack automatically and will point to an invalid address in memory. Hence in order that the array should be returned to function; in case of 1 D array we used static variable if you recollect; here we will see how to use malloc and return the array.
• The important thing is you need to FREE the space that was allocated manually. Recollect the concept of memory management in C for this.

Code( Using pointer to array method to pass array to function):

``````#include<stdio.h>
int transpose(int (*arr1), int row, int col); //function prototype
int main(){
int arr1;    //declaring array of predefined size 5 x 5
int i, j, row,col;
printf ("Enter the number of rows in the matrix\n");
scanf("%d", &row);
printf ("Enter the number of columns in matrix\n");
scanf("%d", &col);
printf("Enter elements of the Matrix:\n");
for(i = 0; i < row; i++){
for(j = 0; j < col; j++){
scanf("%d", &arr1[i][j]);
}
}

printf("Original strcuture of the Matrix:\n");
for(i = 0; i < row; i++){
for(j = 0; j < col; j++){
printf("%d\t", arr1[i][j]);
}
printf("\n");
}
transpose(arr1, row, col);

return 0;
}
int transpose(int (*arr1), int row, int col){
int i, j;
int trans;
for(i = 0; i < row; i++){ //Performing transpose
for(j = 0; j < col; j++){
trans[j][i] = (*arr1)[j];  //(*arr1) points to an array of 5 integers. initially will point to zeroth 1 D array. When i takes the value 1, even this pointer needs to be incremented in order to point to next 1D array.
}
arr1++;
}
``````   printf("\nTranspose of matrix is as follows:\n");

for(i = 0; i < col; i++){
for(j = 0; j < row; j++){
printf("%d\t", trans[i][j]);
}
printf("\n");
}
}``````

Explanation:

• When we are performing transpose note that the loop has an outer loop which has rows =no.  of rows of original matrix and inner loop which has number of columns of original matrix.
• trans[j][i] indicates that transpose will have number of rows = number of columns of original and number of columns = number of rows of original matrix; hence the assignment of values in this fashion.
• `(*arr1)` points to an array of 5 integers. initially will point to zeroth 1 D array which is the first row of the matrix. When i takes the value 1, even this pointer needs to be incremented in order to point to next 1D array which is row number 2.
• The parenthesis () while declaring pointer to 2D array is essential. Because int (*arr1) is different from int *arr1.
• int *arr1 , it denotes that arr1 becomes an array of 5 pointers.
• int (*arr1) denotes that arr1 is a pointer to an array of 5 integers.
• Transpose when printed has to be printed as n x m hence , outer loop has col of original matrix as the rows and row of original as columns of the transpose.

Output:

``````Enter the number of rows in the matrix
2
Enter the number of columns in matrix
3
Enter elements of the Matrix:
1
2
3
4
5
6
Original structure of the Matrix:
1 2 3
4 5 6
Transpose of matrix is as follows:
1 4
2 5
````3 6````

### Code( Using pointer to array method with returning array to main function):

The below code is same as above, however we have returned the transpose matrix to main in order to understand 2D array return to main() and then gone ahead and printed the resultant matrix through main function.

``````#include<stdio.h>
int **transpose(int (*arr1), int row, int col);              //function prototype for returning an array ; ** is
also used to declare pointer to 2D array, it is    usually used in function declarations rather than for
the array itself.
int main()
{
int arr1;    //declaring array of predefined size 5 x 5
int i, j, row,col;
int **transmat;
printf ("Enter the number of rows in the matrix\n");
scanf("%d", &row);
printf ("Enter the number of columns in matrix\n");
scanf("%d", &col);
printf("Enter elements of the Matrix:\n");
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
scanf("%d", &arr1[i][j]);
}
}

printf("Original structure of the Matrix:\n");
for(i = 0; i < row; i++)
{
for(j = 0; j < col; j++)
{
printf("%d\t", arr1[i][j]);
}
printf("\n");
}
transmat = transpose(arr1, row, col);
printf("\nTranspose of matrix is as follows:\n");
for(i = 0; i < col; i++)
{
for(j = 0; j < row; j++)
{
printf("%d\t", transmat[i][j]);
}
printf("\n");
}

//free the memory
for(i = 0; i < col; i++) {
free(transmat[i]);
}
free(transmat);

return 0;
}
int **transpose(int (*arr1), int row, int col)
{
int i, j;
int **trans;
trans = malloc(sizeof(int*) * row);

for(i = 0; i < 3; i++) {
trans[i] = malloc(sizeof(int*) * col);
}
for(i = 0; i < row; i++) //Performing transpose
{
for(j = 0; j < col; j++)
{
trans[j][i] = (*arr1)[j];  //(*arr1) points to an array of 5 integers. initially will point to zeroth
1 D array. When i takes the value 1, even this pointer needs
to be incremented in order to point to next 1D array.
}

arr1++;
}

return trans;
```` }````

Output:

``````Enter the number of rows in the matrix
2
Enter the number of columns in matrix
3
Enter elements of the Matrix:
1
2
3
4
5
6
Original structure of the Matrix:
1 2 3
4 5 6
Transpose of matrix is as follows:
1 4
2 5
````3 6````