Friday, October 30, 2009

Chap8[L]c Transpose 4X4 matrix

Write a program to obtain transpose of a 4 x 4 matrix. The transpose of a matrix is obtained by exchanging the elements of each row with the elements of the corresponding column.


void main()
{
int arr[4][4];
int i,j,a,b,f;
printf("\nInput numbers to 4*4 matrix");

for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("\nKey in the [%d][%d]) value",i+1,j+1);
scanf("%d",&arr[i][j]);
}
}

for(i=0;i<4;i++)
{
for(j=0,f=0;j<4;j++)
{
if(i!=j&&f==0)
continue;

a=arr[i][j];
b=arr[j][i];
arr[i][j]=b;
arr[j][i]=a;
f=1;
}
}

for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf("%d ",arr[i][j]);

printf("\n");
}
}

2 comments:

  1. Check this out....

    #include

    int main()
    {

    int matrix[4][4],i,j,temp,num;

    for(i=0;i<=3;i++)
    {
    printf("\n");
    for(j=0;j<=3;j++)
    {
    printf("\nEnter the %d%d element of Array",i,j);
    scanf("%d",&matrix[i][j]);
    }

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

    for(i=0;i<3;i++)
    {
    printf("\n");
    for(j=i;j<=3;j++)
    {
    if(i==j)
    {
    matrix[i][j]=matrix[j][i];
    }
    else
    {
    temp=matrix[i][j];
    matrix[i][j]=matrix[j][i];
    matrix[j][i]=temp;
    }

    }


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

    }

    ReplyDelete