#include "stdstuff.h" bool readArray (int arraySize, int array[], int &valuesRead) { const int TERMINATOR = -1; int value; cout << "Please enter a list of values followed by " << TERMINATOR << ": "; valuesRead = 0; // nothing so far for (;;) { cin >> value; if (value == TERMINATOR) { return true; // read operation successfully completed } if (valuesRead == arraySize) { return false; // the array is full – the user entered too many values } array[valuesRead++] = value; } // no return required as we can never get here } void sort (int n, int data[]) { int stop, i, temp; bool swapOccurred; stop = n - 1; // on the first pass "i" must be < "n - 1" do { // make a pass swapOccurred = false; // no interchanges so far on this pass for (i = 0; i < stop; i++) { if (data[i] > data[i + 1]) { // if elements are out of order // interchange elements temp = data[i]; data[i] = data[i + 1]; data[i + 1] = temp; swapOccurred = true; } } stop--; // on the next pass, stop one comparison sooner } while (swapOccurred); } void writeArray (int n, int array[]) { int i; for (i = 0; i < n; i++) { cout << array[i] << endl; } } int main () { const int MAXVALUES = 100; int values[MAXVALUES], count; if (!readArray(MAXVALUES, values, count)) { cout << "Too many values were entered.\n"; pause(); return 0; } sort (count, values); cout << "The values in order are:\n"; writeArray (count, values); pause(); return 0; }