Write a program to find the transpose of a 2D array.

Introduction

In computer programming, matrices play a significant role in representing data and solving complex problems. One essential operation involving matrices is finding the transpose of a 2D array. The transpose of a matrix is formed by swapping its rows with columns, resulting in a new matrix. In this article, we will explore the concept of finding the transpose of a 2D array using a C++ program. By understanding this operation, you can effectively manipulate matrices and solve various mathematical challenges.

Understanding the Transpose of a 2D Array:

The transpose of a 2D array is an operation that transforms its rows into columns and its columns into rows. For a given 2D array of size MxN, the transpose will result in a new array of size NxM.

Writing a C++ Program to Find the Transpose of a 2D Array:

Below is a C++ program that calculates the transpose of a given 2D array.

#include <iostream>



const int MAX_ROWS = 3;

const int MAX_COLS = 3;



// Function to find the transpose of a 2D array

void findTranspose(int original[][MAX_COLS], int transposed[][MAX_ROWS], int rows, int cols) {

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

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

           transposed[j][i] = original[i][j];

        }

    }

}



// Function to display a 2D array

void displayArray(int arr[][MAX_COLS], int rows, int cols) {

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

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

            std::cout << arr[i][j] << " ";

        }

        std::cout << std::endl;

    }

}



int main() {

    int originalArray[MAX_ROWS][MAX_COLS] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

    int transposedArray[MAX_COLS][MAX_ROWS];



    std::cout << "Original Array:" << std::endl;

   displayArray(originalArray, MAX_ROWS, MAX_COLS);



    // Finding the transpose of the original array

   findTranspose(originalArray, transposedArray, MAX_ROWS, MAX_COLS);



    std::cout << "\nTranspose of the Array:" << std::endl;

   displayArray(transposedArray, MAX_COLS, MAX_ROWS);



    return 0;

}

Output:

Original Array:
1 2 3
4 5 6
7 8 9
Transpose of the Array:
1 4 7
2 5 8
3 6 9

Explanation of the Program:

We first define two functions – findTranspose() and displayArray(). The findTranspose() function takes the original 2D array and transposes it into the transposed array. The displayArray() function is used to display the content of a 2D array.

In the main() function, we initialize the originalArray with some sample data. We then display the original array using the displayArray() function.

Next, we find the transpose of the originalArray using the findTranspose() function and store the result in the transposedArray.

Finally, we display the transposed array using the displayArray() function.

Conclusion

The transpose of a 2D array is a fundamental operation that is widely used in matrix manipulations and mathematical calculations. By understanding the concept and implementing a C++ program to find the transpose, you gain valuable insights into working with 2D arrays and manipulating data efficiently.

C++ provides a rich set of tools for handling arrays and matrices, making it a versatile language for various numerical and scientific applications.