CS 215 – Fundamentals of Programming II
Spring 2008 - Project 3
20 points


Out: February 13
Due: F
ebruary 20


Reminder: Programming Projects (as opposed to Homework problems) are to be your own work. See syllabus for definitions of acceptable assistance from others.


For this project, we will reuse the Rational class from Project 1, work with vectors, and consider several different sorting algorithms by writing a program that actually counts the number of lines executed by each one. The algorithms we will look at are:



The assignment is to write template functions to sort vectors using the algorithms given above and named SelectionSort, InsertionSort, and ExchangeSort, respectively. Each should have one parameter, the vector to be sorted. In addition, you must write two template functions:


The code for these template functions should be put in a separate header file named sort.h (put ReadVector and WriteVector first) that is included in your main program. Don't forget the compiler guards. The guards should be put around all of the code in the file.


Write a (main) program that takes three command line arguments, an input file name and two output file names. For example, the command line might look like:


> sorttest inputfile.dat sortedfile.dat countfile.dat


This program should read Rationals (from Project 1 - see note below) from the input file into a vector using ReadVector. The program should then call the functions implementing each of the sorting algorithms above and write out the sorted vector into the first output file in the following format:


Selection Sort
<list of Rationals separated by one space>

Insertion Sort
<list of Rationals separated by one space>

Exchange Sort
<list of Rationals separated by one space>


Note that each sorting algorithm should be run on the original list of Rationals, so you will need to keep a copy of the original vector and reinitialize the vector to be sorted for each algorithm. Make sure each of your sorting algorithms works correctly at this point before continuing on to the rest of the project. (One way to determine if your sorting function is working correctly is to write out the vector being sorted to the screen using WriteVector (cerr) after every pass of the sorting algorithm on a small file. Just remember to comment out the calls, otherwise you'll get a lot of output from the submission system.)


To count the number of lines executed, instrument each sorting function to increment appropriately a counter whenever a line of the algorithm is executed (i.e., do not count the instrumentation code). A line of the algorithm is defined as in lecture. In particular, a for-statement header counts as one line as does a function call. You should use global counter variables for each algorithm declared at the top of sort.h; otherwise you would have to change each function to pass a counter variable by reference. Since we are exploring the behavior of O(n2) algorithms at relatively large n, these counters should be declared as unsigned integers, so that we can count to 232-1 (around 4 billion).


The output from this program written to the second output file should be the size of the input (i.e., the number of integers read in), and the instruction counts for each of the algorithms. E.g., your output might be:


Input size: 100
Selection Sort instructions: 10000
Insertion Sort instructions: 15000
Exchange Sort instructions: 20000

Note that these instruction counts are approximations. Your counts will be different for different input files. This output should be appended to the second output file. Output files may be opened for appending by including a mode argument ios::app to the constructor or open call. E.g.,


   ofstream out (argv[3], ios::app);


To explore average, best, and worst-case behavior, input files containing 100, 500, 1000, 5000, and 10000 Rationals in random, sorted, and reverse sorted order will be provided on csserver in directory /home/hwang/cs215/project3 no later than Monday, February 18. Run your program on each category of input file with output for each category going to the same output file. That is, data from the random files should go into one file, data from the sorted files should go into a second file, and data from the reverse sorted files should go into a third file. Draw the following graphs showing the the size of input (x-axis) vs. the number of instructions (y-axis):



Answer the following questions based on your experimental results. Note that the first three questions ask you to compare each of the three curves (for random, sorted, and reverse sorted order) for each algorithm, while the last asks you to compare the algorithms with respect to each other. Briefly explain your answer.



Note about the Rational class: You should fix any errors in your Rational class. In particular, it is very important that operator>> and operator< work properly. In addition, in order to make operator>> work correctly with file streams, it needs to check for the input stream failure and return before it does the invariant check. (Otherwise the random values will likely cause operator>> to throw the RangeError exception on the values.) To do this, put the following code into operator>> just after reading in the values:


// code for reading in values goes here

if (in.fail())  // stream failed or eof
   return in;

// code for invariant check goes here
// code for setting data members goes here


If you did not submit Project 1 or feel that your Rational class is beyond repair, please contact the instructor to obtain a skeleton Rational class that can be used for this project.


You must submit a makefile named Makefile.project3 for this project. It must generate an executable named sorttest. Submissions without working makefiles will be assessed up to 3-point penalty as indicated in the syllabus.


Follow the guidelines in the C++ Programming Style Guideline for CS 215 handout. As stated in the syllabus, part of the grade on a programming project depends on how well you adhere to the guidelines. The grader will look at your code listing and grade it according to the guidelines.


REMINDER: Your project must compile for it to be graded. Submissions that do not compile will be returned for resubmission and assessed a late penalty.


What to submit

Electronically submit a tarfile containing (only) sort.h, rational.h, rational.cpp, Makefile.project3, and your main program file as explained in the handout Submission Instructions for CS 215. . Please do not submit except.h, object files, or executable files. Only the sorting algorithms will be judged by comparing the first output file with the expected output. The instruction counting will not be judged electronically, since each person may implement the algorithms slightly differently.


Turn in a hardcopy of sort.h, Makefile.project3, your main program file, your output files, your graphs, and the answers to the above questions (do not turn in hardcopies of rational.h , rational.cpp or except.h).

Revised: 02/14/08 3 of 3