exchange sort

I have a program that reads a text file then puts the data into parallel arrays, one of student IDs and one of their test scores. I'm having issues with the exchange sort. I am supposted to sort the student IDs from low to high. Here is my code. Sorry about all of the comments. The exchange sort is in void sortHigh.

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;

void header (ofstream &);
void readData (ifstream &, int [], float [], int &);
void printData (ofstream &, int [], float [], int &);
void findLow (ofstream &, int [], float [], int &);
void findHigh (ofstream &, int [], float [], int &);
void findAverage (ofstream &, int [], float [], int &);
void sortLow (ofstream &, int [], float[], int &);
void sortHigh (ofstream &, int [], float[], int &);
void footer (ofstream &);

int main()
{
	int studID[50]; // declare program variables and arrays
	float testScore[50];
	int count = 0;

	ifstream inFile; // open files for input and output
	inFile.open("data.txt");
	ofstream outFile;
	outFile.open("outFile.txt");

	header(outFile); //displays header on outfile

	readData(inFile, studID, testScore, count); // get student IDs and test scores

	outFile << "The original list of test scores is:" << endl << endl;
	
	outFile << "Student ID #" << "     " << "Test Score" << endl;

	printData(outFile, studID, testScore, count); // print out the students' information

	outFile << endl;
	outFile << "The test scores sorted high to low are:" << endl << endl;

	sortLow(outFile, studID, testScore, count);

	printData(outFile, studID, testScore, count); // print out the students' information

	outFile << endl;
	outFile << "The student ID numbers sorted low to high are:" << endl << endl;

	sortHigh(outFile, studID, testScore, count);

	printData(outFile, studID, testScore, count); // print out the students' information

	outFile << endl;

	findLow(outFile, studID, testScore, count); // find the student with the lowest test score

	findHigh(outFile, studID, testScore, count); // find the student with the highest test score

	findAverage(outFile, studID, testScore, count); // find the average of the students' scores

	footer(outFile); // displays footer on outfile
	
system("pause");
return 0;
} //end main function

//***************************** FUNCTION HEADER *****************************
void header (ofstream &outFile)
{
	return;
}
// ************************* END OF FUNCTION HEADER *************************

//*******************************************************************************

void readData (ifstream &inFile, int studID[], float testScore[], int &count)
{
	// GIVEN: input file, empty studID array, empty testScore array
	// TASK: read the students ID and their test score
	// RETURNS: the student ID and test score from the file
	int id;
	inFile >> id;
	while (id > 0)
	{
		studID[count] = id;
		inFile >> testScore[count];
		count++;
		inFile >> id;
	}
return;
}

//*******************************************************************************

void printData (ofstream &outFile, int studID[], float testScore[], int &count)
{
	// GIVEN: output file, empty studID array, empty testScore array
	// TASK: print out the students' IDs and test scores
	// RETURNS: nothing
	int size;
	for (count = 0; count < 50; count++)
	{
		if (studID[count] > 0)
		outFile << setw(8) << studID[count] << setw(17) << setprecision(4) << testScore[count] << endl; // writes to outfile
	}
return;
}

//*******************************************************************************

void sortLow (ofstream &outFile, int studID[], float testScore[], int &count)
{
	// GIVEN: 
	// TASK: 
	// RETURNS: 
	int m;
	int n; 
	int tempScore;
	int tempID;
	for (m = 0; m < 50; m++)
	{
		for (n = 50; n > m; n--)
			if (studID[n] > 0 && testScore[n] > testScore[n-1])
			{
				tempScore = testScore[n];
				tempID = studID[n];
				testScore[n] = testScore[n-1];
				studID[n] = studID[n-1];
				testScore[n-1] = tempScore;
				studID[n-1] = tempID;
			}
	}

return;
}

//*******************************************************************************

