UE CS 215 - CS 215 – Fundamentals of Programming II - Project 3

Unformatted text preview:

CS 215 – Fundamentals of Programming IIFall 2007 (Harlaxton) – Project 320 pointsOut: September 20, 2007Due: October 4, 2007 (Thursday after long weekend)Reminder: Programming Projects (as opposed to Homework problems) are to be your own work. See syllabus for definitions of acceptable assistance from others.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: ● Selection Sort (pages 116-119) ● Bubble Sort (Exercise 3-17 on page 174) ● Insertion Sort (pages 203-205) The assignment is to write template functions to sort vectors using the algorithms named above. In addition, you must write a template function: ● WriteVector that writes a vector to any output stream as demonstrated in lecture. I.e., this function is like the one on page 196, but has an added ostream parameter that is the stream to write the output to (rather than always to the screen). 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(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 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:09/17/07 1 of 3Input size: 100Selection Sort instructions: 10000Bubble Sort instructions: 20000Insertion Sort instructions: 15000Note 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 the Harlaxton server in directory /home/hwang/cs215/project3 no later than Monday, September 24. 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): ● One graph for each algorithm for the three categories of input. I.e., you should produce three graphs (containing data from a single algorithm, respectively) with 3 curves each (one for each category of input file). These graphs compare the effect of input order on a particular algorithm. ● One graph with the curves for the random input. I.e. a graph with 3 curves (one for each algorithm) of the data from random input files. This graph compares the algorithms to each other. 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. ● Which algorithms have best-case running time when items are in sorted order? ● Which algorithms have worst-case running time when items are in reverse sorted order? ● Which algorithms have running time that is mostly unaffected by the input data order? ● Which algorithm appears to be the better algorithm? 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, 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 hereif (in.fail()) // stream failed or eof return in;// code for invariant check goes here// code for setting data members goes hereIf 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


View Full Document

UE CS 215 - CS 215 – Fundamentals of Programming II - Project 3

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download CS 215 – Fundamentals of Programming II - Project 3
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 215 – Fundamentals of Programming II - Project 3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 215 – Fundamentals of Programming II - Project 3 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?