Pages

Wednesday, February 1, 2017

Transpose Matrix

C program to obtain Transpose of a given 3X3 Matrix.


#include<stdio.h>
int main()
{
 int arr[3][3], arrTrans[3][3];
 int i,j;
 //Logic for reading matrix from user...
 printf("Enter the 3x3 matrix:\n");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("Enter the element arr[%d][%d] : ",i,j);
   scanf("%d",&arr[i][j]);
  }
 }

 printf("The Original matrix is: \n");
 for(i=0;i<3;i++)
 {
  for(j=0;j<3;j++)
  {
   printf("\t%d",arr[i][j]);
  }
  printf("\n");
 }

 printf("Press any key to print Transposed Matrix..\n");
 getch();
//Logic for transposing matrix...
 for(i=0;i<3;i++) {
  for(j=0;j<3;j++)
     arrTrans[j][i] = arr[i][j];
 }
 //Logic for printing Transposed matrix...
 printf("The transpose of the matrix is: \n");
 for(i=0;i<3;i++) {
  for(j=0;j<3;j++)
     printf("\t%d",arrTrans[i][j]);
  printf("\n");
 }
 return 0;
}

Output of program

Enter the 3x3 matrix:
Enter the element arr[0][0] : 1
Enter the element arr[0][1] : 2
Enter the element arr[0][2] : 3
Enter the element arr[1][0] : 4
Enter the element arr[1][1] : 5
Enter the element arr[1][2] : 6
Enter the element arr[2][0] : 7
Enter the element arr[2][1] : 8
Enter the element arr[2][2] : 9
The Original matrix is:
        1       2       3
        4       5       6
        7       8       9
Press any key to print Transposed Matrix..
The transpose of the matrix is:
        1       4       7
        2       5       8
        3       6       9

*/

No comments:

Post a Comment