Program for Matrix Transpose
Matrix Transpose
We will write a C program for matrix transpose and the number will be entered by the user. This will help to understand the basic structure of programming. In this program , we will display the matrix transpose of given input easily by using proper syntax and algorithms.
Working of Program :
In the program, we will require some numbers from the user to display the matrix transpose of given input.
Important Note :
To calculate the matrix transpose, we can interchange the rows and column of the matrix i.e. write the elements of the rows as columns and the elements of columns as rows.
- The matrix transpose is can be flipped version of original matrix.
Syntax of for() Loop:-
for (Initialization, condition, updation) { //code }
Problem 1
Write a program to calculate matrix transpose.
- Firstly, we have to enter the input.
- Then print the matrix transpose.
Code
#include<stdio.h> void transpose (int p[3][3], int t[3][3]); int main () { int i, j; int p[3][3], t[3][3]; printf ("Enter matrix P: \n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf ("Enter the elements of matrix P [%d,%d]: ", i, j); scanf ("%d", &p[i][j]); } } transpose (p, t); printf ("Transpose of matrix P is:\n\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf ("%d ", t[i][j]); } printf ("\n"); } } void transpose (int p[3][3], int t[3][3]) { int row, col; for (row = 0; row < 3; row++) { for (col = 0; col < 3; col++) { t[row][col] = p[col][row]; } } }
Output
Enter matrix P: 2 Enter the elements of matrix P [0,0]: 1 Enter the elements of matrix P [0,1]: 2 Enter the elements of matrix P [0,2]: 3 Enter the elements of matrix P [1,0]: 4 Enter the elements of matrix P [1,1]: 5 Enter the elements of matrix P [1,2]: 6 Enter the elements of matrix P [2,0]: 7 Enter the elements of matrix P [2,1]: 8 Enter the elements of matrix P [2,2]: 9 Transpose of matrix P is: 1 4 7 2 5 8 3 6 9
Note:
In the following program we will display the matrix transpose for given input.
Problem 2
Write a program to calculate the matrix transpose.
- Firstly, we have to enter the number.
- Then print the matrix transpose.
Code
#include<stdio.h> int main() { int mat[10][10] ; int i, j, row, col ; 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") ; } return 0 ; }
Output
Enter the order of the matrix = 3 4 Enter the elements of the matrix = 11 22 33 44 55 66 77 88 99 12 23 34 The transpose matrix is = 11 55 99 22 66 12 33 77 23 4 88 34
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment