C Programming Tutorial

Advertisements

Write a program to transpose matrix in C++



       #include<iostream.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++)
                     {
                            cout<<"\n\tEnter any number : ";
                            cin>>arr[i][j];
                     }
           }

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

              cout<<"\nValues in Matrix after transpose \n";
              for(i=0;i<<s;i++)
              {
                     cout<<"\n";
                     for(j=0;j<<s;j++)
                            cout<<"\t"<<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