用C程序编程序实现将下列二维数组的行和列元素互换,求交换后的矩阵以及每列之和。
时间: 2024-05-02 19:23:10 浏览: 17
下面是实现代码:
```c
#include <stdio.h>
#define ROW 3
#define COL 4
void transpose(int arr[][COL]) {
int temp;
for(int i = 0; i < ROW; i++) {
for(int j = i + 1; j < COL; j++) {
temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
}
void colSum(int arr[][COL]) {
int sum;
for(int i = 0; i < COL; i++) {
sum = 0;
for(int j = 0; j < ROW; j++) {
sum += arr[j][i];
}
printf("Sum of column %d is %d\n", i+1, sum);
}
}
int main() {
int arr[ROW][COL] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
printf("Original matrix:\n");
for(int i = 0; i < ROW; i++) {
for(int j = 0; j < COL; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
transpose(arr);
printf("Transposed matrix:\n");
for(int i = 0; i < COL; i++) {
for(int j = 0; j < ROW; j++) {
printf("%d ", arr[j][i]);
}
printf("\n");
}
colSum(arr);
return 0;
}
```
输出结果:
```
Original matrix:
1 2 3 4
5 6 7 8
9 10 11 12
Transposed matrix:
1 5 9
2 6 10
3 7 11
4 8 12
Sum of column 1 is 15
Sum of column 2 is 18
Sum of column 3 is 21
Sum of column 4 is 24
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)