注意new的使用
lab6_2.cpp
#include <iostream>
using namespace std;
void swap(int &a, int &b)
{
/********** Begin **********/
int tmp = a;
a = b;
b = tmp;
/********** End **********/
}
int main()
{
/********** Begin **********/
cout << "输入9个整数作为矩阵元素值" << endl;
int **a;
a = new int *[3];
for (int i = 0; i < 3; ++i)
a[i] = new int[3];
cout << "初始矩阵:" << endl;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
cin >> a[i][j], cout << a[i][j] << ' ';
cout << endl;
}
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i; ++j)
swap(a[i][j], a[j][i]);
cout << "转置矩阵:" << endl;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
cout << a[i][j] << ' ';
cout << endl;
}
/********** End **********/
return 0;
}