求一个3×3矩阵的转置矩阵,并输出到屏幕上。
时间: 2023-09-15 14:24:28 浏览: 44
好的,下面是3x3矩阵的转置矩阵:
```
1 2 3
4 5 6
7 8 9
```
转置后的矩阵为:
```
1 4 7
2 5 8
3 6 9
```
以下是C++代码实现:
```c++
#include <iostream>
using namespace std;
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int transpose[3][3];
// 计算转置矩阵
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
transpose[j][i] = matrix[i][j];
}
}
// 输出转置矩阵
cout << "转置矩阵为:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << transpose[i][j] << " ";
}
cout << endl;
}
return 0;
}
```
输出结果为:
```
转置矩阵为:
1 4 7
2 5 8
3 6 9
```
相关问题
c++编一将3×3的矩阵转置的程序
以下是一个 C++ 编写的将 3x3 矩阵转置的程序:
```cpp
#include <iostream>
using namespace std;
int main()
{
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
// 打印原始矩阵
cout << "原始矩阵:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
// 转置矩阵
for (int i = 0; i < 3; i++) {
for (int j = 0; j < i; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// 打印转置后的矩阵
cout << "转置后的矩阵:" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
```
程序首先定义了一个 3x3 的整数矩阵 `matrix`,并初始化为:
```
1 2 3
4 5 6
7 8 9
```
然后使用双重循环遍历矩阵,将每个元素与对应位置上的元素交换,从而得到转置矩阵。最后再次遍历矩阵,将其输出到屏幕上。
c语言利用指针实现一个3x4的数组的转置
下面是一个示例代码:
```c
#include <stdio.h>
void transpose(int (*arr)[4], int rows) {
int temp;
for (int i = 0; i < rows; i++) {
for (int j = i + 1; j < 4; j++) {
temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
}
int main() {
int arr[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
printf("Original array:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
transpose(arr, 3);
printf("Transposed array:\n");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[j][i]);
}
printf("\n");
}
return 0;
}
```
这里我们定义了一个函数 `transpose`,它接收一个指向二维数组的指针以及数组的行数作为参数。该函数通过交换数组中的元素来实现矩阵的转置。在主函数中,我们定义了一个3行4列的二维数组,并将其输出到屏幕上。然后调用 `transpose` 函数,再次输出转置后的数组。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)