/*排序*/
#include <iostream.h>
void fun(int *p);
void main()
{int a[20]={0};
int i;
cout<<"Please enter 20 numbers:"<<endl;
for(i=0;i<=19;i++)
cin>>a[i];
cout<<"The numbers you have entered are : "<<endl;
for(i=0;i<=19;i++)
cout<<a[i]<<endl;
fun(a);
cout<<"The munmbers are :"<<endl;
for(i=0;i<=19;i++)
cout<<a[i]<<endl;
}
void fun(int *P)
{int i,j,temp;
for(i=1;i<=19;i++)
{temp=p[i];
for(j=0;j<i;j++)
{if(p[i]<p[j]) {do {p[i]=p[i-1];i=i-1;}
while((i-1)>=j);
p[j]=temp;
break;}
}
}
}
參考答案:#include <iostream.h>
void fun(int *p);
void main()
{int a[20];
int i;
cout<<"Please enter 20 numbers:"<<endl;
for(i=0;i<=19;i++)
cin>>a[i];
cout<<"The numbers you have entered are : "<<endl;
for(i=0;i<=19;i++)
cout<<a[i]<<endl;
fun(a);
cout<<"The numbers are :"<<endl;
for(i=0;i<=19;i++)
cout<<a[i]<<endl;
}
void fun(int *P)
{int i,j,temp;
for(i=1;i<20;i++)
{
for (j=19;j>=i;j--)
{
if(P[j]<P[j-1])
{
temp = P[j-1];
P[j-1] = P[j];
P[j] = temp;
}
}
}
}
我用起泡法排序
你的错误么
1 void fun(int *P),temp=p[i]; 变量名不匹配,将所有p改成P或者P改成p;