C++ Program to Find the Transpose of a Matrix

«
»

This is a C++ Program to Find the Transpose of a Matrix.

Problem Description

The program takes a matrix and prints the transpose of the matrix. In a transpose matrix, rows become columns and vice versa.

Problem Solution

1. Initially, the number of rows and columns of the matrix are entered, followed by the matrix elements.
2. The tranpose of the matrix is printed.
3. Exit.

advertisement
C++ Program/Source code

Here is the source code of C++ Program to Find the Transpose of a Matrix. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int A[10][10], m, n, i, j;
  6.     cout << "Enter rows and columns of matrix : ";
  7.     cin >> m >> n;
  8.     cout << "Enter elements of matrix : ";
  9.     for (i = 0; i < m; i++)
  10.         for (j = 0; j < n; j++)
  11.             cin >> A[i][j];
  12.     cout << "Entered Matrix : \n ";
  13.     for (i = 0; i < m; i++)
  14.     { 
  15.         for (j = 0; j < n; j++)
  16.             cout << A[i][j] << " ";
  17.         cout << "\n ";
  18.     }
  19.     cout << "Transpose of Matrix : \n ";
  20.     for (i = 0; i < n; i++)
  21.     {
  22.         for (j = 0; j < m; j++)
  23.             cout << A[j][i] << " ";
  24.         cout << "\n ";
  25.     }
  26.     return 0;
  27. }
Program Explanation

1. The user is asked to enter the number of rows and columns of the matrix.
2. The elements of the matrix are asked to enter and stored in the matrix ‘A’.
3. The transpose is found by exchanging the rows with columns and columns with rows.
4. The original matrix and the transpose are both printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter rows and columns of matrix : 3 3
Enter elements of matrix : 9 8 7 6 5 4 3 2 1
Entered Matrix :
 9 8 7
 6 5 4
 3 2 1 
Transpose of Matrix :
 9 6 3
 8 5 2
 7 4 1
 
Case 2 :
Enter rows and columns of matrix : 2 2
Enter elements of matrix : 1 2 3 4 
Entered Matrix :
 1 2
 3 4
Transpose of Matrix :
 1 3
 2 4
 
Case 3 :
Enter rows and columns of matrix : 2 3
Enter elements of matrix : 0 2 4 6 8 10
Entered Matrix :
 0 2 4 
 6 8 10 
Transpose of Matrix :
 0 6
 2 8
 4 10

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

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

advertisement

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter