C++ 程序:执行矩阵转置

原文:https://www.studytonight.com/cpp-programs/program-to-perform-transpose-of-a-matrix-in-cpp

下面是执行矩阵转置的程序。

#include<iostream.h>

void main()
{
    int mat1[3][3], mat2[3][3];
    int i, j, k;
    cout<<"Enter the elements of Matrix(3X3) : ";
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            cin>>mat1[i][j];
        }
    }
    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];
        }
    }
    cout<<"\nTransposed matrix is : "<<endl;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            cout<<mat2[i][j]<<" ";
        }
        cout<<endl;
    }
}

输入矩阵的元素(3x 3):1 2 3 4 5 6 7 8 9 矩阵为: 1 2 3 4 5 6 7 8 9 转置矩阵为: 1 4 7 2 5 8 3 6 9