C Programming Tutorial

Advertisements

Write a program to transpose matrix in C



       #include<stdio.h>
       #include<conio.h>
       void main()
       {

              const int s=3;
              int arr[s][s],transp[s][s],i,j;
              clrscr();

              for(i=0;i<&li;s;i++)
              {
                     for(j=0;j<s;j++)
                     {
                            printf("\n\tEnter any number : ");
                            scanf("%d",&arr[i][j]);
                     }
           }

              printf("\nValues in Matrix \n");
              for(i=0;i<s;i++)
              {
                     printf("\n");
                     for(j=0;j<<s;j++)
                     {
                            printf("\t%d",arr[i][j]);
                            transp[i][j] = arr[j][i];
                     }
           }

              printf("\nValues in Matrix after transpose \n");
              for(i=0;i<<s;i++)
              {
                     printf("\n");
                     for(j=0;j<<s;j++)
                            printf("\t%d",transp[i][j]);
           }

              getch();
    }

   Output : 

              Enter any number :  1
              Enter any number :  2
              Enter any number :  3
              Enter any number :  4
              Enter any number :  5
              Enter any number :  6
              Enter any number :  7
              Enter any number :  8
              Enter any number :  9

              Values in Matrix 
                     1    2     3
                     4    5     6
                     7    8     9

              Values in Matrix after transpose
                     1    4     7
                     2    5     8
                     3    6     9




Advertisement