Question: 6.11 LAB: Course gradebook with unordered_map Step 1: Inspect the Gradebook abstract base class The read-only Gradebook.h file has a declaration for the Gradebook abstract
6.11 LAB: Course gradebook with unordered_map
Step 1: Inspect the Gradebook abstract base class
The read-only Gradebook.h file has a declaration for the Gradebook abstract base class. Access Gradebook.h by clicking on the orange arrow next to main.cpp at the top of the coding window.
The Gradebook class stores a collection of entries for a course. Conceptually, a gradebook entry is a (assignment name, student ID, score) triplet. Each assignment name is a string, each student ID an integer, and each score is a double. A score is entered into the gradebook via the SetScore() function.
The Gradebook class has six pure virtual functions that must be implemented in an inheriting class.
Step 2: Add member variables to CourseGradebook class
The CourseGradebook class inherits from Gradebook and is declared in CourseGradebook.h. Inspect each function that must be implemented. Choose one or more member variables to store gradebook data and add the member variables to the CourseGradebook class. Several options exist, so grading does not analyze the choice of member variables.
Step 3: Implement CourseGradebook's SetScore() and GetScore() functions
Implement the SetScore() function to enter a single entry into the gradebook. SetScore() has parameters for the assignment name, student ID, and score. How the entry is stored depends on the member variables chosen in step 2.
Implement the GetScore() function to get a single score from the gradebook. GetScore() has parameters for the assignment name and student ID, and returns a double for the corresponding score. NAN is returned if no such entry exists in the gradebook.
Code in main.cpp calls TestGetScoreAndSetScore() to test both functions. Run your program in develop mode and ensure that the test passes before proceeding further.
Step 4: Implement the remaining CourseGradebook functions
Implement the remaining functions according to the specifications below.
- GetAssignmentScores() takes a string for the assignment name and returns an unordered_map that maps a student ID to the student's corresponding score for the assignment. An entry exists in the returned map only if a score has been entered with the SetScore() function. An empty map is returned if no such assignment exists in the grade book.
- GetSortedAssignmentNames() returns a vector with all distinct assignment names, sorted in ascending order.
- GetSortedStudentIDs() returns a vector with all distinct student IDs, sorted in ascending order.
- GetStudentScores() gets all scores that exist in the gradebook for the student whose ID equals the function parameter. Scores are returned as an unordered_map that maps an assignment name to the student's corresponding score for that assignment.
- If needed, implement CourseGradebook's destructor to free any dynamically allocated content stored in the gradebook.
main.cpp ( only read)
#include
bool TestGetScoreAndSetScore(); bool TestGetAssignmentScores(); bool TestGetSortedAssignmentNames(); bool TestGetSortedStudentIDs(); bool TestGetStudentScores();
int main(int argc, char *argv[]) { bool result1 = TestGetScoreAndSetScore(); bool result2 = TestGetAssignmentScores(); bool result3 = TestGetSortedAssignmentNames(); bool result4 = TestGetSortedStudentIDs(); bool result5 = TestGetStudentScores(); cout
return 0; }
bool TestGetScoreAndSetScore() { cout testCases[] = { std::make_tuple("Midterm", 11111, 91.0), std::make_tuple("Homework 1", 22222, NAN), // no entry std::make_tuple("Homework 3", 55555, 71.5), std::make_tuple("Course project", 66666, 0.0), std::make_tuple("Homework 2", 10000, 90.0), std::make_tuple("Homework 4", 55555, 77.5), std::make_tuple("Homework 5", 33333, NAN), // no such assignment std::make_tuple("Final exam", 44444, NAN), // no entry std::make_tuple("Homework 2", 77777, 76.0), std::make_tuple("Homework 1", 88888, 64.5) }; // Iterate through test cases for (auto testCase : testCases) { string assignmentName = std::get(testCase); int studentID = std::get(testCase); double expected = std::get(testCase); double actual = gradebook.GetScore(assignmentName, studentID); // Reminder: Can't compare NAN with ==, so a special case is needed bool areEqual = isnan(expected) ? isnan(actual) : (actual == expected); if (areEqual) { cout
bool TestGetAssignmentScores() { cout hw2Scores = { pair bool TestGetSortedAssignmentNames() { cout expected = { "Course project", "Final exam", "Homework 1", "Homework 2", "Homework 3", "Homework 4", "Midterm" }; vector bool TestGetSortedStudentIDs() { cout expected = { 10000, 11111, 22222, 33333, 44444, 55555, 66666, 77777, 88888, 90000, 99999 }; vector bool TestGetStudentScores() { cout student22222Scores = { // Student has no score for "Homework 1" pair Gradebook.h (only read) #ifndef GRADEBOOK_H #define GRADEBOOK_H #include class Gradebook { public: virtual ~Gradebook() { } // GetScore() returns the specified student's score for the specified // assignment. NAN is returned if either: // - the assignment does not exist in the gradebook, or // - the assignment exists but no score exists for the specified student. virtual double GetScore(std::string assignmentName, int studentID) = 0; // SetScore() adds or updates a score in the gradebook. virtual void SetScore(std::string assignmentName, int studentID, double score) = 0; // GetAssignmentScores() returns an unordered_map that maps a student ID to // the student's corresponding score for the specified assignment. An entry // exists in the returned map only if a score has been entered with the // SetScore() function. virtual std::unordered_map // GetSortedAssignmentNames() returns a vector with all distinct assignment // names, sorted in ascending order. virtual std::vector<:string> GetSortedAssignmentNames() = 0; // GetSortedStudentIDs() returns a vector with all distinct student IDs, // sorted in ascending order. virtual std::vector #endif CourseGradebook.h ( edit) #ifndef COURSEGRADEBOOK_H #define COURSEGRADEBOOK_H #include class CourseGradebook : public Gradebook { protected: // Your code here public: virtual ~CourseGradebook() { // Your code here, if needed } std::unordered_map double GetScore(std::string assignmentName, int studentID) override { // Your code here (remove placeholder line below) return NAN; } std::vector<:string> GetSortedAssignmentNames() override { // Your code here (remove placeholder line below) return std::vector<:string>(); } // GetSortedStudentIDs() returns a vector with all distinct student IDs, // sorted in ascending order. std::vector void SetScore(std::string assignmentName, int studentID, double score) override { // Your code here } }; #endif TestUtility.h(only read) #ifndef TESTUTILITY_H #define TESTUTILITY_H #include class TestUtility { public: // Populates a CourseGradebook from a vector of rows. Each row is a vector // of strings. Row 0 must be the header row. Column 0 must be the student ID // column. static void PopulateGradebookFromRows(Gradebook& gradebook, const std::vector<:vector>>& rows) { using namespace std; // Iterate through non-header rows for (size_t rowIndex = 1; rowIndex 0) { // Get the assignment name from the header row string assignmentName = rows[0][colIndex]; // Convert score from string to double double score = stod(entry); // Add to gradebook gradebook.SetScore(assignmentName, studentID, score); } } } } // Returns a sample gradebook to use for testing purposes. static CourseGradebook MakeSampleGradebook() { using namespace std; vector #endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
