Transpose of a matrix is obtained by changing the row elements to column elements and column elements to row elements.
If the order of the matrix ‘A’ is r * c, then the order of transposed matrix ‘A’ will be c * r. If the matrix is a square matrix then the order of original and the transposed matrices will be same.
Below figure explains the transpose of matrix:
In C programming language Transpose of a given matrix(square or non square) can be found in following 2 ways:
- Without using function.
- Using user defined function.
Both the method will work fine. In the second method we are going to pass parameters to function transpose().
Description:
r and c – Number of rows and columns of matrix.
a – Original or input matrix which is a 2 – Dimensional array. Order of matrix ‘a’ is r * c.
t – Transposed matrix which is a 2 – D array. Order of matrix ‘t’ is c * r.
Input : 2 Dimensional array matrix.
Output : Transpose of 2 Dimensional array matrix.
Now let us write an Algorithm and C program to solve the given problem using both methods.
Algorithm to find the transpose of matrix without using function:
STEP 2: READ number of rows and columns of matrix
READ r, c
STEP 3: READ the elements of original matrix
LOOP from i = 0 to r
LOOP from j = 0 to c
READ a(i)(j)
STEP 4: PRINT the elements of original matrix
LOOP from i = 0 to r
LOOP from j = 0 to c
PRINT a(i)(j)
STEP 5: Find the transpose of the matrix
LOOP from i = 0 to r
LOOP from j = 0 to c
t(j)(i) = a(i)(j)
STEP 6: PRINT the transposed matrix
LOOP from i = 0 to c
LOOP from j = 0 to r
PRINT a(i)(j)
STEP 7: END
Example: C program to find the transpose of matrix without using function:
#include<stdio.h>
#include<conio.h>
int main() {
int a[10][10], t[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
printf("Enter matrix elements:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
scanf("%d", &a[i][j]);
printf("Entered matrix: \n");
for (i = 0; i < r; ++i){
for (j = 0; j < c; ++j)
printf("%d\t", a[i][j]);
printf("\n");
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
t[j][i] = a[i][j];
}
printf("\nTranspose of the matrix:\n");
for (i = 0; i < c; ++i){
for (j = 0; j < r; ++j)
printf("%d\t", t[i][j]);
printf("\n");
}
getch();
return 0;
}
Output:
Enter matrix elements:
1 2 3 4
9 8 7 6
Entered matrix:
1 2 3 4
9 8 7 6
Transpose of the matrix:
1 9
2 8
3 7
4 6
Algorithm to find the transpose of matrix using function:
STEP 2: READ number of rows and columns of matrix
READ r, c
STEP 3: READ the elements of original matrix
LOOP from i = 0 to r
LOOP from j = 0 to c
READ a(i)(j)
STEP 4: PRINT the elements of original matrix
LOOP from i = 0 to r
LOOP from j = 0 to c
PRINT a(i)(j)
STEP 5: CALL the transpose() function and pass the arguments a, r and c
transpose(a, r, c)
LOOP from i = 0 to r
LOOP from j = 0 to c
Find the transpose of the matrix
t(j)(i) = a(i)(j)
STEP 6: PRINT the transposed matrix
LOOP from i = 0 to c
LOOP from j = 0 to r
PRINT a(i)(j)
STEP 7: END
Example: C program to find the transpose of matrix using transpose() user defined function.
#include<conio.h>
void transpose(int a[10][10], int r, int c)
{
int t[10][10],i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
t[j][i]=a[i][j];
printf("Transpose of the matrix:\n");
for(i=0;i<c;i++){
for(j=0;j<r;j++)
printf("%d\t",t[i][j]);
printf("\n");
}
}
int main()
{
int r,c,a[10][10],i,j;
printf("Enter the number of rows and columns: ");
scanf("%d %d",&r,&c);
printf("Enter elements of the matrix: \n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
printf("Original matrix is: \n");
for(i=0;i<r;i++){
for(j=0;j<c;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
transpose(a,r,c);
getch();
return 0;
}
Output:
Enter elements of the matrix:
2 9 0 4
5 1 5 7
5 4 2 3
Original matrix is:
2 9 0 4
5 1 5 7
5 4 2 3
Transpose of the matrix:
2 5 5
9 1 4
0 5 2
4 7 3
QUIZ:
1] Transpose of a matrix means
- Rows and columns are interchanged.
- Number of rows and columns are equal.
- Its a square matrix.
- Its a null matrix.
2] Order of transposed matrix is
- Always same as original matrix.
- Always different than original matrix.
- Order is reversed.
- All the above.
3] To take matrix input we can use.
- One for loop.
- Two nested for loops.
- Two for loops.
- None of the above.
4] ‘\t’ is
- Vertical tab.
- Horizontal tab.
- New line character.
- None of the above.
NOTE: Arrays are collection of elements of same type of data. Two dimensional arrays are used to represent matrices. 2 D arrays have two subscripts. Here we are using static arrays to store the matrix elements.