Sorting a string array based off of the indexes of a sorted int array?

Hi, I'm working with dynamic arrays and I'm trying to sort a string array based off of the indexes of an int array that has been sorted in descending order.
I'm trying to figure out how to display the names associated with the corresponding scores. The scores are sorted properly but the names aren't in the correct order. Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void initializeArrays(/*in*/string names[], /*in*/int scores[], /*in*/int size)
{
	for (int count = 0; count < size; count++)
	{
		cout << "Enter the name for score #" << (count + 1) << ": ";
		getline(cin >> std::ws, names[count]);
		cout << "Enter the score for score #" << (count + 1) << ": ";
		cin >> scores[count];
	}
	cout << endl;
}

void sortData(/*in*/string names[], /*in*/int scores[], /*in*/int size)
{
	int startScan, minIndex, minValue, temp;

	for (startScan = size - 1; startScan > 0; startScan--)
	{
		minIndex = 0;
		minValue = scores[0];
		for (int index = 1; index <= startScan; index++)
		{
			if (scores[index] < scores[minIndex])
			{
				minIndex = index;
			}
		}
		temp = scores[minIndex];
		scores[minIndex] = scores[startScan];
		scores[startScan] = temp;
	}
}


void displayData(/*in*/const string names[], /*in*/const int scores[], /*in*/int size)
{
	cout << "Top Scorers:" << endl;
	for (int count = 0; count < size; count++)
	{
		cout << names[count] << ": " << scores[count] << endl;
	}
}


when my code is displayed, it just lists the names in the entered order, but I want to be able to sort my names so that they match scores associated with them.
Sample output:
(notice the names do not correspond with their scores in the top scorers section)
How many scores will you enter?: 4
Enter the name for score #1: Jack
Enter the score for score #1: 40
Enter the name for score #2: John
Enter the score for score #2: 35
Enter the name for score #3: Rick
Enter the score for score #3: 37
Enter the name for score #4: Bobby
Enter the score for score #4: 23

Top Scorers:
Jack: 40
John: 37
Rick: 35
Bobby: 23
Press any key to continue . . .
Last edited on
then enter the scores at the entered order of them name.
Otherwise learn how to use two-dimensional arrays (I hate them)
then enter the scores at the entered order of them name.
Otherwise learn how to use two-dimensional arrays (I hate them)

I need to sort them, not just enter them in the correct order with the scores going from greatest to least.
Last edited on
When you're swapping elements in the score array, also swap the elements in the names array.
When you're swapping elements in the score array, also swap the elements in the names array.

But there's issues because the swap is based off of integers, not strings. I'm wondering if there's a way to equate the elements of my score array with my names array after swapping?

I tried adding this,
1
2
3
4
temp = scores[minIndex];
scores[minIndex] = scores[startScan];
names[minIndex] = names[startScan]; //added this and it made it worse
scores[startScan] = temp;
Last edited on
But there's issues because the swap is based off of integers, not strings.

That is a non-issue.


I tried adding this,

That isn't swapping elements. That's assigning one element to another.

1
2
3
4
5
6
7
        temp = scores[minIndex];
        scores[minIndex] = scores[startScan];
        scores[startScan] = temp;

        string tmp = names[minIndex];
        names[minIndex] = names[startScan];
        names[startScan] = tmp;


Of, if you used std::swap:

1
2
        std::swap(scores[minIndex], scores[startScan]);
        std::swap(names[minIndex], names[startScan]);


Thank you so much cire! That is what I needed! You got it to work for me!

Man I feel stupid, much appreciated sir!
Topic archived. No new replies allowed.