C prog to find transpose of a matrix using single 2-d array only

1 comment
#include<stdio.h>
#include<conio.h>
int main()
{
int a[100][100],m,n,t,i,j;
printf("Enter the value of m & n for mxn matrix:");
scanf("%d%d",&m,&n);
for(i=0;i<n;i++){
for(j=0;j<m;j++){
printf("\nEnter the number:");
scanf("%d",&a[i][j]);
}
}
printf("\nThe above matrix is\n\n");
for(i=0;i<n;i++){
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
printf("\n\n");
}
if(n>=m)
{
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
t=a[i][j];
a[i][j]=a[j][i];
a[j][i]=t;

}
}
}
else
{
for(i=0;i{
for(j=i+1;j{
t=a[i][j];
a[i][j]=a[j][i];
a[j][i]=t;

}
}
}
printf("\nThe resultant matrix after trasposing is\n\n");
for(i=0;i<n;i++){
for(j=0;j<m;j++){
printf("%d\t",a[i][j]);
}
printf("\n\n");
}
getche();
return 0;
}

1 comments:

zzzzz said...

The transpose of matrix A is written AT.
The ith row, jth column element of A is the jth row, ith column element of AT.
If A is an m × n matrix then AT is an n × m matrix.