/* ---------------------------------------*/ /* This program sorts a list of 6 integers */ /* into ascending order. */ #include //Required for cin, cout. #include //Required for setw(). using namespace std; void sort(int x[ ], int n); int main( ) { //Declare variables. int x[6]; // The array. int i; // The loop index. //input 6 integers into the array x cout << "Please enter 6 integers:" << endl; for(i=0; i<6; i++) { cin >> x[i]; } //output the list with the original order cout << "The list in the original order:" << endl; for(i=0; i<6; i++) { cout << x[i] << " "; } cout << endl; //sort the array in ascending order sort(x, 6); //output the list in ascending order cout << "The list in ascending order:" << endl; for(i=0; i<6; i++) { cout << x[i] << " "; } cout << endl; return 0; } /* ------------------------------------------- */ /* This function sorts an array with n elements */ /* into ascending order */ void sort(int x[ ], int n) { // Declare variables. int m; int hold; //Implement selection sort algorithm. for (int k=0; k<=n-2; k++) { //Find position of smallest value in array //beginning at k m=k; for (int j=k+1; j<=n-1; j++) { if (x[j] < x[m]) m = j; } // Exchange smallest value with value at k hold = x[m]; x[m] = x[k]; x[k] = hold; } return; }