Transpose Matrix using Array in C++


Example Program:

Here is the simple program for printing the transpose matrix using array in C++.

Source Code

#include<iostream>
using namespace std;
int main()
{
  int matrix1[10][10],matrix2[10][10],row,col;
  cout<<"\nEnter the number of rows: ";
  cin>>row;
  cout<<"\nEnter the number of columns: ";
  cin>>col;
  cout<<"\nEnter elements of matrix: "<<endl;
  for(int i=0;i<row;i++)
  {
    for(int j=0;j<col;j++)
    {
       cin>>matrix1[i][j];
    }
  }
  for(int i=0;i<row;i++)
  {
    for(int j=0;j<col;j++)
    {
       matrix2[j][i]=matrix1[i][j];
    }
  }
  cout<<"Transpose of Matrix: "<<endl;
  for(int i=0;i<col;i++)
  {
    for(int j=0;j<row;j++)
    {
       cout<<matrix2[i][j]<<" ";
       if(j==row-1)
        cout<<endl;
    }
  }
  return 0;
}
To download raw file Click Here

Output

Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of matrix: 
1
2
3
4
Transpose of Matrix: 
1 3 
2 4 

Basic Programs


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples



Learn All in Tamil © Designed & Developed By Tutor Joes