/* ************************************************************************** * Program name : 027_Sorting_without_using_array (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/11 - first version * ************************************************************************** * Description : (a) Asking the largest value * * (e) Draw random numbers * * (f) Try again ? * ************************************************************************** */ #include #include float swap(float &Num_A, float &Num_B) // Note (1) { float temp; temp = Num_A; Num_A = Num_B; Num_B = temp; } int main() { // part 1 : declaration float Num_1, Num_2, Num_3, Num_4, Num_5, Num_6; char Again; do { // part 2 : Input six numbers cout << "Please input 6 numbers : "; cin >> Num_1 >> Num_2 >> Num_3 >> Num_4 >> Num_5 >> Num_6; // part 3 : Compare / swap if (Num_1 > Num_2) swap(Num_1,Num_2); // Note (2) if (Num_1 > Num_3) swap(Num_1,Num_3); if (Num_1 > Num_4) swap(Num_1,Num_4); if (Num_1 > Num_5) swap(Num_1,Num_5); if (Num_1 > Num_6) swap(Num_1,Num_6); if (Num_2 > Num_3) swap(Num_2,Num_3); if (Num_2 > Num_4) swap(Num_2,Num_4); if (Num_2 > Num_5) swap(Num_2,Num_5); if (Num_2 > Num_6) swap(Num_2,Num_6); if (Num_3 > Num_4) swap(Num_3,Num_4); if (Num_3 > Num_5) swap(Num_3,Num_5); if (Num_3 > Num_6) swap(Num_3,Num_6); if (Num_4 > Num_5) swap(Num_4,Num_5); if (Num_4 > Num_6) swap(Num_4,Num_6); if (Num_5 > Num_6) swap(Num_5,Num_6); // part 3 : Print the list in sorted order cout << "\nThe above six number in ascending order are : "; cout << "\n\t" << Num_1 << " " << Num_2 << " " << Num_3 << " " << Num_4 << " " << Num_5 << " " << Num_6 << "\n\n"; // part 4 : try other numbers ? cout << "\aTry another set of numbers (Y/N) : "; cin >> Again; cout << "\n"; } while (Again=='Y' || Again=='y'); cout << "\n" << endl; system("PAUSE"); return 0; } /* Notes (1) When an argument is passed call-by-value (formal value parameter), a copy of the argument's value is made and passed to the called function. Changes to the copy do not affect the original variable's value in the caller. Another type of argument is called call-by-reference (formal variable parameter), the caller gives the called function the ability to access the caller's data. An ampersand(&) is placed in front of the reference parameters to indicate that the original variable can be modified directly by the called function. Remark : C++ -> float swap(float &Num_A, float &Num_B); PASCAL -> PROCEDURE swap (VAR Num_A, Num_B :real); (2) Sort the list with the way same as "bubble sort". */