/* ---------------------------------------*/ /* This program sorts a list of 10 double precision */ /* floating point numbers into descending order. */ #include //Required for cin, cout. #include //Required for setw(). using namespace std; void sort(double x[ ], int n); int main( ) { //Declare variables. double x[10]; // The array. int i; // The loop index. //input 10 double precision numbers into the array x cout << "Please enter 10 double precision numbers:" << endl; for(i=0; i<10; i++) { cin >> x[i]; } //output the list with the original order cout << "The list in the original order:" << endl; for(i=0; i<10; i++) { cout << x[i] << " "; } cout << endl; //sort the array in descending order sort(x, 10); //output the list in descending order cout << "The list in descending order:" << endl; for(i=0; i<10; i++) { cout << x[i] << " "; } cout << endl; return 0; } /* ------------------------------------------- */ /* This function sorts an array with n elements */ /* into ascending order */ void sort(double x[ ], int n) { // Declare variables. int m; double hold; //Implement selection sort algorithm. for (int k=0; k<=n-2; k++) { //Find position of largest 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; }