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:
- lab3.h: contains the specifications for a partInfo struct and the headers for
all the functions needed for the lab
- main.cpp: contains just the program main routine
- lab3.cpp: contains the implementations of all functions other than main
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:
- it gets the user to specify how many parts they wish to track
and allocates an array to hold the part information
- it then gets the user to pick between sorting by name or by id
- it then reads in the names, ids, and descriptions for each part, where
each name, id, and description is read in as a single line of text
- it sorts the parts using the desired field
- it displays the sorted data then deletes the allocated array
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:
- add a helper function (e.g. merge to help in mergesorting or partition to help in quicksorting),
- update the sort parameter list to accept a low/high range rather than just the array size,
- add/update the prototypes in the lab3.h file to match the implementation in the .cpp file,
- update the sorting call from main in main.cpp
Testing your program
You'll need to test your program for a variety of behaviours now:
- does it correctly handle cases where the user provides a valid filename as a command line argument?
(i.e. does it read from the file instead of cin)
- does it correctly handle cases where the user provides an invalid filename as a command line argument?
(i.e. does it recognize the file didn't open, and so reads from cin)
- does it correctly handle cases where the user provides no command line argument?
(i.e. does it use cin for this case)
- does it sort correctly by name when asked?
- does it sort correctly by id when asked?
- does it work correctly even if the user specify only one or two parts?
- does it work correctly even if there are duplicate names/ids in different parts?