Тело внешнего l oop должно выглядеть как минимум как
int mindex = i;
//DO6: finish the inner loop
// Hint: it should start from i+1 and go until num-1
// if studentNames[j] is greater than studentNames[mindex]
// then store j in mindex
for (int j = i+1; j < num; j++) {
if (studentNames[mindex] < studentNames[j]) {
mindex = j;
}
}
Это условие
if (studentNames[mindex] < studentNames[j]) {
обеспечивает сортировку в порядке убывания.
Вот демонстрационная программа.
#include <iostream>
#include <string>
#include <utility>
void ProcessSort( int studentIDs[], std::string studentNames[], size_t n )
{
for ( size_t i = 0; i < n; i++)
{
size_t mindex = i;
//DO6: finish the inner loop
// Hint: it should start from i+1 and go until num-1
// if studentNames[j] is greater than studentNames[mindex]
// then store j in mindex
for ( size_t j = i + 1; j < n; j++ )
{
if ( studentNames[mindex] < studentNames[j] ) mindex = j;
}
//DO7: Swap student names
if ( i != mindex )
{
std::swap( studentNames[mindex], studentNames[i] );
//DO8: Swap student IDs'
std::swap( studentIDs[mindex], studentIDs[i] );
}
}
}
int main()
{
int studentIDs[] = { 495, 520, 550, 666 };
std::string studentNames[] = { "Kyle", "Liam", "Bill", "Michael" };
const size_t N = sizeof( studentIDs ) / sizeof( *studentIDs );
for ( size_t i = 0; i < N; i++ )
{
std::cout << "( " << studentNames[i] << ", " << studentIDs[i] << " ) ";
}
std::cout << std::endl;
ProcessSort( studentIDs, studentNames, N );
for ( size_t i = 0; i < N; i++ )
{
std::cout << "( " << studentNames[i] << ", " << studentIDs[i] << " ) ";
}
std::cout << std::endl;
return 0;
}
Ее вывод
( Kyle, 495 ) ( Liam, 520 ) ( Bill, 550 ) ( Michael, 666 )
( Michael, 666 ) ( Liam, 520 ) ( Kyle, 495 ) ( Bill, 550 )