Program to Perform Transpose of a Matrix

Beginner

Introduction

This lab will guide you through the process of performing the transpose of a matrix in C++. The transpose of a matrix is obtained by interchanging the rows and columns of a matrix, which results in a new matrix in which columns become rows and rows become columns.

Initializing the Matrices

  1. Declare two 3x3 matrices, mat1 and mat2.
  2. Ask the user to input the elements of the matrix using cout and cin.
  3. Display the original matrix mat1 using cout.
#include <iostream>

using namespace std;

int main()
{
    // Initializing matrices
    int mat1[3][3], mat2[3][3];
    int i, j;

    // Getting elements input by user
    cout << "Enter the elements of Matrix (3x3): " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cin >> mat1[i][j];
        }
    }

    // Displaying the original matrix
    cout << "\nMatrix is: " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cout << mat1[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

Transposing the Matrices

  1. Perform the transpose of the matrix by switching the rows and columns of the original matrix.
  2. Store the result of the transposed matrix in mat2.
#include <iostream>

using namespace std;

int main()
{
    // Initializing matrices
    int mat1[3][3], mat2[3][3];
    int i, j;

    // Getting elements input by user
    cout << "Enter the elements of Matrix (3x3): " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cin >> mat1[i][j];
        }
    }

    // Displaying the original matrix
    cout << "\nMatrix is: " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cout << mat1[i][j] << " ";
        }
        cout << endl;
    }

    // Transposing Matrices
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            mat2[i][j] = mat1[j][i];
        }
    }

    // Displaying the transposed matrix
    cout << "\nTransposed matrix is: " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cout << mat2[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

Full Code

#include <iostream>

using namespace std;

int main()
{
    // Initializing matrices
    int mat1[3][3], mat2[3][3];
    int i, j;

    // Getting elements input by user
    cout << "Enter the elements of Matrix (3x3): " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cin >> mat1[i][j];
        }
    }

    // Displaying the original matrix
    cout << "\nMatrix is: " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cout << mat1[i][j] << " ";
        }
        cout << endl;
    }

    // Transposing Matrices
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            mat2[i][j] = mat1[j][i];
        }
    }

    // Displaying the transposed matrix
    cout << "\nTransposed matrix is: " << endl;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cout << mat2[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}

Summary

This lab has guided you through the process of performing the transpose of a matrix in C++. By following the given steps, you should now have a better understanding of how to initialize and transpose matrices. The transpose of a matrix is an important operation in linear algebra, and performing it in C++ can help you with various computational tasks.

Other Tutorials you may like