/* testsort2.cpp CIS 150 David Klick 6/24/08 Demonstrates sorting parallel arrays. */ #include #include using std::cin; using std::cout; void cont(); int main() { int i, j, itmp; double dtmp; //set up parallel arrays int empid[10] = { 5, 92, 16, 3, 2, 7, 42, 99, 23, 75 }; double rate[10] = { 12.50, 12.75, 13.50, 15.00, 12.95, 15.50, 19.00, 17.50, 12.00, 12.25 }; // display arrays printf("Unsorted:\n"); for (i=0; i<10; i++) { printf("%2d: %6.2f\n", empid[i], rate[i]); } // sort arrays based on employee ID# for (i=0; i<10-1; i++) { for (j=i+1; j<10; j++) { if (empid[i] > empid[j]) { // swap empid elements itmp = empid[i]; empid[i] = empid[j]; empid[j] = itmp; // swap rate elements dtmp = rate[i]; rate[i] = rate[j]; rate[j] = dtmp; } } } // display arrays printf("\nSorted:\n"); for (i=0; i<10; i++) { printf("%2d: %6.2f\n", empid[i], rate[i]); } printf("\n"); cont(); return 0; } /* void cont(); Arguments: none Returns: nothing Clears input buffer and pauses waiting for user to press Enter. */ void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); } /* Sample output: Unsorted: 5: 12.50 92: 12.75 16: 13.50 3: 15.00 2: 12.95 7: 15.50 42: 19.00 99: 17.50 23: 12.00 75: 12.25 Sorted: 2: 12.95 3: 15.00 5: 12.50 7: 15.50 16: 13.50 23: 12.00 42: 19.00 75: 12.25 92: 12.75 99: 17.50 */