我正在使用 3 个并行数组并使用选择排序,即按英里降序排序。它确实按降序排序,但是例如当我在输入文件中输入数据时,如下所示:
- 北卡罗来纳州 33.9 5
- 弗吉尼亚州 22.2 0
- 等等
然后程序将打印两次'VA'数据而不显示'NC'数据。并行数组不是我的想法,选择排序也不是。它们是必需的。请帮忙
void sortEntries( string initials[], double miles[], int shelters[] )
{
int top, bottom, i, n;;
string tempInitials;
double tempMiles;
int tempShelters;
//Selection sorting for initials[]
n = 14;
for( top = 0; top < MAX_ENTRIES - 1; top++ )
{
tempInitials = initials[smallest];
tempMiles = miles[smallest];
tempShelters = shelters[smalles];
smallest = top;
//Locate smallest number between 1 and MAX_ENTRIES(14)
for ( i = top; i < MAX_ENTRIES; i++ )
{
if ( initials[i] < tempMiles )
{
tempInitials = initials[i];
tempMiles = miles[i];
tempShelters = shelters[i];
smallest = i;
}
}
initials[top] = initials[smallest];
miles[top] = miles[smallest];
shelters[top] = shelters[smallest];
initials[smallest] = tempInitials;
miles[smallest] = tempMiles;
shelters[smallest] = tempShelters;
}