C语言 二维数组逆置
时间: 2024-01-12 09:22:42 浏览: 134
以下是C语言中二维数组逆置的示例代码:
```c
#include <stdio.h>
void transpose(int A[][3], int row, int col) {
int res[col][row];
for (int i = 0; i < col; ++i) {
for (int j = 0; j < row; ++j) {
res[i][j] = A[j][i];
}
}
printf("逆置后的二维数组:\n");
for (int i = 0; i < col; ++i) {
for (int j = 0; j < row; ++j) {
printf("%d ", res[i][j]);
}
printf("\n");
}
}
int main() {
int A[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int row = 3;
int col = 3;
printf("原始二维数组:\n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
printf("%d ", A[i][j]);
}
printf("\n");
}
transpose(A, row, col);
return 0;
}
```
输出结果:
```
原始二维数组:
1 2 3
4 5 6
7 8 9
逆置后的二维数组:
1 4 7
2 5 8
3 6 9
```
阅读全文