It is assumed you are keeping up to date with the lectures and labs.
Some additional git information is available through the
git quick notes,
git project/lab submission, and
lecture resources pages.
This lab builds on the ideas from lab1, but now lets the user run multiple head-to-head matchups, adds dynamic array allocation (including an array-resizing approach), and uses quicksort to sort the rankings before writing them back to a file.
The mechanics of git and make are essentially unchanged since lab1 (though of course
now the git repository is named lab2 and the file names are lab2.h, lab2.cpp, test2.h, test2.cpp,
ranking.h, and ranking.cpp.
(Refer back to lab1 for the git/make syntax if needed.)
The marking for this lab will be stricter in terms of the code standards, so be sure you have reviewed them carefully. Marks will be deducted for failing to follow standards.
// sample main from lab2.cpp
int main(int argc, char* argv[])
{
// quit if the user didn't pass valid arguments
if (!checkUsage(argc, argv)) {
cerr << "Program terminated early, unable to proceed\n";
return 0;
}
string filename = argv[1]; // filename is more intuitive later
welcome(); // program intro for user
// load the rankings from the named file
ItemRank *rankings = new ItemRank[DefaultSize];
int rankSize = DefaultSize;
int numItems = readRankings(filename, rankings, rankSize);
// run head-to-head matchups
int matches = getNumMatches();
for (int m = 1; m <= matches; m++) {
if (!runMatchup(rankings, numItems)) {
cerr << "Error: match " << m << " unsuccessful, rankings not updated\n";
}
}
// sort the rankings (lowest to highest)
qsortRanks(rankings, 0, numItems-1);
// write the updated rankings back to the file
if (!writeRankings(filename, rankings, numItems)) {
cerr << "Unable to updated rankings file " << filename << endl;
}
return 0;
}
|
./lab2x tests/posted Welcome to the lab2 item ranking program This program reads a collection of item rankings from a file (whose name is provided as a command line argument, e.g. ./lab2x foods) Lets you pick how many head-to-head item comparisons to run, then for each head-to-head comparison it presents you with a choice between two items, and uses your preference to update their rankings. It then sorts the updated rankings and writes them back to the file. Please enter the number of head-to-head matches you would like run (e.g. 10) 3 Given the choice between the following two items: First: brussel sprouts Second: salmon sashimi please enter F for first, S for second, or D for draw (tied) f Given the choice between the following two items: First: hawaiian pizza Second: salmon sashimi please enter F for first, S for second, or D for draw (tied) d Given the choice between the following two items: First: capsicum Second: those dutch olie bols things please enter F for first, S for second, or D for draw (tied) s Updated rankings written back to file tests/posted |