#include "stdstuff.h" 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, TERMINATOR = -1; int data [MAXVALUES], count, value, i; cout << "\nEnter up to " << MAXVALUES << " values followed by " << TERMINATOR << ": "; count = 0; for (;;) { cin >> value; if (value == TERMINATOR) { // the input process is complete break; } if (count == MAXVALUES) { // the array is full cout << "\nToo many values entered.\n"; pause(); return 0; } data[count++] = value; } sort(count,data); if (count == 0) { cout << "\nNo values were entered.\n"; } else { cout << "\nThe values in order are:\n"; writeArray(count,data); } pause(); return 0; }