const int N = 5;
int person[] = {2, 3, 12, 5, 19};
int big = 0;
int small = 100;
int i;
for (i = 0; i <= N; i++)
{
cout << "Person[" << i << "] ate: " << person[i] << endl;
//cin >> person[i];
for (int j = i+1; j <= N; j++)
{
if (person[i] < person[j])
{
int tmp = person[i];
person[i] = person[j];
person[j] = tmp;
}
}
if (person[i]>big)
big=person[i];
if (person[i]<small)
small=person[i];
}
cout << "Person[" << i << "] ate the most pancakes: " << big << endl;
cout << "Person[" << i << "] ate the least pancakes: " << small << endl;
cout << "Sorted:" << endl;
for (i = 0; i < N; i++)
{
cout << "Person[" << i << "]: " << person[i] << endl;
}
system("pause");
哪裏弄亂了這些陣列它保持顯示我2但泡沫排序作品。另一個問題是如何從最小值和數組索引中獲取最高值的數組索引?
問題是你要走出界限,而不是'i <= N'和'j <= N'你需要:'i
*如何從最小值獲取數組索引並從最高值獲取數組索引?* - ['std :: minmax_element'](http://en.cppreference.com/w/cpp/algorithm/minmax_element) – chris