// Shell Program - Template for Program 2 #include // I/O functions #include // I/O formatting functions #include // string class #include // abs(), rand(), atof(), etc. #include // exp(), pow(), trig functions, floor/ceiling, etc. using namespace std; void BubbleSort(int qAns[], int n); int main() { int quizAnswers[6] = {100, 92, 80, 73, 70, 62}; int i; cout << "The original array contains the following values:\n\n" ; for(i=0; i<5; i++) cout << quizAnswers[i] << ", " ; cout << quizAnswers[5] << endl << endl; BubbleSort(quizAnswers, 6); cout << "The sorted array contains the following values:\n\n" ; for(i=0; i<5; i++) cout << quizAnswers[i] << ", " ; cout << quizAnswers[5] << "." << endl << endl; return 0; } // ****************************************************************************** void BubbleSort(/* inout */ int qAns[], // array to be sorted /* in */ int n) // number of answers in array // This function sorts the data in array qAns. // Preconditions: the first n elements in qAns have values and n >= 0 // Postcondition: the array elements will be arranged in ascending order: // qAns[0] <= qAns[1] <= ... <= qAns[n-1] // ****************************************************************************** { int i, j, pass, // number of current pass through the array temp; // temporary variable used to swap unsorted pairs of values bool sorted; // flag: set to false when it is known the array is unsorted pass = 1; sorted = false; // array is not yet known to be sorted while (!sorted) { sorted = true; // assume array is sorted until an out-of-order pair is found // make a pass through the array for (i = 0; i < n-1; i++) { if (qAns[i] > qAns[i+1]) // exchange values if out-of-order { temp = qAns[i]; qAns[i] = qAns[i+1]; qAns[i+1] = temp; sorted = false; // the array was not sorted } } cout << "At the end of pass " << pass ; cout << " the array contains the following values:" << endl; for(j=0; j