Tuesday 26 April 2016

Transpose of a Matrix using C++

#include <iostream>
using namespace std;
int main()
{
    int array[3][3],trans[3][3],i,j;
    for(i=0; i<3; ++i)
    for(j=0; j<3; ++j)
    {
        cout << "Enter elements a" << i+1 << j+1 << ": ";
        cin >> array[i][j];
    }
    cout << endl << "Entered Matrix: " << endl;
    for(i=0; i<3; ++i)
    for(j=0; j<3; ++j)
    {
        cout << " " << array[i][j];
        if(j==3-1)
            cout << endl << endl;
    }

    for(i=0; i<3; ++i)
    for(j=0; j<3; ++j)
    {
       trans[j][i]=array[i][j];
    }

    cout << endl << "Transpose of Matrix: " << endl;
    for(i=0; i<3; ++i)
    for(j=0; j<3; ++j)
    {
        cout << " " << trans[i][j];
        if(j==3-1)
            cout << endl << endl;
    }
    return 0;
}

No comments:

Post a Comment