Project 6: Sort Templates

Making InsertionSort generic

Revision dated 10/01/18

Educational Objectives: After completing this assignment the student should have the following knowledge, ability, and skills:

Operational Objectives: Implement and test the function templates InsertionSort and Display. Build applications and test programs from previous projects.

Deliverables: Files:

tsort.h          # contains function templates 
cstringcomp.h    # from project 1 - will be re-tested
cstringcomp.cpp  # from project 1 - will be re-tested
id.h             # from project 2, with input operator and less-than operator added
id.cpp           # implements id.h
log.txt          # as usual: work log and testing diary

(We supply a makefile for this project.)

Assessment Rubric

builds:                                    [0..5]:   x
test1: [intsort]                           [0..5]:   x
test2: [stringsort]                        [0..5]:   x
test3: [charsort]                          [0..5]:   x
test4: [idsort]                           [0..10]:  xx
test5: [fcstringcomp]                      [0..5]:   x
test6: [fcstringcomp]                      [0..5]:   x
log.txt [answered questions]              [0..10]:  xx
log.txt [including test diary]           [-20..0]: ( x)
project specs                            [-20..0]: ( x)
code quality                             [-20..0]: ( x)
dated submission deduction            [2 pts per]: ( x)
                                                   ---
total                                     [0..50]:  xx

Notes: 1. input files may vary over time
       2. answers to questions should be near top of log

Code quality includes: 
  - conformance to assignment requirements and specifications
  - conformance to coding standards [see course organizer]
  - engineering and design, including appropriateness of name choices
  - readability

Background: See lecture notes Chapter 12. Templates.

Procedural Requirements

  1. Copy all files from LIB/proj6/. You should see at least these:

    charsort.cpp     # sorts files of characters
    idsort.cpp       # sorts files of ID tokens
    intsort.cpp      # sorts files of integers
    stringsort.cpp   # sorts files of strings
    makefile         # builds all required components of project
    deliverables.sh  # submission configuration file
    

  2. Begin your log file named log.txt. (See Assignments for details.)

  3. Read and understand the supplied makefile. Pay careful attention to the targets and dependency relationships.

  4. Read and understand the supplied client sort programs charsort.cpp, intsort.cpp, stringsort.cpp, and idsort.cpp. Pay careful attention to the similarities among these files.

  5. Create the file tsort.h containing the function templates Display and InsertionSort, as detailed in the requirements section.

  6. Test your templates by building and running charsort.x, intsort.x, and stringsort.x using the supplied makefile. (Command make intsort.x builds intsort.x.) Note: "make" also attempts to build idsort.x, which won't work until you have completed steps 7 and 8. So you need to make the 3 specific targets at this point.

  7. Copy your files proj1/cstringcomp.h and proj1/cstringcomp.cpp to proj6/. If these need revision based on feedback from proj1, do that now.

  8. Copy your files proj2/id.h and proj2/id.cpp into your proj6 directory. Modify these files by adding operator>> and operator<, as detailed below (and, of course, updating the file header doc).

  9. Test your project thoroughly. Be sure to keep records and report the results in your test diary (part of log.txt).

  10. Answer the questions at the end of this document. Place the questions, each followed by your answer, near the top of your log.txt (before the chronology and test diary).

  11. General Identical Behavior Requirement. Except where specifically noted otherwise, behavior of executables must be identical to those of the area51 benchmark executables.

  12. Turn in all deliverables using the submit script system. (Read here for reminders how the submit system works.)

    Warning: Submit scripts do not work on the program and linprog servers. Use shell.cs.fsu.edu or quake.cs.fsu.edu to submit projects. If you do not receive two confirmations, the second with the contents of your project, there has been a malfunction.

Code Requirements and Specifications

  1. In file tsort.h define and implement function templates with these prototypes:

    template < typename T >
    void Display (const T* beg, const T* end, char ofc = '\0');
    
    template < typename T >
    void InsertionSort (T* beg, T* end);
    

  2. Both function templates use pointers to an unknown type T to define a range of values that is assumed, as usual, to be "half open", that is, include the beginning element and exclude the ending element. We use the notation [beg,end) to denote the range.

  3. Display writes the data in the range [beg,end) to standard output. If the output formatting character ofc is '\0' then the output data is not separated by anything. If ofc is any other character, then ofc should be output preceeding each item in the range.

  4. InsertionSort transforms the data in the range [beg,end) into sorted order using the InsertionSort algorithm. This is the same algorithm discussed in the Project 1 document and implemented in your file proj1/cstringsort.cpp.

  5. The software will only work on types for which there is an input operator overload (to fill up an array using std::cin >> ...) and a less-than operator overload (so that the sort algorithm can compare two elements).

    Overload these operators for class ID using the following prototypes:

    std::istream& operator>> ( std::istream& is , ID& id );
    bool          operator<  ( const ID& id1 , const ID& id2 );
    

    The behavior of operator>> should be:

    1. Read a string into a locally declared buffer of size 121. Be sure to protect the string read against buffer overflow with std::setw. Note that this restricts ID names to 120 characters.
    2. Read an integer into a locally declared number variable.
    3. Call the id.Set...(...) methods to put the read data into the object id as name and age, respectively.
    4. Return is by reference as usual.
    5. Hint: the data read can be done with a chain of three calls to operator>>, which is already overloaded for C-strings and ints. (One of the calls uses an IO manipulator to prevent overflow of the string buffer). Use "read token" pattern, not "read line" or, even harder, read character-by-character.

    The behavior of < should be based on the value d = DictionaryComp( id1.GetName() , id2.GetName() ) according to these cases:

    1. If d < 0 return true.
    2. If d > 0 return false.
    3. If d == 0 return (id1.GetAge() < id2.GetAge()).

    In words: first consider the name (independent of case), then use age to break ties.

    Note that neither of these operators is a member operator of class ID, nor should either be a friend of the class.

  6. Be sure that you have tested your code for syntax errors. All warnings should be eliminated.

  7. Be sure that you have tested your code for logic errors with the supplied clients charsort.cpp, intsort.cpp and stringsort.cpp.

  8. You should take some time to think about the results. For example, if you have a file of unsigned integers, that same file can be processed by charsort.x, intsort.x and stringsort.x, and the outputs differ. If there are an even number of entries in the file, it can also be processed by idsort.x! Make sure you can explain why and how this occurs.

  9. Be sure that you have tested your templates for genericity by compiling and running idsort.cpp.

  10. Be sure your code conforms to the C++ Code Standards (available also through the Course Organizer).

  11. Be sure you understand why the parameters for all operators and functions discussed above are declared as they are.

  12. A makefile has been supplied for this project. This does not mean it should be used with blind faith. Be sure that you understand this makefile, line-by-line (just as you should with any other supplied source code).

Discussion Questions

Answer the following questions. Submit the questions, each followed by your answer, near the beginning of your log, before the chronology and test diary.

  1. State what changes you made to transform the InsertionSort code from the specific code in project 1 to a function template. And for each change, explain why it was made.
  2. A file of integers can be sorted as type int or type std::string. Explain why the results sometimes differ.
  3. Explain why the sort template works for type std::string. (Hint: what operators are available for std::string?)
  4. Explain why the sort template does not work (without some code enhancements) for C-strings.
  5. Considering the way we were able to apply generic InsertionSort to type ID, describe what would be required to make the sort template actually work for C-strings. (You don't need details, just a few sentences that draw on the experience with ID.)

Hints