161 Lab 3 exercises

See the main lab page for submission deadlines and late penalties.

It is assumed you are keeping up to date with the lectures and labs.

The focus for this lab is on reading from files, using command line arguments, implementing more advanced sorting methods (mergesort or quicksort), adding functions to .h/.cpp files, and working with arrays of structs.

The sequence for obtaining the lab will be much the same as previous labs, and the commands are briefly summaried below (see the lab1 discussion for explanations and any troubleshooting needed):
ssh -x csci fork csci161/lab3 csci161/$USER/lab3
cd csci161
git clone csci:csci161/$USER/lab3
cd lab3
Similarly, the sequence to add/commit/push your lab work is similar to previous labs:
git add lab3.h
git add lab3.cpp
git add main.cpp
git commit -m "...some message describing your changes..."
git push


Program overview

The source code for lab3 is divided into three files: The provided makefile will combine these into a lab3x executable
(compile using make lab3x and run using ./lab3x).

The provided code forms a complete program for basic inventory information, and behaves as follows:

The initial version of the program works, but it reads all its input from the user via standard input (cin/the keyboard), and carries out sorting using a simple bubblesort.

You'll be making a variety of changes to the program as outlined in the section below.


Required lab3 changes
For full marks in lab3 you'll need to perform each of the changes specified below, following the restrictions on how those changes are made and following course code standards.

1. Introducing command line arguments so the user can specify an input file
As discussed in lectures, we can allow the user to pass command line arguments to a program when they invoke it. In this case we'll allow the user to specify the name of an input file to read from, e.g.
./lab3x myinputfile

If they don't provide an input filename as a command line argument, or if we are unable to open the file they specify, then the program will simply use cin as in the original version.

You will need to modify the main routine in main.cpp to accept parameters int argc, char* argv[], and within the main routine you can check argc to see if they provided any arguments.

If argc is greater than one then they provided at least one argument, and we'll assume the name of the file they want to use is the one in argv[1].

The mechanics of opening the file and using it instead of cin are described in part 2 below.

2. Reading all the input data from the named file (if one was provided) instead of cin
If the user provided a filename as a command line argument, then we can attempt to open it for input using an ifstream variable, e.g.
ifstream infile;
infile.open(argv[1]);
We can check if the file opened successfully using infile.is_open(), which returns true if it did open ok and false otherwise.

If the file opened successfully then we can pass infile (or whatever you named your ifstream variable) to the various input routines instead of cin (the input functions are set up to use whichever form they are passed). If the file did not open successfully, or no filename was provided, then pass cin to the input routines as was done in the original main.

Note that if the file did open successfully then at the end of the program we also need to close the file, e.g. infile.close();

3. Using mergesort or quicksort to sort, rather than bubblesort
You are to replace the simple bubblesort code with either your own code to carry out a quicksort or your own code to carry out a mergesort.

Whichever approach you choose, you'll likely need to:


Testing your program
You'll need to test your program for a variety of behaviours now: