BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C++ Program to Find and display the Transpose of a Matrix

Last Updated: September 4, 2017 by Chaitanya Singh | Filed Under: C++ Programs

In this C++ tutorial, we will see how to find the transpose of a matrix, before going through the program, lets understand what is the transpose of matrix.
Lets say we have a matrix:

----------------
| 1  | 2  | 3  |
| 4  | 5  | 6  |
| 7  | 8  | 9  |
| 10 | 11 | 12 |
----------------

The transpose of this matrix is shown below: Rows and columns are interchanged, rows of original matrix becomes column in transpose and columns of original matrix becomes rows in transpose.

---------------------
| 1  | 4  | 7  | 10 |
| 2  | 5  | 8  | 11 |
| 3  | 6  | 9  | 12 |
---------------------

Let’s implement this logic in a C++ program. To understand the program you should have a basic knowledge of arrays and multidimensional array.

C++ program to find transpose of a matrix

#include<iostream>
using namespace std;
int main(){
   int matrix[10][10], transMatrix[10][10], row, col;
   //Getting the rows from user and storing in row
   cout<<"Enter the number of rows: ";
   cin>>row;
   //Getting the columns from user and storing in col
   cout<<"Enter the number of columns: ";
   cin>>col;

   /* Asking the user to input the elements of matrix
    * and storing them in the matrix array
    */
   cout<<"Enter elements of matrix: "<<endl;
   for(int i =0;i<row;i++) {
     for(int j=0;j<col;j++) {
       cin>>matrix[i][j];
     }
   }
   // Finding the transpose matrix.
  for(int i=0;i<row;i++) {
    for(int j=0;j<col;j++) {
      transMatrix[j][i] = matrix[i][j];
    }
  }
  //Displaying the transpose matrix
  cout<<"Transpose of Matrix: "<<endl;
  for(int i=0;i<col;i++) {
    for(int j=0;j<row;j++) {
      cout<<transMatrix[i][j]<<" ";
      /* This is just to format the output
       * so you can see the matrix format
       * in the output transpose matrix.
       */
      if(j==row-1)
        cout<<endl;
    }
  }
  return 0;
}

Output:

Enter the number of rows: 4
Enter the number of columns: 3
Enter elements of matrix: 
1  2  3
4  5  6
7  8  9
10 11 12
Transpose of Matrix: 
1 4 7 10 
2 5 8 11 
3 6 9 12 

Top Related Articles:

  1. C++ Program to add two numbers
  2. C++ Program to Find largest element in an array
  3. C++ Program to Add two Matrices
  4. C++ Program to find the Missing Number
  5. C++ Program to Find Second Smallest Element in an Array

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap