C++ Program to Generate the Transpose of a Given Matrix of Order 3 x 3

«
»
This C++ Program which generates the transpose of a given matrix of order 3 x 3. The program initializes the matrices according to the input, creates second matrix, transposes the elements of the matrix and puts it in second matrix.

Here is source code of the C++ program which generates the transpose of a given matrix. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ program to Generate Transpose of a given 3x3 Matrix
  3.  */
  4. #include<iostream>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int mat[3][3], trans_mat[3][3];
  10.  
  11.     /* Initializing Mat1 and Mat2 */
  12.     for (int i = 0; i < 3; i++)
  13.     { 
  14.         for (int j = 0; j < 3; j++)
  15.         {
  16.             cin >> mat[i][j];	
  17.         }
  18.     }
  19.  
  20.     /* Transposing elements of the matrix */
  21.     for (int i = 0; i < 3; i++)
  22.     {
  23.         for (int j = 0; j < 3; j++)
  24.         {
  25.             trans_mat[j][i] = mat[i][j];	
  26.         }
  27.     }
  28.  
  29.     cout << "Transpose of the Given 3x3 Matrix : " << endl;
  30.     for (int i = 0; i < 3; i++)
  31.     {
  32.         for (int j = 0; j < 3; j++)
  33.         {
  34.             cout << trans_mat[i][j] << "\t";	
  35.         }
  36.         cout << endl;
  37.     }
  38. }

advertisement
$ g++ main.cpp
$ ./a.out
Enter elements of a 3x3 Matrix : 
2	3	4
3	4	5
4	5	6
Transpose of the Given 3x3 Matrix : 
2	3	4	
3	4	5	
4	5	6

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.