lab03 : Selection Sort
num | ready? | description | assigned | due |
---|---|---|---|---|
lab03 | true | Selection Sort | Thu 10/24 08:00AM | Wed 10/30 11:59PM |
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.
Step by Step
Step 0: 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.
As stated in previous labs, there are a few requirements you must follow if you decide to work with a partner. I will re-iterate them here:
- Your partner must be enrolled in the same lab section as you.
- You and your partner must agree to work together outside of lab section in case you do not finish the lab during your lab section. You must agree to reserve at least two hours outside of lab section to work together if needed (preferrably during an open lab hour where you can work in Phelps 3525 and ask a mentor for help). You are responsible for exchanging contact information in case you need to reach your partner.
- If you choose to work with a partner for a future lab where pair programming is allowed, then you must choose a partner you have not worked with before.
- You MUST add your partner on Gradescope when submitting your work EACH TIME you submit a file(s). After uploading your file(s) on Gradescope, there is a “Group Members” link at the bottom (or “Add Group Member” under “Groups”) where you can select the partner you are working with. Whoever uploaded the submission must make sure your partner is part of your Group. Click on “Group Members” -> “Add Member” and select your partner from the list.
- You must write your Name(s) and Perm number on each file submitted to Gradescope.
Once you and your partner are in agreement, choose an initial driver and navigator, and have the driver log into their account.
Step 1: Copying some programs from my directory
Visit the following web link—you may want to use “right click” (or “control-click” on Mac) to bring up a window where you can open this in a new window or tab:
http://cs.ucsb.edu/~richert/cs32/misc/lab03/
You should see a listing of several C++ programs. We are going to copy those into your~/cs32/lab03 directory all at once with the following command:
cp ~richert/public_html/cs32/misc/lab03/* ~/cs32/lab03
Note: If you get the error message:
cp: target ‘/cs/student/youruserid/cs32/lab03’ is not a directory
then it probably means you didn’t create a ~/cs32/lab03 directory yet. So do that first.
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/lab03.
After doing this command, if you cd
into ~/cs32/lab03 and use the ls
command, you should see several files in your ~/cs32/lab03 directory—the same ones that you see if you visit the link http://cs.ucsb.edu/~richert/cs32/misc/lab03/
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.
Step 2: Understanding the Starting Point Code
Step 2a: Understanding the tddFuncs.cpp code
You may find it interesting to compare the versions of tddFuncs.h
and tddFuncs.cpp
from with the ones from this lab (lab03).
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 lab01 and this week:
lab01 | lab03 |
---|---|
tddFuncs.h | tddFuncs.h |
tddFuncs.cpp | tddFuncs.cpp |
Some things to notice:
- In the lab01 version of tddFuncs.cpp, there was duplicated code—identical versions of
assertEquals
for bothint
andstd::string
. In the lab03 version, this has been DRY’ed up and factored out into a template in the lab03tddFuncs.h
. - In the lab03 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. 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
(H/T to Hunter Laux for finding this.)
Step 2b: 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 lab01 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.
Step 2c: Understanding the Student and Roster classes
The Student.h
and Student.cpp
classes are mostly unchanged from lab01, 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 lab01. 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.
Step 2c: 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 lab01) 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.
Step 3: 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
.
Step 4: 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.
Step 5: Submitting via Gradescope
The lab assignment “Lab03” 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
). 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 Lab03.