void sortLow (ofstream &outFile, int studID[], float testScore[], int &count)
{
// GIVEN: output file, studID array, testScore array
// TASK: sort student IDs from low to high
// RETURNS: test scores with matching student IDs from low to high
int i = 0;
int j = 0;
float tempScore;
int tempID;
while( i < count-1 )
{
j = i + 1;
while( j < count )
{
if( studID[i] > studID[j] )
{
tempID = studID[i];
tempScore = testScore[i];
studID[i] = studID[j];
testScore[i] = testScore[j];
studID[j] = tempID;
testScore[j] = tempScore;
}
j++;
}
i++;
}
return;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void findLow (ofstream &outFile, int studID[], float testScore[], int &count)
{
// GIVEN: studID array, 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;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void findAverage (ofstream &outFile, int studID[], float testScore[], int &count)
{
// GIVEN: studID array, testScore array
// TASK: find the average test score for the group
// RETURNS: the average test score (sum of the test scores divided by the number of test scores)
double total = 0;
double average = 0;
do // add to total while the student ID is positive
{
total += testScore[count];
count++; // increments count
} while (studID[count] > 0 && studID[count] != -999);
average = total / count;
outFile << "The average test score for the group is " << fixed << setprecision(1) << average << "%" << endl; // writes to outfile
return;
}
Here is part of my output...
The lowest test score was -107374176.0% achieved by student -858993460
The average test score for the group is -19362556.3%
I guess I'm just not sure where in the sortLow function it changes my array. If I take the sortLow function out my average and lowest test score works, but if I keep the sortLow function the sorting works, but messes up the other parts.