#include
using namespace std;
int main()
{
int a[10],i,j, t;
cout << "input 10 numbers:" << endl;
for (i = 0; i < 10; i++)
cin >> a[i];
cout << endl;
for (j = 0; j < 9;j++)
for (i = 0; i < 10-j;i++)
if( a[i] > a[i + 1]) {
t = a[i]; a[i] = a[i + 1]; a[i + 1] = t;
}
cout <<"the sorted numbers :" << endl;
for (i = 0; i < 10; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}
你的第二個for這裡for (i = 0; i < 10-j;i++)
判斷條件應該改為i < 9 - i
不然你i=0的時候下面的if會出現a[9]和a[10]比較,顯然報錯了