Sorting my second array?

Hello everyone,

I have a program that reads from a text file and into two arrays, one array has data like: CD232323; the other array has data like: Freshman, senior, etc.
Within the selectSort function I am also calling my second array that has stored in it, the string value of the class level. For example:
Credits Completed / Class Level
Under 32 Freshman
32 – 63 Sophomore
64 - 95 Junior
Over 95 Senior

The following text file has:
DD444444 U 52
CD334455 U 100

DD444444 has 52 completed credits so class level is Sophomore
Also the ordinal value of FRESHMAN will be 0, and the ordinal value of SENIOR will be 3. So in my classLevel[] array I have the string value stored in it; ie, Senior, etc.

Problem:
When I use this same function to sort classLevel[], it sorts it in this manner:
Sophomore, Senior, Junior, and Freshman. Not in the order of the text file. For example, if CD334455 was in array[1] than the corresponding 100 or Senior should aslo be in array[1].

Here is some of my code, I hope this helps. Thank you:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  //*************************************************************************
string readCreditFile(ifstream& inCreditData, string idNum[], string classLevel[], int& cnt)
{
	string studentId;					// stored value for the students ID number
	char ch;							// stored value for the students status
	int classesTaken;					// number of credits completed by student
	studentLevel num;					// the numerical value of the function studentClass;
	string title;						// stored value from the function studentClass
	bool arrayFull = false;			    // check to see if there is still space in the array idNum[]

	while(inCreditData >> studentId >> ch >> classesTaken && cnt <= MAX_ARRAY)
	{
		if (ch == 'U' && !arrayFull)
		{
			idNum[cnt] = studentId;				// store student ID into idNum array
			num = studentNum(classesTaken);	    // function to convert class taken to ordinal value
			title = studentTitle(num);			// function to convert ordinal value to string
			classLevel[cnt] = title;			// store string value of class level into array
			cnt++;
			if (cnt >= MAX_ARRAY)				// test to see if array is full
			{
				arrayFull = true;
				cout << "Error: only the first " << MAX_ARRAY
					<< " students will be read." << endl << endl;
			}
		} // end inner if

	} // end while

	inCreditData.close();

	return title;
}
//*************************************************************************
studentLevel studentNum(int numCredits)
{
	studentLevel title;

	if (numCredits < 32)
		title = FRESHMAN;
	else if (numCredits >= 32 && numCredits <= 63)
		title = SOPHOMORE;
	else if (numCredits >= 64 && numCredits <= 95)
		title = JUNIOR;
	else
		title = SENIOR;

	return title;
}
//***************************************************************************
string studentTitle(studentLevel level)
{
	string name;
	switch (level)
	{
	case 0:
		name = "Freshman";
		break;
	case 1:
		name = "Sophomore";
		break;
	case 2:
		name = "Junior";
		break;
	case 3:
		name = "Senior";
	}

	return name;
}
//***************************************************************************
void selectSort(string array[], int& cnt)
{
	int curTop,          // Current top of unsorted list
		next,            // Position to compare value to
		max;       	     // Position of greatest value
	string temp;         // Temp value for swapping

	// for each item in the list (top to bottom)
	for (curTop = 0; curTop < cnt - 1; curTop++)
	{
		max = curTop;      // start with current top as greatest value

		// find greatest value from curTop down
		for (next = curTop + 1; next < cnt; next++)
			if (array[next] > array[max])
			{
				max = next;
				array[max] = array[next];

			}

		//if greatest not at curTop, swap with curTop
		if (max != curTop)
		{
			temp = array[curTop];
			array[curTop] = array[max];
			array[max] = temp;
		}   // end swap
	}
	return;
}
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
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>

// return the position of the largest element. invariant: n > 0
std::size_t pos_largest( const std::string array[], std::size_t n )
{
    std::size_t pos_largest = 0 ;
    for( std::size_t i = 1 ; i < n ; ++i )
        if( array[pos_largest] < array[i] ) pos_largest = i ;
    return pos_largest ;
}

void do_swap( std::string& a, std::string& b ) // simple swap, not using move
{
    std::string temp = a ;
    a = b ;
    b = temp ;
}

// for both arrays, bring the element at position 'pos' to the front
void bring_to_front( std::string a[], std::string b[], std::size_t pos )
{
    do_swap( a[0], a[pos] ) ;
    do_swap( b[0], b[pos] ) ;
}

// sort descending on key, keeping value aligned with associated key
void sort_pairs( std::string key[], std::string value[], std::size_t n )
{
    if( n > 1 )
    {
        // get the largest value to the front of both sequences
        bring_to_front( key, value, pos_largest( key, n ) ) ;

        // repeat for the tail of (the remaining elements in) the sequences
        sort_pairs( key+1, value+1, n-1 ) ;
    }
}

int main()
{
    const std::size_t n = 5 ;
    std::string a[n] = { "2",   "0",    "4",    "1",   "3" } ;
    std::string b[n] = { "two", "zero", "four", "one", "three" } ;

    sort_pairs( a, b, n ) ; // sort on a, keeping b aligned with a
    for( std::size_t i = 0 ; i < n ; ++i ) std::cout << a[i] << ' ' << b[i] << '\n' ;
    std::cout << "--------------\n" ;

    sort_pairs( b, a, n ) ; // sort on b, keeping a aligned with b
    for( std::size_t i = 0 ; i < n ; ++i ) std::cout << b[i] << ' ' << a[i] << '\n' ;
}

http://coliru.stacked-crooked.com/a/4a6318af234cc643
Registered users can post here. Sign in or register to post.