Lab 4: Selection sort
Due: 4/26 11:59pm
1. Goals
By the time you have finished this lab, you should have demonstrated your ability to code a selection sort on a an array of pointers to objects in C++. You will also get more practice with doing test-driven development of object-oriented C++ code.
2. Getting Started
This lab may be done solo, or in pairs.
Before you begin working on the lab, please decide if you will work solo or with a partner. If you decide to work with a partner, read the Working in pairs section of the course webpage.
Once you and your partner are in agreement, choose an initial driver and navigator, and have the driver log into their account.
3. Copying some programs from my directory
Visit the following web link:
https://cs.ucsb.edu/~emre/cs32/code/lab4/
You should see a listing of several C++ programs. We are going to copy those into your ~~/{{site.course | downcase}}/{{page.num}} directory all at once with the following command:
cp ~emre/public_html/cs32/code/lab4/* ~/cs32/lab4
The *
symbol in this command is a "wildcard"–it means that we want
all of the files from the source directory copy be copied into the
destination directory namely ~/cs32/lab4
.
After doing this command, if you cd
into ~/cs32/lab4
, and use the ls
command, you should see several files in the directory–the same ones that you
see if you visit the link above.
If so, you are ready to move on to the next step.
If you don't see those files, go back through the instructions and make sure you didn't miss a step. If you still have trouble, ask your TA for assistance.
4. Understanding the Starting Point Code
4.1. Understanding the tddFuncs.cpp
code
You may find it interesting to compare the versions of tddFuncs.h
and
tddFuncs.cpp
from lab 2 with the ones from this lab.
You do NOT need to make any changes to these files. However, they serve the purpose of showing how we can use template functions in C++ and Template Specialization to create code that is more "DRY", i.e. follows the adage "Don't Repeat Yourself".
Here are links to the versions of these files from lab 2 and this week:
Some things to notice:
- In the lab2 version of tddFuncs.cpp, there was duplicated
code–identical versions of
assertEquals
for bothint
andstd::string
. In the lab4 version, this has been DRY'ed up and factored out into a template in the lab4tddFuncs.h
. - In the lab4 version of
tddFuncs.cpp
, there are also two overloaded versions of the functionassertEquals
that are NOT using the template. These versions of the function are needed because C-strings (i.e.const char *
andconst char * const
values) cannot be compared with the==
operator. The version for two C-strings usedstrcmp
instead, while the version for a C-string and anstd::string
simply converts the C-string to a C++ string and invokes the template version forstd::string
.
Again, you don't need to do anything with this for this lab, so if you want to gloss over this for now and come back to it later, that's fine. You need to look it over though, and understand what is going on.
A final note: the way we overloaded the functions here does work, and it has the advantage of being easy to understand and follow. However, it is probably not the "current best practice" way of handing this, and strictly speaking, is not "template specialization", it is rather just "garden variety overloading". We'll go over the "better way", i.e. true template specialization, in a future lab or lecture (although each variant has its uses). For now, if you are curious, and want to "read ahead", consider the following StackOverflow link:
http://stackoverflow.com/questions/17344405/template-function-specialization-vs-overloading
4.2. Understanding the CXXFLAGS
in the Makefile
In this week's Makefile, as with last weeks, we have the following.
CXXFLAGS = -Wall -Wextra -Wno-unused-parameter -Wno-unused-private-field # Change to this before final submission: # CXXFLAGS = -Wall -Wextra -Werror
See the explanation in lab 2 of these two definitons for CXXFLAGS
. It
will be important for you to modify these before submitting to make sure
that your code will compile with those flags set.
4.3. Understanding the Student
and Roster
classes
The Student.h
and Student.cpp
classes are mostly unchanged from
lab 2, except for the addition of a few new methods.
Please look those over–these are mostly methods for reading in a Student from a comma-separated string, and are there so that we can read in students from a CSV file (i.e. a file of comma separated values) into a Roster.
The Roster.h
and Roster.cpp
files, though, are new.
You'll see that the Roster.h
file contains the specification of a
class that represents a collection of Students. It represents this
collection with a simple fixed size C++ array of pointers to Student
objects. THERE ARE CERTAINLY BETTER WAYS TO IMPLEMENT A COLLECTION OF
STUDENTS. The point for this week's lab, though, is to understand
sorting algorithms.
We will implement a selection sort on the array of pointers to Student objects. Since we only have to swap the pointers in the array–we don't have to swap the entire Student object–our algorithm will run in the same amount of time no matter how large the actual Student objects may be. In general, this is a good idea if/when one is working with large objects.
The files where you will be making changes this week are:
Student.cpp
, where you'll be updating the methods that you wrote in lab 2. Don't just copy over the entire file though, because if you do, you'll lose the new methods that we supplied you with in the starting point code.Roster.cpp
, where you'll be replacing stubs with correct code.
4.4. Understanding the tests: testStudent.cpp
and tddFuncs.cpp
What you should do next is to look through the test cases in
testStudent.cpp
(which includes all the tests from the three test
files in lab 2) and testRoster1.cpp
, testRoster2.cpp
and
testRoster3.cpp
Look over these files and understand how they work. Then, type
make tests
to compile and run all of the tests.
You can also type, for example, make testRoster1
and ./testRoster1
to make and run these tests one at a time.
5. Make all the test cases pass
When you are ready, start editing Student.cpp
, replacing the stubs for
the constructor first, and then each of the stubs for the other
functions. Get all the tests in testStudent to pass first.
Then, read through testRoster1.cpp
and see what methods in
Roster.cpp
you think you need to update in order to get those tests to
pass.
Then proceed to do the same with testRoster2.cpp
and
testRoster3.cpp
.
6. Checking your work before submitting
When you are finished, you should be able to type make clean
and then
make tests
and see that all the test cases pass.
At that point, you are ready to try submitting to the Gradescope system.
7. Submitting via Gradescope
The lab assignment "Lab 4" should appear in your Gradescope dashboard in CMPSC 32. If you haven't submitted anything for this assignment yet, Gradescope will prompt you to upload your files.
For this lab, you will need to upload your modified files (i.e. Student.cpp
and Roster.cpp
). The autograder will ignore other files, so do not modify them
if you want to observe the same behavior as what is run on Gradescope. For this
lab, you are required to submit your files with your github repo.
If you already submitted something on Gradescope, it will take you to their "Autograder Results" page. There is a "Resubmit" button on the bottom right that will allow you to update the files for your submission.
For this lab, if everything is correct, you'll see a successful submission passing all of the autograder tests.
Remember to add your partner to Groups Members for this submission on Gradescope if applicable. At this point, if you worked in a pair, it is a good idea for both partners to log into Gradescope and check if you can see the uploaded files for Lab4.