2

C program to transpose of a matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[5][5],b[5][5],m,n;
clrscr();
printf("Enter the number of rows and columns for matrice:(max 5)");
scanf("%d%d",&m,&n);
printf("Enter the elements of matrix");
for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
   {
    scanf("%d",&a[i][j]);
   }
 }

 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
   {
    b[i][j]=a[j][i];
   }
 }
printf("\nNew matrix after transpose is: \n");


 for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
    {
     printf(" %d",b[i][j]);
    }
   printf("\n");
  }
getch();
}

Here we have a program for transpose of a matrix first of all let us understand what is transpose of a matrix. lets say we have a matrix

3 4 6                            3 4 2
4 8 9 ----> transpose 4 8 5
2 5 7 6 9 7

A transposed matrix is a matrix whose columns are the rows of the original
matrix or vice versa . As you can see that the matrix above has its rows changed by its columns.

Now lets understand the code :

first of all we are taking matrix as input from the user By running For loop here a[i][j] represents an array A with two index which makes it 2d array i.e. a matrix with i as rows and j as column. so to Transpose we just need to interchange rows by column.

so for this we again execute a for loop this time we store the transpose in a new matrix b. by changing the position of i and j in the index of matrix a the matrix is transposed. then just simply print the matrix b by running a for loop.

ks007

2 Comments

    • Well devlopersguide is not a company actually. It is just a small blog all you can do is just share our blogs.
      Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *