C Program to Find Transpose of a Matrix
In this program, user is asked to entered the number of rows r and columns c. The value of r and c should be less than 10 in this program.
The user is asked to enter elements of the matrix (of order r*c).
Then, the program computes the transpose of the matrix and displays it on the screen.
Example: Program to Find Transpose of a Matrix
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
// Storing elements of the matrix
printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
// Displaying the matrix a[][] */
printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
if (j == c-1)
printf("\n\n");
}
// Finding the transpose of matrix a
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}
// Displaying the transpose of matrix a
printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);
if(j==r-1)
printf("\n\n");
}
return 0;
}Output
Enter rows and columns of matrix: 2 3 Enter element of matrix: Enter element a11: 2 Enter element a12: 3 Enter element a13: 4 Enter element a21: 5 Enter element a22: 6 Enter element a23: 4 Entered Matrix: 2 3 4 5 6 4 Transpose of Matrix: 2 5 3 6 4 4
step-by-step explanation of a C program to find the transpose of a matrix:
#include <stdio.h>
#define ROWS 3
#define COLS 3
// function prototype
void transpose(int a[][COLS], int t[][ROWS]);
int main() {
int a[ROWS][COLS], t[COLS][ROWS];
printf("Enter elements of matrix A:\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
scanf("%d", &a[i][j]);
}
}
transpose(a, t); // call the transpose() function and pass matrices a and t
printf("Transpose of matrix A:\n");
for (int i = 0; i < COLS; i++) {
for (int j = 0; j < ROWS; j++) {
printf("%d ", t[i][j]);
}
printf("\n");
}
return 0;
}
// function definition
void transpose(int a[][COLS], int t[][ROWS]) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
t[j][i] = a[i][j];
}
}
}
Let’s break it down step-by-step:
We include the necessary header file
stdio.hfor input/output functions.We define constants
ROWSandCOLSto represent the number of rows and columns of the matrices.We declare two integer matrices
aandtof sizeROWSxCOLSandCOLSxROWS, respectively.We prompt the user to enter the elements of matrix
ausing nestedforloops and read them in usingscanf().We call the
transpose()function and pass the matricesaandtas arguments.The
transpose()function takes in two 2D integer arraysaandt. Inside the function, we use nestedforloops to iterate over all the elements of matrixa.For each element of
a[i][j], we assign it tot[j][i]to obtain the transpose of matrixa.Once the function completes execution, the values of matrix
tinmain()are updated with the transpose of matrixa.We print the transpose of matrix
ausing nestedforloops andprintf().Finally, we return 0 to indicate successful completion of the program.