void sortHigh (ofstream &outFile, int studID[], float testScore[], int &count)
{
	// GIVEN: 
	// TASK: 
	// RETURNS: 
	int m;
	int n;
	int min;
	int tempScore;
	int tempID;
	for ( m = 0; m < 50; m++)
	{
		min = m;
	for ( n = 50; n > m; n--)
		if (studID[n] > 0 && studID[n] < studID[min])
		{
			min = n;
			tempID = studID[m];
			tempScore = testScore[m];
			studID[m] = studID[min];
			testScore[m] = testScore[min];
			studID[min] = tempID;
			testScore[min] = tempScore;
		}
	}

return;
}

//*******************************************************************************

void findLow (ofstream &outFile, int studID[], float testScore[], int &count)
{
	// GIVEN: empty studID array, empty testScore array
	// TASK: find the student with the lowest test score
	// RETURNS: the student ID with the lowest score
	int min = 0;
	for (count = 0; count < 50; count++)
	{
		if (studID[count] > 0 && testScore[count] < testScore[min])
			min = count;
	}

	outFile << "The lowest test score was " << testScore[min] << "% achieved by student " << studID[min] << endl;
return;
}

//*******************************************************************************

void findHigh (ofstream &outFile, int studID[], float testScore[], int &count)
{
	// GIVEN: empty testScore array
	// TASK: find the student with the highest test score
	// RETURNS: the student ID with the highest score
	int max = 0;
	for (count = 0; count < 50; count++)
	{
		if (testScore[count] > testScore[max])
			max = count;
	}

	outFile << "The highest test score was " << testScore[max] << "% achieved by student " << studID[max] << endl;
return;
}

//*******************************************************************************

void findAverage (ofstream &outFile, int studID[], float testScore[], int &count)
{
	double total = 0;
	double average = 0;
	count = 0;
	do // add to total while the student ID is positive
	{
		total += testScore[count];
		count++; // increments count
	}
	while (studID[count] > 0);

	average = total / count;

	outFile << "The average test score for the group is " << setprecision(4) << average << "%" << endl; // writes to outfile

return;
}

//***************************** FUNCTION FOOTER *****************************
void footer (ofstream &outFile)
{
	outFile << endl;
	outFile << setw(35) << "-------------------------" << endl;
	outFile << setw(35) << "  END OF PROGRAM OUTPUT " << endl;
	outFile << setw(35) << "-------------------------" << endl;
	return;
}
// ************************* END OF FUNCTION FOOTER ************************* 


And here is my output...

The original list of test scores is:

Student ID # Test Score
8243 63.5
4321 77.6
6007 94.7
7502 75
8332 83.3
3374 91.9
2348 60.4

The test scores sorted high to low are:

6007 94
3374 91
8332 83
4321 77
7502 75
8243 63.5
2348 60.4

The student ID numbers sorted low to high are:

4321 77
2348 60
3374 91
7502 75
6007 94
8243 63
8332 83

The lowest test score was 60% achieved by student 2348
The highest test score was 94% achieved by student 6007
The average test score for the group is 77.57%

-------------------------
END OF PROGRAM OUTPUT
-------------------------

It changes somehow from the time I sort from test scores to student IDs, but it's not fully sorting.
I haven't read all your program so if this isn't what you expected as answer just say it.

In C++ is implemented the form to sort thinks. Inside the library #include <algorithm>

For example:

1
2
3
4
5
    int vector[10] = {1,3,56,2,567,-123,3,2,100,123};

    sort(vector, vector + 10);
    for(int i=0;i<10;i++)
        cout << vector[i] << ", ";


This function can be even more useful than just sort as default, you can implement any fuction and pass it as argument, so easyly you can got any type of sort. Even of your clases.
http://cplusplus.com/reference/algorithm/sort/
My professor has asked specifically for an exchange sort. Thank you though. I've had several people tell me that this is the best way to go about it. I appreciate your time. I'm sure I somehow have an error within one of my loops or something silly like that.
Topic archived. No new replies allowed.