Q:

C++ Program to Find Transpose of a Matrix using array

belongs to collection: C++ Arrays Solved Programs

0

Write a C++ Program to Find Transpose of a Matrix using array. Here’s simple Program to Find Transpose of a Matrix using array in C++ Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

What is Matrix ?


Matrix representation is a method used by a computer language to store matrices of more than one dimension in memory. C uses “Row Major”, which stores all the elements for a given row contiguously in memory.

 
 

Two-dimensional Arrays : :

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. An m × n (read as m by n) order matrix is a set of numbers arranged in m rows and n columns.

To declare a two-dimensional integer array of size [x][y], you would write something as follows −

 
  • type arrayName [ x ][ y ];

Where type can be any valid C data type and arrayName will be a valid C identifier.


To transpose matrix in C++ Programming language, you have to first ask to the user to enter the matrix and replace row by column and column by row to transpose that matrix, then display the transpose of the matrix on the screen.

Below is the source code for C++ Program to Find Transpose of a Matrix using array which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/*  C++ Program to Find Transpose of a Matrix using array  */

#include <iostream>
using namespace std;

int main()
{
    int a[5][5], trans[5][5], r, c, i, j;

    cout << "Enter rows of matrix: ";
    cin >> r;
    cout << "Enter columns of matrix: ";
    cin >> c;

    cout<<"\nEnter Elements to Matrix Below :: \n";

    for(i=0;i<r;i++)
    {
        for(j=0;j<c;++j)
        {
            cout<<"\nEnter a1["<<i<<"]["<<j<<"] Element :: ";
            cin>>a[i][j];
        }

    }
    // Displaying the matrix a[][]
    cout << "\n The Entered Matrix is :: \n" << endl;
    for (i = 0; i < r; ++i)
        {
            for (j = 0; j < c; ++j)
            {
                cout<<"\t"<<a[i][j];
            }
            printf("\n\n");
        }

    // Finding transpose of matrix a[][] and storing it in array trans[][].
    for(i = 0; i < r; ++i)
        for(j = 0; j < c; ++j)
        {
            trans[j][i]=a[i][j];
        }

    // Displaying the transpose,i.e, Displaying array trans[][].
    cout << endl << "Transpose of Matrix :: " << endl;
    for (i = 0; i < r; ++i)
        {
            for (j = 0; j < c; ++j)
            {
                cout<<"\t"<<trans[i][j];
            }
            printf("\n\n");
        }

    return 0;
}

OUTPUT : :


/*  C++ Program to Find Transpose of a Matrix using array  */

Enter rows of matrix: 3
Enter columns of matrix: 3

Enter Elements to Matrix Below ::

Enter a1[0][0] Element :: 1

Enter a1[0][1] Element :: 2

Enter a1[0][2] Element :: 3

Enter a1[1][0] Element :: 4

Enter a1[1][1] Element :: 5

Enter a1[1][2] Element :: 6

Enter a1[2][0] Element :: 7

Enter a1[2][1] Element :: 8

Enter a1[2][2] Element :: 9

 The Entered Matrix is ::

        1       2       3

        4       5       6

        7       8       9


Transpose of Matrix ::
        1       4       7

        2       5       8

        3       6       9


Process returned 0

Above is the source code for C++ Program to Find Transpose of a Matrix using array which is successfully compiled and run on Windows System.The Output of the program is shown above .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Arrays Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Program to Subtract Two Matrices using array... >>
<< C++ Program to Multiply Two Matrices using array...