编写程序,找出一个4乘4二维数组中找到最大元素所在的位置,将其所在行与列互换c语言
时间: 2024-02-01 15:13:51 浏览: 74
以下是一个示例代码,它可以找到一个4x4的二维数组中最大元素的位置,并将其所在行和列互换:
```c
#include <stdio.h>
int main() {
int arr[4][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
int max = arr[0][0];
int max_row = 0, max_col = 0;
// 找到最大元素的位置
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
max_row = i;
max_col = j;
}
}
}
printf("Max element is %d, at row %d, column %d\n", max, max_row, max_col);
// 交换行和列
for (int i = 0; i < 4; i++) {
int temp = arr[i][max_col];
arr[i][max_col] = arr[max_row][i];
arr[max_row][i] = temp;
}
// 打印交换后的数组
printf("Array after swapping:\n");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
```
输出结果为:
```
Max element is 16, at row 3, column 3
Array after swapping:
1 2 3 16
5 6 7 12
9 10 11 8
4 14 15 13
```
阅读全文