C++ Program to Find Transpose of a Matrix

In this post, you will learn how to Find Transpose of a Matrix using C++ Programming language.

This lesson will teach you how to Find Transpose of a Matrix, with mathematical function, and the for loop statement using the C++ Language. Let’s look at the below source code.

How to Find Transpose of a Matrix?

RUN CODE SNIPPET

Source Code

#include<iostream>
using namespace std;
int main() 
{
   int transpose[10][10], r=3, c=2, i, j;
   int a[3][3] = { {1, 2} , {3, 4} , {5, 6} };
   cout<<"The matrix is:"<<endl;
   for(i=0; i<r; ++i) 
   {
      for(j=0; j<c; ++j)
      cout<<a[i][j]<<" ";
      cout<<endl;
   }
   cout<<endl;
   for(i=0; i<r; ++i)
   for(j=0; j<c; ++j) 
   {
      transpose[j][i] = a[i][j];
   }
   cout<<"The transpose of the matrix is:"<<endl;
   for(i=0; i<c; ++i) 
   {
      for(j=0; j<r; ++j)
      cout<<transpose[i][j]<<" ";
      cout<<endl;
   }
   return 0;
}

Output

The matrix is:
1 2 
3 4 
5 6
The transpose of the matrix is:
1 3 5 
2 4 6

The statements #include<iostream>, using namespace std, int main are the main factors that support the function of the source code.

Now we can look into the working and layout of the code’s function.

  1. Declare the variables transpose, r, c, i, j, a as integers. The variables r, c hold the size of the rows and columns of the transpose array. Declare the variable a as in array with the size of the array in [ ] and assign the value of the array in { }
  2. Declare a for loop with the condition (i=0; i<r; ++i) to verify the number of rows and declare another for loop with the condition (j=0; j<c; ++j)to verify the number of columns, then using the output statement cout display the array a.
  3. Declare two for loop statements with conditions to verify the number of the rows and columns. In the body of the loop include the function transpose[j][i] = a[i][j] where the row variable and the column variable is interchanged. This function performs the Transpose of the Matrix.
  4. Declare a final for loop with the condition to verify the number of rows and columns, with a another for loop in the body of the loop and display the output/answer using the function cout.

Share:

Leave A Reply

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

You May Also Like

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
  • C++
  • January 30, 2022