ernie.WPI.EDU> cat class11.C // file class11.C, cs2223, D97/98 // // minimum print queuing time problem // 15Mar98 NW, First version // includes #include "headers.h" // prototype void selection(int, int *); // selection sort an array // definition inline int min(int x, int y) { return x < y ? x : y;} // main int main () { int times[] = {3, 1, 2, 7}; // print times for four jobs int min_time = INT_MAX; cout << "The set of all possible print orders is" << endl; cout << "j1 j2 j3 j4 time" << endl; for (int a = 0; a < 4; a++) // loop through jobs { for (int b = 0; b < 4; b++) { if (b == a) continue; // avoid duplicates for (int c = 0; c < 4; c++) { if ((c == a) || (c == b)) continue; // avoid duplicates for(int d = 0; d < 4; d++) { if ((d == a) || (d == b) || (d == c)) continue; // avoid duplicates int total = 4 * times[a] + 3 * times[b] + 2 * times[c] + times[d]; min_time = min(min_time, total); cout << setw(2) << a+1 << setw(3) << b+1 << setw(3) << c+1 << setw(3) << d+1 << " " << total << endl; } // end for(d) } // end for(c) } // end for(b) } // end for(a) cout << endl << "The minimum of the above set is " << min_time << endl << endl; selection(4, times); // sort the job times int total = 4 * times[0] + 3 * times[1] + 2 * times[2] + times[3]; cout << "Total time with jobs printed in increasing time order: " << total << endl; return 0; } // end main() void selection(int n, int *array) // selection sort an array { // note: this code is adapted from class 06 int temp; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (array[j] < array[i]) { temp = array[i]; // swap the values array[i] = array[j]; array[j] = temp; } return; } // end selection() // Copyright 1998 Norman Wittelsernie.WPI.EDU> g++ class11.C ernie.WPI.EDU> a.out The set of all possible print orders is j1 j2 j3 j4 time 1 2 3 4 26 1 2 4 3 31 1 3 2 4 27 1 3 4 2 33 1 4 2 3 37 1 4 3 2 38 2 1 3 4 24 2 1 4 3 29 2 3 1 4 23 2 3 4 1 27 2 4 1 3 33 2 4 3 1 32 3 1 2 4 26 3 1 4 2 32 3 2 1 4 24 3 2 4 1 28 3 4 1 2 36 3 4 2 1 34 4 1 2 3 41 4 1 3 2 42 4 2 1 3 39 4 2 3 1 38 4 3 1 2 41 4 3 2 1 39 The minimum of the above set is 23 Total time with jobs printed in increasing time order: 23 ernie.WPI.EDU>