09 June, 2013

C++ Program To Print Transpose Of A Matrix.

This Post Contains A C++ Program To Print Transpose Of A Matrix With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Arrays', 'Nested Loops' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.

# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



C++ Program To Print Transpose Of A Matrix.

/* Declaration Of Header Files */
#  include  <iostream.h>
#  include  <conio.h>

/* Start Of Main Program */
void main()
{
    
       /* Declaration Of Variables */
       int  i,  j,  r  =  0,  c  =  0, t = 0;
       int  a [ 10 ][ 10 ];
       clrscr();

       /* Asking For The Input From User */
       cout  <<  "  Enter Number Of Rows & Columns Of 2D Array [ Matrix ]  :  ";
       cin  >>  r  >>  c ;
      
       //  Accepting Values Of 2D Array [ Matrix ]
       cout  <<  "  Enter  "  <<  r  *  c  <<  "  Values for 2D Array  :  ";
       for  (  i  =  0;  i  <  r;  i++  )
       {
                for  (  j  =  0;  j  <  c;  j++  )
                {
                         cin  >>  a [ i ][ j ];
                }
       }

       // Printing Values Of 2D Array [ Matrix ]
       cout  <<  "  Values Of 2D Array [ Matrix ] Are  :  ";
       for  (  i  =  0;  i  <  r;  i++  )
       {
                cout  <<  " \n ";
                for  (  j  =  0;  j  <  c;  j++  )
                {
                         cin  >>  a [ i ][ j ];
                }
       }

/* Source Code For Computing Transpose Of Matrix  If & Only If Rows & Columns Are Equal */
if(r==c)
{
   for(i=0; i<r; i++)
   {
      for(j=0; j<i; j++)
      {
         t=a[i][j];
         a[i][j]=a[j][i];
         a[j][i]=t;
      }
   }
/* Printing The Output Onto The Screen/Console */
   cout<<" \n After Transposing : ";
   for(i=0; i<r; i++)
   {
      cout<<"\n";
      for(j=0; j<c; j++)
      {
         cout<<a[i][j]<<"\t";
      }
   }
}
else
{
   cout<<" \n Transpose Is Not Possible";
}
getch();
}
/* End Of Main Program */


Output :

Enter Order For Array A : 3 3

Enter 9 Values For Array :
1  2  3  4  5  6  7  8  9

Array A Is :
1  2  3
4  5  6
7  8  9

After Transpose :
1  4  7
2  5  8
3  6  9




No comments:

Post a Comment

Subscribe To:

Most Commonly Asked Programs In 'C' & 'C++' Language.

Blog Archive