用初值初始化二维数组并转置矩阵输出c++
时间: 2023-08-12 21:06:31 浏览: 93
013 C语言用二维数组实现矩阵转置
5星 · 资源好评率100%
可以使用以下代码来实现:
```c++
#include <iostream>
using namespace std;
int main() {
int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int transposed[3][3];
// 转置矩阵
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
transposed[j][i] = arr[i][j];
}
}
// 输出原始矩阵
cout << "Original Matrix: " << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
// 输出转置矩阵
cout << "Transposed Matrix: " << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << transposed[i][j] << " ";
}
cout << endl;
}
return 0;
}
```
输出结果为:
```
Original Matrix:
1 2 3
4 5 6
7 8 9
Transposed Matrix:
1 4 7
2 5 8
3 6 9
```
阅读全文