// Source File: ExamResults.C // // Member functions for ExamResults class // // Author: Alan Baumgarten // Date: March 12, 1999 #include #include #include #include "ExamResults.H" ExamResults::ExamResults (int count, int values[]) { assert (count >= 2); _numStudents = count; _scores = new int [ _numStudents ]; for (int i = 0; i < _numStudents; ++i) { _scores[i] = values[i]; } _sorted = false; } ExamResults::ExamResults (const ExamResults& r) { _numStudents = r._numStudents; _scores = new int [ _numStudents ]; for (int i = 0; i < _numStudents; ++i) { _scores[i] = r._scores[i]; } _sorted = r._sorted; } ExamResults::~ExamResults() { delete [] _scores; } ExamResults& ExamResults::operator = (const ExamResults& r) { if (&r == this) { // check for self-assignment return *this; } if (_numStudents != r._numStudents) { // re-allocate array if delete [] _scores; // result sets are of _numStudents = r._numStudents; // different sizes _scores = new int [ _numStudents ]; } for (int i = 0; i < _numStudents; ++i) { _scores[i] = r._scores[i]; } _sorted = r._sorted; return *this; } double ExamResults::GetMean() { int total = 0; for (int i = 0; i < _numStudents; ++i) { total += _scores[i]; } return ((double) total / (double) _numStudents ); } int ExamResults::GetMedian() { if (!_sorted) { Sort(); } return ( _scores[ _numStudents / 2 ] ); } int ExamResults::GetMaxScore() { if (!_sorted) { Sort(); } return ( _scores[0] ); } int ExamResults::GetMinScore() { if (!_sorted) { Sort(); } return ( _scores[ _numStudents - 1 ] ); } void ExamResults::Display() { if (!_sorted) { Sort(); } for (int i = 0; i < _numStudents; ++i) { cout << _scores[i] << " "; } cout << endl; } void ExamResults::Sort() { int* tempArray = new int [ _numStudents ]; // temporary workspace // for merge sort MergeSort (tempArray, 0, _numStudents - 1); delete [] tempArray; _sorted = true; } void ExamResults::MergeSort (int workSpace[], int low, int high) { if (low == high) { return; // no need to sort } int mid = (low + high) / 2; // find midpoint MergeSort (workSpace, low, mid); // sort "lower" half MergeSort (workSpace, mid + 1, high); // sort "upper" half Merge (workSpace, low, mid + 1, high); // merge the two halves } void ExamResults::Merge (int workSpace[], int lowIndex, int highIndex, int upperBound) { int j = 0; // workspace index int lowerBound = lowIndex; int mid = highIndex - 1; int n = upperBound - lowerBound + 1; // number of items // Compare elements in the two halves, always copying the larger // element into the workspace array (descending order) while (lowIndex <= mid && highIndex <= upperBound) { if ( _scores [lowIndex] > _scores [highIndex] ) { workSpace [j++] = _scores [lowIndex++]; } else { workSpace [j++] = _scores [highIndex++]; } } // Copy remaining elements in the lower half, if any while (lowIndex <= mid) { workSpace [j++] = _scores [lowIndex++]; } // Copy remaining elements in the upper half, if any while (highIndex <= upperBound) { workSpace [j++] = _scores [highIndex++]; } // Now copy from the workspace back to the actual array of scores for (j = 0; j < n; j++) { _scores [ lowerBound + j ] = workSpace [j]; } }