Homework 6 - Natural Mergesort

Due: Friday May 28, 2010 at 5:00pm

The purpose of this assignment is to implement natural mergesort and to write a program which takes arguments from the command line, instead of prompting the user and reading in via cin.

For this assignment, you will implement natural mergesort. Rather than using a menu-driven program or prompting the user for the filename, the user will provide the filename on the command line. The advantage of this method is that it requires no further interaction from the user after they type the command. Thus, the user can easily automate the program if they desire.

The command line arguments are parsed by changing the main() function to one that takes parameters. The parameters to main() are an integer that stores the number of command line arguments and an array of C-strings that contain the actual arguments. Refer to cli_example.cpp for an example of how to get two filenames from the command line by modifying main(). To compile and run the example, issue the following commands:

g++ -o cli_example cli_example.cpp
./cli_example
./cli_example file1 file2
You will be writing one main program that, like cli_example, takes two command line arguments. The first argument will be the input filename. The second argument will be the output filename. You will open the input filename for reading. You will open the output filename for writing. If you fail to open either file, print an error message and exit.

You will also need temporary output filenames to use during spliting and merging. Use a unique filename like a string of random characters so that you will not overwrite an actual file ("file1" or "tmp1" is not unique enough). A method to create a temporary filename out of the input filename is given in cli_example.cpp.

Your program will then perform the natural mergesort on the input file, using the method stated in the book and discussed in class. At the end of the program, the input filename should be untouched (containing the elements in the original order) and the output filename will contain the sorted elements.

Sample input files:

Email your completed assignment to me.