输入一个5行4列的二维数组,将数组里所有元素按从小到大顺序排列后输出。

#include"stdio.h"
void bubble(float a[]){
int i,j;
float temp;
for(i=0;i<19;i++)
{for(j=0;j<19-i;j++)
if(a[j]>a[j+1])
{temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
void main()
{float a[5][4],b[20],(*p)[4];
int i,j,m,n,t,h=0;
p=a;
for(i=0;i<5;i++){
for(j=0;j<4;j++)
scanf("%f",&a[i][j]);
for(m=0;m<5;m++){
for(n=0;n<4;n++)
b[h]=*(*(p+m)+n);
h++;
}
bubble(b);
for(t=0;t<20;t++)
printf("%f",b[t]);
}
}

编译通过,但运行有误,请指点

给你简化了一些。

void bubble(float *a){int i,j;float temp;
for(i=0;i<19;i++){
for(j=i+1;j<20;j++)if(a[i]>a[j])
{temp=a[j];a[j]=a[i]; a[i]=temp;}}}

void main(){float a[5][4],b[20],(*p)[4];
int i,j,m,n,t,h=0; p=a;
for(i=0;i<5;i++)for(j=0;j<4;j++) scanf("%f",&a[i][j]);

bubble(&a[0][0]);
for(i=0;i<5;i++)for(j=0;j<4;j++) printf("%f ",a[i][j]);
}追问

(b[20]多余了吧)

温馨提示:内容为网友见解,仅供参考
第1个回答  2012-12-12
#include"stdio.h"
void bubble(float a[]){
int i,j;
float temp;
for(i=0;i<20;i++)
{for(j=0;j<19-i;j++)
if(a[j]>a[j+1])
{temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
void main()
{float a[5][4],b[20],(*p)[4];
int i,j,m,n,t,h=0;
p=a;
for(i=0;i<5;i++){
for(j=0;j<4;j++)
scanf("%f",&a[i][j]);
}
for(m=0;m<5;m++){
for(n=0;n<4;n++)
{
b[h]=*(*(p+m)+n);
h++;
}
}
bubble(b);
for(t=0;t<20;t++)
printf("%g ",b[t]);
}
第2个回答  2012-12-12
选择排序中第二个for语句中j初值错误,下次写仔细点就好了追问

这不是选择排序

相似回答