C++ Program to Transpose a Matrix

C++ Program to Transpose a Matrix

Matrix transposition is a fundamental operation in linear algebra where the rows of a matrix become columns and the columns become rows. Learning how to transpose a matrix in C++ helps beginners practice arrays, loops, and logical thinking. Matrix transposition is widely used in areas like data analysis, computer graphics, and solving mathematical problems. In this article, we will explore multiple C++ programs to perform matrix transposition, including options for user input, predefined matrices, and colorful outputs for better visualization.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Program 1: Transpose of a Matrix Using User Input

This program allows the user to input a matrix of any size and calculates its transpose. It introduces beginners to arrays, nested loops, and dynamic handling of matrix elements.

#include <iostream>

using namespace std;

int main() {

    int rows, cols;

    cout << "Enter the number of rows: " << endl;
    cin >> rows;

    cout << "Enter the number of columns: " << endl;
    cin >> cols;

    int matrix[rows][cols], transpose[cols][rows];

    cout << "Enter elements of the matrix:" << endl;

    for(int i = 0; i < rows; i++)
        for(int j = 0; j < cols; j++)
            cin >> matrix[i][j];

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

    cout << "Transpose of the matrix is:" << endl;

    for(int i = 0; i < cols; i++) {

        for(int j = 0; j < rows; j++)
            cout << transpose[i][j] << " ";

        cout << endl;

    }

    return 0;

}

This program first reads the number of rows and columns from the user, then stores the elements in a 2D array. Using nested loops, it swaps rows and columns to create the transpose. Beginners learn how array indexing works and how to manipulate data in two dimensions.

Program 2: Transpose of a Predefined Matrix

Sometimes it is helpful for beginners to focus only on the logic without entering matrix elements manually. This program demonstrates transposition with a predefined matrix.

#include <iostream>

using namespace std;

int main() {

    int matrix[3][2] = {{1, 2}, {3, 4}, {5, 6}};
    int transpose[2][3];

    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 2; j++)
            transpose[j][i] = matrix[i][j];

    cout << "Original Matrix:" << endl;

    for(int i = 0; i < 3; i++){

        for(int j = 0; j < 2; j++)
            cout << matrix[i][j] << " ";

        cout << endl;

    }

    cout << "Transpose of the Matrix:" << endl;

    for(int i = 0; i < 2; i++){

        for(int j = 0; j < 3; j++)
            cout << transpose[i][j] << " ";

        cout << endl;

    }

    return 0;

}

This program clearly shows the transformation of a 3×2 matrix into a 2×3 matrix. Beginners can visually follow how each element moves from its original row and column to its new position, strengthening understanding of matrix structure.

Program 3: Colorful Transpose Display for Better Visualization

For a more interactive learning experience, this program displays the original and transposed matrices in different colors, helping beginners easily distinguish them.

#include <iostream>

using namespace std;

int main() {

    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int transpose[3][2];

    // Transpose the matrix
    for(int i = 0; i < 2; i++)
        for(int j = 0; j < 3; j++)
            transpose[j][i] = matrix[i][j];

    // Display original matrix in cyan
    cout << "\033[1;36mOriginal Matrix:\033[0m" << endl;

    for(int i = 0; i < 2; i++){

        for(int j = 0; j < 3; j++)
            cout << "\033[1;36m" << matrix[i][j] << " \033[0m";

        cout << endl;

    }

    // Display transpose in yellow
    cout << "\033[1;33mTranspose Matrix:\033[0m" << endl;

    for(int i = 0; i < 3; i++){

        for(int j = 0; j < 2; j++)
            cout << "\033[1;33m" << transpose[i][j] << " \033[0m";

        cout << endl;

    }

    return 0;

}

Colorful output enhances comprehension by highlighting the original and transposed matrices separately. Beginners can track how rows become columns and understand transposition visually.

Program 4: Colorful Transpose of a 4×4 Predefined Matrix

This program transposes a 4×4 matrix and displays both the original and transposed matrices in different colors for better understanding.

#include <iostream>

using namespace std;

int main() {

    int matrix[4][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16}
    };

    int transpose[4][4];

    // Transpose the matrix
    for(int i = 0; i < 4; i++)
        for(int j = 0; j < 4; j++)
            transpose[j][i] = matrix[i][j];

    // Display original matrix in cyan
    cout << "\033[1;36mOriginal 4x4 Matrix:\033[0m" << endl;

    for(int i = 0; i < 4; i++){

        for(int j = 0; j < 4; j++)
            cout << "\033[1;36m" << matrix[i][j] << " \033[0m";

        cout << endl;

    }

    // Display transpose in yellow
    cout << "\033[1;33mTransposed 4x4 Matrix:\033[0m" << endl;

    for(int i = 0; i < 4; i++){

        for(int j = 0; j < 4; j++)
            cout << "\033[1;33m" << transpose[i][j] << " \033[0m";

        cout << endl;

    }

    return 0;

}

The program uses nested loops to iterate through every element of the original 4×4 matrix. Each element at position (i, j) in the original matrix is placed at (j, i) in the transposed matrix. Using ANSI color codes, the original matrix is displayed in cyan and the transposed matrix in yellow, making it easy for beginners to follow the transformation visually.

Frequently Asked Questions (FAQ)

Q1: What is a transpose of a matrix?
A1: A transpose is a new matrix obtained by turning rows into columns and columns into rows.

Q2: Can we transpose a non-square matrix?
A2: Yes, transposition works for any matrix, square or rectangular.

Q3: Why is transposition important?
A3: Transposing matrices is useful in linear algebra, solving equations, graphics transformations, and data manipulation.

Q4: How do nested loops help in transposition?
A4: Nested loops allow you to access each element by row and column, making it easy to assign it to the correct position in the transposed matrix.

Q5: Why use colorful output for transpose?
A5: Colors help beginners visually distinguish the original matrix from its transpose, improving understanding of element movement.

Conclusion

Transposing a matrix in C++ is a simple yet important concept that helps beginners practice arrays, loops, and logical thinking. By working with user input, predefined matrices, and colorful visualizations, beginners can clearly understand how rows and columns switch places. Practicing matrix transposition prepares learners for more advanced operations, including multiplication, inversion, and solving linear systems.

Additional & References

Beginners can try transposing larger matrices or implementing in-place transposition for square matrices to deepen understanding. Exploring matrix operations in C++ builds a strong foundation in programming and linear algebra.

Scroll to Top