TRANSPOSE OF A MATRIX USING SWAP METHOD
Consider the following I/O samples.
SAMPLE INPUT : 3
4 7 9
3 6 2
0 4 1
3 6 2
0 4 1
SAMPLE OUTPUT: 4 3 0
7 6 4
9 2 1
#include <stdio.h>
int main()
{
int n,i,j,arr[100][100],temp;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
printf("\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
The given source code in C program is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free.
7 6 4
9 2 1
C PROGRAM:
#include <stdio.h>
int main()
{
int n,i,j,arr[100][100],temp;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
printf("\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
The given source code in C program is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free.
If you have any feedback about this article and want to improve this, please comment in the comment section.
Comments
Post a Comment