Programming Assignment 2
Due Friday, October 27
Based on an assignment created by Dr. Sarraille.
Write a program that REPEATEDLY:
The program will continue UNTIL the user wishes NOT to continue.
- prompts the user to enter a filename,
- opens the file,
- reads a comment from the first line,
- reads an integer N from the second line (e.g. N = 1000),
- reads N integers from the rest of the file, putting them into
consecutive locations in an array,
- writes the name of the input file,
- writes the comment from the input file,
- sorts an identical copy of the array using each of the following
- selection sort
- insertion sort
- quick sort
- merge sort
- writes a report of the number of compares and moves done in each
- asks the user if they wish to continue.
Your program must make a little announcement (examples to follow) just
starting and just after completing each run of one of the four sorting
functions. This will allow you to get a sense of how long each
algorithm takes to do its work.
Your program must pass in a new copy of the original
each time it calls a sorting function. We don't want the first function
call to be the only one that really sorts the original array!
When you count "moves" count the number of times that an element of the
list is copied from one location to another (with an assignment
Each call to the function swap counts as three moves.
When counting compares, count only comparisons of list elements
things that are being sorted), not the array indices. For
if the array you are using is named "data" the line of code:
if (data[j] < data[j - 1])
contains a comparison that you would have to count, because it is a
comparison of two elements in the list. On the other hand, the line of
while (i < j)
does not compare elements of the list. It is a comparison of array
only. Therefore you must not count that comparison.
The line of code
for (;(loc > 0) && (data[loc-1] > nextItem)
contains one comparison that you should not count and one
that you should count.
You should not count "loc > 0" because it compares indices.
You should count "data[loc-1] > nextItem" because
compares a list element with (a copy of) another list element.
Keep in mind that it may be a little "tricky" to insert the code that
counts the comparisons executed in if-clauses and loop conditions. Give
problem due thought and consideration.
The code you need to perform the sorting is contained in a set of
You will need to modify this code so that it takes care of
the counting of
the compares and the moves. That will mean adding parameters to
functions and adding pieces of code that do counting within functions.
can discuss more details of this in class.
I also included a little prototype of a driver program called
driver.cpp You can get ideas from driver.cpp for
formulating the program you do for this assignment.
To do some preliminary testing, you can compile the whole "package" of
driver plus sorting code on the SUN Ultra's with this command:
This assumes that the only source files in your current working
are the ones that are part of the "package." Of course, to complete the
assignment you will need to write another driver that is fully
SAMPLE INPUT AND OUTPUT:
Let's look at two sample input files and the corresponding output in
better understand what the format of the files and outputs has to be.
The sample input files are
The corresponding sample program output:
Note: When you see a message such as "selection sort starting ... done"
understand that the program first writes "selection sort starting ... "
then performs the selection sort, and then writes
Lists can be small, medium, and large in size. They can also be random,
ordered, or reverse-ordered. You need to test all the possible
combinations (there are 9). Dr. Sarraille has a package containing all
you need here (but don't click until you are ready to download):
(Some browsers may not access the link above properly. If one browser
problem, try another. Clicking on the link should download and unpack
files on most systems. You may have to do an extra step to unpack the
In the header of your program, include a paragraph or two describing
you learned from writing and testing this program. Do your very best to
make this well-written. This will be another aspect of the assignment
will count heavily.
WHAT TO TURN IN:
Here is the list of things you have to turn in:
Notice that I'm asking you to send me all the files I need to compile
program (plus some other things). Send all the source files I am asking
whether you changed them or not. If I have to add or take away files or
the format to get the program to compile or work properly then I will
- At the start of class on the due date, place the
following items on the "counter" in front of me:
Make sure that all of the code and script content shows on the paper.
Make sure all content is plainly readable and properly formatted.
- a hardcopy of your final version of the program source code,
- a hardcopy of your test script showing adequate testing of
- Upload to the homework
submission system before midnight on the due date:
shell archive file (only one) containing all things
- Your final versions of these files that I gave you:
- Your fully functional driver program, including your
writings about what you learned,
- Your script showing all the test runs on the input files I
- A file named 'README' containing the compilation command one
should use to compile your program.
- A copy of your 'makefile' if you used one.
Make sure the script you send is cleaned up using the sort of "col -b"
(Don't forget to change the commands so you are using the correct name
Make sure all your source files are clean too. Sometimes files
from a PC to a Sun Ultra need to be
Please don't send me any compiled code.