Question: Modify the selection sort function presented in chapter 9 so it sorts an array of C++ strings (std::string) into dictionary order. It should take two
Modify the selection sort function presented in chapter 9 so it sorts an array of C++ strings (std::string) into dictionary order. It should take two parameters, an array of strings, and the size of the array. You can compare strings using '<' or '>', however those comparisons are case sensitive - "Zeppelin" would come before "zebra" because in ASCII capital letters have lower numbers than lower-case letters. Your sort, however, should be case-insensitive, so that "zebra" would come before "Zeppelin". The case-insensitivity should be for the whole string, not just the first letter. Hint: you can make your own string comparison function that uses toupper() together with the built-in string comparison ('<' or '>'), but don't change the original strings. Your function must be named stringSort.
The file must be called stringSort.cpp
stringSort.cpp - must be modified per above and utilize unmodified main function
#include
// Function prototypes void stringSort(int [], int); void showArray(const int [], int);
int main() //main function cannot be modified { using std::string; string array[] = {"Zeppelin", "zebra"}; stringSort(array, 2); return 0; }
void stringSort(int array[], int size) { int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++) { minIndex = startScan; minValue = array[startScan]; for(int index = startScan + 1; index < size; index++) { if (array[index] < minValue) { minValue = array[index]; minIndex = index; } } array[minIndex] = array[startScan]; array[startScan] = minValue; } } void showArray(const int array[], int size) { for (int count = 0; count < size; count++) cout << array[count] << " "; cout << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
