Saint Louis University |
Computer Science 180
|
Dept. of Math & Computer Science |
Please see the general programming webpage for details about the programming environment for this course, guidelines for programming style, and details on electronic submission of assignments.
For this assignment, you must work individually in regard to the design and implementation of your project.
Please make sure you adhere to the policies on academic integrity in this regard.
Our goal for this assignment is to manipulate list of integers and iterators for those lists. Given two lists, both of which are known to be sorted in non-decreasing order, your goal is to efficiently merge the elements from the second list into the first list, while ensuring that the combined result is still sorted. Because the two original lists are internally sorted, the merging process can be completed in linear time.
The source code for the entire project is already complete, with the exception of a single routine that we ask you to implement.
Notice that we have designated the secondary list as immutable with the const designation. You are not to remove any values from that list, but to reinsert those values in the proper place in the primary list./** * Given two distinct lists, each of which are assumed to be sorted in * non-decreasing order, this function mutates the primary list so as * to include all elements of the secondary list while maintaining the * non-decreasing invariant. */ void merge(list& primary, const list & secondary)
Before writing your code, think about how you will accomplish the task. It may help to consider the process you might use if doing this with lists written on paper. You might imagine placing one finger at the beginning of the first list and one finger at the beginning of the second list. Now, you should be able to advance the fingers in a way, ensuring that items are copied from the second list into the proper place in the first list as you go. To implement this strategy, the iterator classes can be used to represent the idea of a finger moving through a list.
Advice: if you are having trouble, always consider that a problem can be fixed by simplifying your code rather than complicating it. There exists a remarkably simple solution to this assignment.
All such files can be downloaded here.
Merger.cpp
This is the one file which you surely must modify
It contains a stub for the merge function,
yet without any code.
MergeTest.cpp
This file provides a main driver to be used in testing your
program. You will not need to modify or examine this code. The
use of the driver is discussed later in this
assignment description.
makefile
This makefile should allow you to rebuild your project by
simply typing 'make' rather than in invoking the compiler
directly.
The executable MergeTest is a driver which will handle reading the input, creating the initial lists, calling your routine, and then outputting the merged result.
The expected format for input is as follows (please make sure that the inputfile you submit adheres strictly to these standards!):
You will notice that this format allows you to specify a test file which involves many different merges, as demonstrated in the following example of a properly formatted input file.
5 9 12 1000 4 10 11 15 1000 3 4 1000 1000 1 5 5 8 1000 2 8 10 1000 -1
The driver will create the two lists
After that, it will create two lists
Then it will merge
By default, the driver reads input from the keyboard. However, if you would like to have the driver read input from a text file, you may create such a file, and then give the filename as a single argument when starting the program (as was done in an earlier programming assignment).
Test Input
Please submit a single file, inputfile, which we will
use as sample input when testing all of the students'
submissions. The file format for inputfiles is based on use with our driver
Your input file may use at most 100 lines (although you may
certainly specify multiple merges within the 100 line limit).
In order to make sure that your inputfile follows the proper
format, we strongly recommend that you make sure that your
inputfile is accepted by the driver.
Readme File
A brief summary of your program, and any further comments you
wish to make to the grader.
The assignment is worth 10 points. Eight points will be awarded based on our own evaluation of your assignment and the readme file. One additional point will be awarded fractionally based on how well your program performs on other students' test inputs. The final point will be awarded fractionally based on how well your test input fools other students' flawed programs.