C Programming - Learn to Transpose a Matrix

in c-programming •  2 years ago 

Welcome back people,
I am here with a new lesson for everyone. We are currently performing matrix functions/operations using C programing language.

Today we will learn how to perform transpose of a matrix.

Here is the program itself.

Screenshot 2023-01-27 211106.png

code continued.

Screenshot 2023-01-27 211123.png

You can copy the code from below :

#include <stdio.h>

#define ROW 3
#define COL 4

int main()
{
int originalMatrix[ROW][COL], transposedMatrix[COL][ROW];
int i, j;

printf("Enter elements of the original matrix:\n");
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
scanf("%d", &originalMatrix[i][j]);
}
}

printf("The original matrix is:\n");
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
printf("%d ", originalMatrix[i][j]);
}
printf("\n");
}

//transpose the matrix
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
transposedMatrix[j][i] = originalMatrix[i][j];
}
}

printf("\nThe transposed matrix is:\n");
for (i = 0; i < COL; i++)
{
for (j = 0; j < ROW; j++)
{
printf("%d ", transposedMatrix[i][j]);
}
printf("\n");
}

return 0;
}

This program uses a 3x4 matrix as an example, but the dimensions can be changed by modifying the ROW and COL macro definitions. The original matrix is read in from the user, and then its transpose is calculated and stored in the transposedMatrix variable. The transposed matrix is then printed out for the user to see.

Now let us look at the output and see if we are able to transpose the matrix successfully or not.

image.png

So we can see that the matrix was transpose successfully. In case you do not know about Matrix transpose here is a word about it.

The transpose of a matrix is a new matrix in which the rows and columns of the original matrix are switched. If the original matrix is an m x n matrix, the transpose will be an n x m matrix. The element in the ith row and jth column of the original matrix will become the element in the jth row and ith column of the transpose. It is denoted as A^T.

If you also want to learn coding then find your teacher according to your language. For me i am comfortable with hindi language and follow hindi teachers.

You can refer to my teacher below in case you are also comfortable with hindi.

Source

I will soon call myself a trader and a small programmer together.

Happy trading and keep learning what you love.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!
Sort Order:  

Programming is not just for me. I respect anyone who has a knowledge about it. I respect them in a different way. Weldon buddy