Here's a C Program to find the transpose of a given matrix with proper explanation and output. This program uses Multidimensional Arrays, Nested Loops and For Loops.
Note: The transpose of a matrix A is another matrix AT created by any one of the following equivalent actions:
- reflect A over its main diagonal (which runs top-left to bottom-right) to obtain AT
- write the rows of A as the columns of AT
- write the columns of A as the rows of AT
# include <stdio.h> # include <conio.h> void main() { int mat[10][10] ; int i, j, row, col ; clrscr() ; printf("Enter the order of the matrix : ") ; scanf("%d %d", &row, &col) ; printf("\nEnter the elements of the matrix : \n\n") ; for(i = 0 ; i < row ; i++) for(j = 0 ; j < col ; j++) scanf("%d", &mat[i][j]) ; printf("\nThe transpose matrix is : \n\n") ; for(i = 0 ; i < col ; i++) { for(j = 0 ; j < row ; j++) { printf("%d \t", mat[j][i]) ; } printf("\n") ; } getch() ; }
Output of the above program
Enter the order of the matrix : 2 3
Enter the elements of the matrix :
1 2 3
4 5 6
The transpose matrix is :
1 4
2 5
3 6
Explanation of above program
In this program, we have a square matrix mat of maximum size 10 x
10. The variables i and j are the loop variables that corresponds to
the row and column of the matrix respectively.
Next, the program prompts the user to enter order of the matrix. Notice the syntax of first scanf. It is used to take two space separated numbers as input. The first number will be stored in row variable and the second number will be stored in col.
Next, using the first Nested For Loop the program populates the matrix mat and using the second nested for loop the program finds out the transpose of the matrix. The transpose is calculated by replacing the rows and columns of the given matrix.
Hi There,
A really interesting, clear and easily readable
C Program to Find Transpose of a Matrix article of interesting and different perspectives' will clap. So much is so well covered here.
I am writing a program. In a situation I want to print something and go to clear the screen but when I clear the screen the printed text also gets cleared. I want it such that after the print we have to click a key for the screen to get cleared. Is there a command or code which does that.
It was cool to see your article pop up in my google search for the process yesterday. Great Guide.
Keep up the good work!
Grazie,
Asha