#include<stdio.h> int main(void) { /*********Begin*********/ int m, n; int a[100][100]; scanf("%d%d", &n, &m); for(int i=0; i<n; i++) for(int j=0; j<m; j++) scanf("%d", &a[i][j]); int max; int t; int pos; for(int i=0; i<n; i++) { max=a[i][0]; for(int j=0; j<m; j++) { if(a[i][j]>a[i][0]) { max=a[i][j]; pos=j; } } t=a[i][pos], a[i][pos]=a[i][0], a[i][0]=t; } for(int i=0; i<n; i++){ for(int j=0; j<m; j++){ printf("%d ", a[i][j]); } printf("\n"); } /*********End**********/ return 0; } 哪里有问题
时间: 2024-03-12 09:49:14 浏览: 26
这段代码是一个二维数组的排序程序,其中可能存在以下问题:
1. 在程序开头缺少头文件声明,应该添加 `#include<stdio.h>`。
2. 在程序中 `int pos;` 没有初始化,应该在 `max=a[i][0];` 之前添加 `pos=0;`。
3. 在排序时,如果第一列的元素不是最大的,程序会将第一列元素与最大值所在的列进行交换,这是不正确的。应该在找到最大值后将其与第一列元素进行交换。
4. 在输出数组时,每行输出完毕后应该添加换行符。
下面是修改后的代码:
```
#include<stdio.h>
int main(void) {
int m, n;
int a[100][100];
scanf("%d%d", &n, &m);
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
scanf("%d", &a[i][j]);
int max, t, pos;
for(int i=0; i<n; i++)
{
max=a[i][0];
pos=0;
for(int j=0; j<m; j++)
{
if(a[i][j]>max)
{
max=a[i][j];
pos=j;
}
}
t=a[i][pos], a[i][pos]=a[i][0], a[i][0]=t;
}
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
```
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![png](https://img-home.csdnimg.cn/images/20210720083516.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)