优化这段代码 #include <stdio.h>// 输入二维数组void input_array(int m, int n, int arr[m][n]) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { scanf("%d", &arr[i][j]); } }}// 输出二维数组void output_array(int m, int n, int arr[m][n]) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("%d ", arr[i][j]); } printf("\n"); }}// 求最大元素及其所在位置void max_element(int m, int n, int arr[m][n], int *max, int *row, int *col) { *max = arr[0][0]; *row = 0; *col = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (arr[i][j] > *max) { *max = arr[i][j]; *row = i; *col = j; } } }}int main() { int m, n; printf("请输入行数和列数:"); scanf("%d %d", &m, &n); int arr[m][n]; printf("请输入二维数组的所有元素:\n"); input_array(m, n, arr); printf("您输入的二维数组是:\n"); output_array(m, n, arr); int max, row, col; max_element(m, n, arr, &max, &row, &col); printf("最大元素的值为:%d,所在的行号为:%d,所在的列号为:%d\n", max, row, col); return 0;}
时间: 2024-01-31 12:03:39 浏览: 28
这段代码本身并没有太大的问题,但可以进行一些微调和优化。
1.在输入输出时,可以使用指针传递二维数组,避免在函数中使用变长数组。
2.在求最大元素及其所在位置时,可以减少不必要的判断和变量赋值,提高效率。
修改后的代码如下:
```c
#include <stdio.h>
// 输入二维数组
void input_array(int m, int n, int (*arr)[n]) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &arr[i][j]);
}
}
}
// 输出二维数组
void output_array(int m, int n, int (*arr)[n]) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
// 求最大元素及其所在位置
void max_element(int m, int n, int (*arr)[n], int *max, int *row, int *col) {
*max = arr[0][0];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (arr[i][j] > *max) {
*max = arr[i][j];
*row = i;
*col = j;
}
}
}
}
int main() {
int m, n;
printf("请输入行数和列数:");
scanf("%d %d", &m, &n);
int arr[m][n];
printf("请输入二维数组的所有元素:\n");
input_array(m, n, arr);
printf("您输入的二维数组是:\n");
output_array(m, n, arr);
int max, row, col;
max_element(m, n, arr, &max, &row, &col);
printf("最大元素的值为:%d,所在的行号为:%d,所在的列号为:%d\n", max, row, col);
return 0;
}
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)