This is a C++ Program to Find the Transpose of a Matrix.
Method 1:
The program takes a matrix and prints the transpose of the matrix. In a transpose matrix, rows become columns and vice versa.
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.
Here is the source code of C++ Program to Find the Transpose of a Matrix. The program output is shown below.
#include<iostream>
using namespace std;
int main ()
{
int A[10][10], m, n, i, j;
cout << "Enter rows and columns of matrix : ";
cin >> m >> n;
cout << "Enter elements of matrix : ";
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
cin >> A[i][j];
cout << "Entered Matrix : \n ";
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
cout << A[i][j] << " ";
cout << "\n ";
}
cout << "Transpose of Matrix : \n ";
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
cout << A[j][i] << " ";
cout << "\n ";
}
return 0;
}
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.
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
Method 2:
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.
/*
* C++ Program to Generate a Transpose of a Given Matrix of any order
*/
#include <iostream>
int main()
{
std::cout << "Enter the order of matrix ";
int x, y;
std::cin >> x >> y;
double** arr = new double*[x];
for (int i = 0; i < y; i++)
{
arr[i] = new double[y];
}
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
std::cin >> arr[i][j];
}
}
for(int i = 0; i < x; i++)
{
for(int j = i; j < y; j++)
{
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
std::cout << "Transpose Matrix" << std::endl;
for(int i = 0; i < x; i++)
{
for(int j = 0; j < y; j++)
{
std::cout << arr[i][j] << " ";
}
std::cout << "\n";
}
for(int i = 0 ; i < y; i++)
{
delete[] arr[i];
}
delete[] arr;
}
$ 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.
- Check Programming Books
- Apply for Computer Science Internship
- Check Computer Science Books
- Apply for C++ Internship
- Practice Computer Science MCQs