C++ Program to Find Transpose of a Matrix

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

Method 1:

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.

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

Note: Join free Sanfoundry classes at Telegram or Youtube

Method 2:

This C++ program generates a transpose of a given matrix of any order. It takes the size of the array from standard input and allocates memory for it using new[]. The elements are then stored into the multidimensional array and the transpose is obtained using a conventional method. The transpose is printed and the memory is finally deallocated using delete[].

Here is the source code of the C++ program generates a transpose of given matrix of any order. 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 a Transpose of a Given Matrix of any order
  3.  */
  4. #include <iostream>
  5.  
  6. int main()
  7. {
  8.     std::cout << "Enter the order of matrix ";
  9.     int x, y;
  10.     std::cin >> x >> y;
  11.  
  12.     double** arr = new double*[x];
  13.     for (int i = 0; i < y; i++)
  14.     {
  15.         arr[i] = new double[y];
  16.     }
  17.     for (int i = 0; i < x; i++)
  18.     {
  19.         for (int j = 0; j < y; j++)
  20.         {
  21.             std::cin >> arr[i][j];
  22.         }
  23.     }
  24.     for(int i = 0; i < x; i++)
  25.     {
  26.         for(int j = i; j < y; j++)
  27.         {
  28.             int temp = arr[i][j];
  29.             arr[i][j] = arr[j][i];
  30.             arr[j][i] = temp;
  31.         }
  32.     }
  33.     std::cout << "Transpose Matrix" << std::endl;
  34.     for(int i = 0; i < x; i++)
  35.     {
  36.         for(int j = 0; j < y; j++)
  37.         {
  38.             std::cout << arr[i][j] << " ";
  39.         }
  40.         std::cout << "\n";
  41.     }
  42.     for(int i = 0 ; i < y; i++)
  43.     {
  44.         delete[] arr[i];
  45.     }
  46.     delete[] arr;
  47. }

advertisement
$ a.out
Enter the order of matrix 3 3
1 2 3
4 5 6
7 8 9
Transpose Matrix
1 4 7
2 5 8
3 6 9

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

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

advertisement

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.