/* testsort2.cpp CIS 150 David Klick 6/28/05 Sorting parallel arrays. */ #include int main(void) { 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]); } return 0; } /* 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 */