I used a string-stream to read an input file with 10 lines and stored all the values into separate arrays. ID, first, last, city, state, and GPA. It looked like this.
1 Nathan Humphery Kansas MO 3.35
2 Sara Jonathan LeesSummit MO 3.56
3 Kayla James Liberty KS 3.78
4 Kyle Davis Independence KS 2.98
...
8 Daniel Earla Independence KS 3.77
So the ID array would be { 1, 2, 3, 4, ..., 8}
and the city array would be {Kansas, LeesSummit, Liberty, Independence, ..., Independence}
One of the functions in my program is supposed to print out the information sorted by city.
I used the selection sort algorithm to sort the city array to put them in the correct alphabetical order. But now I'm not sure how to properly sort the other arrays to match the information that was in the city array. Can something be added to the SelectionSort function that would match the changes that happened in the city array to the other arrays? Do I even need to sort every array, or is there an easier way that I'm just missing?
I can't use things such as "#include algorithm" and "sort()" yet, and I'm also using namespace std.
void SelectionSort(string city[], int size)
{
int i;
int j;
int indexSmallest;
string temp;
for (i = 0; i < size; ++i)
{
indexSmallest = i;
for (j = i + 1; j < size; ++j)
{
if (city[j] < city[indexSmallest])
{
indexSmallest = j;
}
}
temp = city[i];
city[i] = city[indexSmallest];
city[indexSmallest] = temp;
}
}
The output should look something like this.
4 Kyle Davis Independence KS 2.98
8 Daniel Earla Independence KS 3.77
1 Nathan Humphery Kansas MO 3.35
2 Sara Jonathan LeesSummit MO 3.56
3 Kayla James Liberty KS 3.78