Bubble Sort - Put in Descending Order

Got a question for you guys, so my program reads a file type which looks like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Michelle               71
Marcie                 99
David                  42
Rebecca                83
Jonathan               79
Matthew                77
Rose                    7
Melanie                75
Kimberly               73
Roger                  74
Scott                  76
Bradley                77
Drextell               10
Heidi                  70
Alan                   68
Pearl                  13
Jeanne                 43
Heber                  55


Here is whats in the header of the class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class StudentStat
{
private:
	int Size;
	string Names[20];
	int Scores[20];
	double Avg, StDev, Median;
	void Sortem();
	void Calc_Avg();
	void Calc_StDev();
	void Calc_Median();
	StudentStat();       // hide the default constructor from user
public:
	StudentStat(char *); // Argument is input filename
	string getName(int); // return the name at position on the list
	int getScore(int); // return score at position on the list
	double getStatistic(string); // retrieve "Avg", "StD", "Med", "High", "Low", "Range"
};


Here is the function that reads the file and stores it into the Names or Scores array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
StudentStat::StudentStat(char * fname)
{
	Avg = StDev = Median = -1.0;  // Initialize values before reading
	Size = 0;
    // TODO: READ DATA FROM INPUT FILE
    ifstream file(fname);
    if(!file)
    {
        cout << "Couldn't open file!" << endl;
    }
    while(file >> Names[Size] >> Scores[Size]) // goes through file sotring strings in the Names and ints in the Scores
    {
        ++Size;
    }
    file.close();
    
    // Sort the names/scores in descending order
	Sortem();
}


code where the bubble sort will need to go

1
2
3
4
5
void StudentStat::Sortem()
{
   // TODO: ADD BUBBLE SORT
    
}


So as of right now the names are stored in a Names string array and the scores are saved in a Score int array. So my question is where do I begin with my bubble sort? I need it to put the scores in descending order so from greatest score to lowest but I need the Names in the string array to still be connected to the number from the list. Does that make sense? If anyone could help or if theres any questions that I need to explain in more depth let me know. Never done a bubble sort before so not sure where to begin. Thanks in advanced.
Last edited on
Example
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
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;

int main()
{
	const int SIZE = 5;
	string name[SIZE] = { "Michelle", "Marcie", "David", "Rebecca", "Jonathan" };
	int score[SIZE] = { 71, 99, 42, 83, 79 };

	for (int row = 0; row < SIZE; row++)
	{
		for (int col = 0; col < SIZE - 1; col++)
		{
			if (score[col] < score[col + 1])
			{
				swap(name[col], name[col + 1]);
				swap(score[col], score[col + 1]);
			}
		}
	}

	for (int i = 0; i < SIZE; i++)
		cout << setw(20) << left << name[i] << setw(20) << left << score[i] << endl;


	cin.ignore();
	return 0;
}
Topic archived. No new replies allowed.