Saturday, March 25, 2023

Write a C++ Program to Find Transpose of a Matrix

C++ Program to Find Transpose of a Matrix Write a C++ Program to Find Transpose of a Matrix.

The transpose of a matrix is a new matrix obtained by interchanging the rows and columns of the original matrix. For a matrix A, its transpose is denoted by A^T. The number of rows and columns of the transpose are reversed from the original matrix.

For example, consider the following matrix A:

1 2 3 4 5 6

The transpose of A denoted by A^T is:
1 4 2 5 3 6


#include <iostream>
using namespace std;

int main() {
    int rows, cols;

    // Get the size of the matrix
    cout << "Enter the number of rows: ";
    cin >> rows;
    cout << "Enter the number of columns: ";
    cin >> cols;

    // Declare the matrix
    int matrix[rows][cols];

    // Get the matrix elements
    cout << "Enter the elements of the matrix:\n";
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            cin >> matrix[i][j];
        }
    }

    // Print the original matrix
    cout << "\nThe original matrix is:\n";
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            cout << matrix[i][j] << "\t";
        }
        cout << endl;
    }

    // Transpose the matrix
    int transpose[cols][rows];
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }

    // Print the transpose of the matrix
    cout << "\nThe transpose of the matrix is:\n";
    for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
            cout << transpose[i][j] << "\t";
        }
        cout << endl;
    }

    return 0;
}

//===========
UUSER INPUT:
Enter the number of rows: 3
Enter the number of columns: 2
Enter the elements of the matrix:
1 2
3 4
5 6
OUTPUT:
The original matrix is:
1       2
3       4
5       6

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

Explanation:

  1. We start by declaring two integers rows and cols to store the number of rows and columns in the matrix respectively.
  2. We then prompt the user to enter the number of rows and columns using the cout and cin statements.
  3. We declare the matrix int matrix[rows][cols] to store the matrix elements entered by the user.
  4. We prompt the user to enter the matrix elements using nested for loops.
  5. We print the original matrix using another set of nested for loops.
  6. We declare the transpose matrix int transpose[cols][rows] to store the transpose of the matrix.
  7. We use another set of nested for loops to compute the transpose of the matrix and store it in the transpose matrix.
  8. We print the transpose of the matrix using another set of nested for loops.

Notes:

  • A matrix is a two-dimensional array of elements. It can be represented using rows and columns.
  • The transpose of a matrix is obtained by interchanging its rows and columns.
  • To transpose a matrix, we simply need to copy its elements to a new matrix in such a way that the rows become columns and vice versa.
  • In this program, we have used nested for loops to traverse the matrix and its transpose.
  • The outer for loop is used to iterate over the rows, while the inner for loop is used to iterate over the columns.
  • The transpose matrix has the same number of rows as the original matrix and the same number of columns as the original matrix.
  • The elements of the transpose matrix are obtained by swapping the row and column indices of the original matrix.

No comments:

Post a Comment

Write a c++ program to show the difference between an array and a list.

Array vs List Example Write a c++ program to show the difference between an array and a list. Arrays are useful when you know the number ...