#include
using namespace std;
const int row = 2;
const int column = 2;
void PrintMatrix(int m[][column]);
void ReverseMatrix(int matrix[][column]);

void main()
{

``````cout<<"请输入"<<(row * column)<<"个数字"<<endl;
int matrix[row][column];
for(int i = 0; i < row; ++i)
{
for(int j = 0; j < column; ++j)
{
cin>>matrix[i][j];
}
}
PrintMatrix(matrix);
ReverseMatrix(matrix);
cout<<"转置后："<<endl;
PrintMatrix(matrix);
``````

}

void PrintMatrix(int matrix[][column])
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl<<endl<<endl;
}
}

void ReverseMatrix(int matrix[][column])
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}

//求解释为什么矩阵转置不成功，还是原来的矩阵

1个回答

for(int j = 0; j < column; j++)应该改成for(int j = 0; j < i; j++)

`````` #include <iostream>
using namespace std;
const int row = 2;
const int column = 2;
void PrintMatrix(int m[][column]);
void ReverseMatrix(int matrix[][column]);

void main()
{

cout << "请输入" << (row * column) << "个数字" << endl;
int matrix[row][column];
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
cin >> matrix[i][j];
}
}
PrintMatrix(matrix);
ReverseMatrix(matrix);
cout << "转置后：" << endl;
PrintMatrix(matrix);
}

void PrintMatrix(int matrix[][column])
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
cout << matrix[i][j] << "  ";
}
cout << endl << endl << endl;
}
}

void ReverseMatrix(int matrix[][column])
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < i; j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
``````