// /*--------------------------------------------------------------------*\ // | Main Program: What can happen with repeated arguments? // |*--------------------------------------------------------------------*| // | Date: 20 June 2003 // | Author: Bruce W. Weide // | // | Brief User's Manual: // | Allows user to test a simple array sorting algorithm. With this // | version of Array_Kernel, not much sorting gets done! But // | Array_Kernel_2 is a correct implementation of its spec. The // | problem is in the body of Sort: repeated arguments to operator< // | and operator&=. For the "fix", see Sort_2_Fixed.cpp. // | // \*--------------------------------------------------------------------*/ ///--------------------------------------------------------------------- /// Global Context ----------------------------------------------------- ///--------------------------------------------------------------------- #include "RESOLVE_Foundation.h" #include "CT/Array/Kernel_2_C.h" ///--------------------------------------------------------------------- /// Interface ---------------------------------------------------------- ///--------------------------------------------------------------------- concrete_instance class Array_Of_Text : instantiates Array_Kernel_2_C {}; //---------------------------------------------------------------------- //---------------------------------------------------------------------- // Global operations global_procedure Sort (alters Array_Of_Text& a); /*! ensures [a (thought of as a string) is a permutation of #a, and its entries are sorted in non-decreasing order] !*/ //---------------------------------------------------------------------- //---------------------------------------------------------------------- // Global operation bodies global_procedure_body Sort (alters Array_Of_Text& a) { object Integer i = a.Lower_Bound (); while (i < a.Upper_Bound ()) { object Integer index_of_min = i, j = i + 1; while (j <= a.Upper_Bound ()) { if (a[j] < a[index_of_min]) { index_of_min = j; } j++; } a[i] &= a[index_of_min]; i++; } } //---------------------------------------------------------------------- //---------------------------------------------------------------------- program_body main () { object Character_IStream input; object Character_OStream output; object Integer n_items, i; object Array_Of_Text a; // Open input and output streams input.Open_External (""); output.Open_External (""); // Get number of items to be sorted input >> n_items; // Set size of array a.Set_Bounds (0, n_items - 1); // Get individual items to be sorted into array while (i < n_items) { input >> a[i]; i++; } // Sort array Sort (a); // Output sorted array output << "Sorted Text values are:\n"; i = 0; while (i < n_items) { output << a[i] << "\n"; i++; } // Close input and output streams input.Close_External (); output.Close_External (); }