CS 215 - Fundamentals of Programming II

Spring 2005 - Programming Project 3

25 points

Out: February 8, 2006
Due: February 17, 2006 (Friday after Exam 1)



Note that although this project is not due until after Exam 1, the material covered in this project will be on Exam 1, so it is in your best interest to have substantially started this project before Exam 1.


For this project, we will reuse the Date 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 named above. In addition, you must write a template function: The code for these template functions should be put in a separate header file named sort.h (put 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 two command line arguments, an input file name and an output file name. This program should read Dates (from Project 1 - see note below) from the input file into a vector. The program should then call the functions implementing each of the sorting algorithms above. Note that each sorting algorithm should be run on the original list of Dates, 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 (cout) after every pass of the sorting algorithm. Just remember to comment out the calls before submitting it for grading.)


To count the number of lines executed, instrument each 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(n^2)$ algorithms at relatively large n, these counters should be declared as unsigned integers, so that we can count to $2^{32}-1$ (around 4 billion).


The output from this program 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
Bubble Sort instructions: 20000
Insertion Sort instructions: 15000
Note that these instruction counts are approximations. Your counts will be different for different input files. This output should be appended to the 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[2], ios::app);


To explore average, best, and worst-case behavior, input files containing 100, 500, 1000, 5000, and 10000 Dates in random, sorted, and reverse sorted order will be provided on csserver in directory /home/hwang/cs215/project3 no later than Monday, February 13. 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 Date class: You should fix any errors in your Date class. In particular, it is very important that operator< work properly. In addition, in order to make operator» work correctly with file streams, 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 Date class is beyond repair, please contact the instructor to obtain a skeleton Date class that can be used for this project.


You must submit a makefile for this project. 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.


Submit a tar file with sort.h, date.h, date.cpp, your makefile, and your main program file. Turn in a hardcopy of sort.h, your makefile, and your main program file, your output files, your graphs, and the answers to the above questions (do not turn in hardcopies of date.h or date.cpp). Please do not submit except.h, object files, or executable files.


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.



Converted using latex2html on Tue Feb 7 17:47:03 CST 2006