#include<iostream>
using namespace std;
//写一函数将一个3*3数组转置
void martrix(int* p)
{
for (int i = 0; i < 3; i++)
for (int j = i; j < 3; j++)
{
int temp = *(p + 3 * i + j);
*(p + 3 * i + j) = *(p + 3 * j + i);
*(p + 3 * j + i) = temp;
}
}
int main()
{
int a[3][3];
cout << "输入3*3矩阵:" << endl;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cin >> a[i][j];
martrix(a[0]); //二维数组指针,首元素是a[0],而不是a
cout << "转置后的矩阵:" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
cout << a[i][j] << " ";
cout << endl;
}
return 0;
}
谭浩强c++第三版6-9
最新推荐文章于 2024-09-09 12:31:15 发布