Lab PDF

EE 355 Lab 11 - Maps
1 Introduction
In this lab you will practice using associative containers (i.e. C++ maps) to track a
student information database.
This is a peer evaluated lab where your grade will be determined by 2 peer
evaluations. Please see the guidelines for peer evaluated labs provided by the
instructor.
2 What you will learn
After completing this lab you should be able:
 Use a C++ map to store key, value pairs
 Use iterators to search for or iterate over all elements in a map
3 Background Information and Notes
Review the lecture slides on C++ maps and iterators
4 Procedure
1. Download the class code
$ wget http://ee.usc.edu/~redekopp/ee355/code/student.h
2. The student class is completed in the .h file and you do NOT need to write a
student.cpp.
3. Make a test file, student_test.cpp. In main() create a map of student name to
Student objects
4. In main() enter a loop that prints a menu of options and lets the user enter a
command number where the commands are: 1=Add new student, 2=Remove
student, 3=Add a new score to all student objects, 4=Update score i for student,
s, to the provided value, 5=Calculate average, 6=Print student names and scores,
7=Exit program
5. If the user enters command 1, then query the user for a student’s first name and
add them to the map
6. If the user enters command 2, then query the user for a student’s first name and
remove that student object from the map if it exists
7. If the user enters command 3, add a new score of 0 to each Student’s ‘grades’
vector by iterating over all elements in the map
Last Revised: 4/1/2015
1
EE 355 Lab 11 - Maps
8. If the user enters command 4, then query the user for a student’s first name, an
index into the grades vector and an updated score then update the score at that
index for the given student.
9. If the user enters command 5, then query the user for an index into the grades
vector and calculate the class average (average over all students in the map) for
that score (iterate over all elements in the map)
10. If the user enters command 6, then print each students’ name and all of his/her
scores in order (iterate over each student in the map and then iterator over their
score vector…you must use map and vector iterators)
11. If the user enters command 7, exit the program
Student Class
class Student {
string name_;
std::vector<int> grades_;
public:
Student();
Student(std::string myname)
{
std::string get_name()
{
void add_grade(int score)
{
void update_grade(int index, int
int get_grade(int index)
{
std::vector<int>& get_grades() {
};
name_ = myname; }
return name_; }
grades_.push_back(score); }
score) { grades_[index] = score; }
return grades_[index]; }
return grades_; }
Sample Input:
1
1
3
4
4
5
3
6
2
6
0
Tommy
Jane
Tommy 0 88
Jane 0 94
0
// should output an average of 91
Tommy
// should output ‘Tommy: 88 0<ENTER>Jane: 94 0<ENTER>’
// Should remove Tommy from the map
// should output ‘Jane: 94 0<ENTER>’
5 Evaluation Criteria
1. [5 pt.] Run the program and use the sample input above and verify the output.
2. [4 pt.] Run the program again and add 2 users with 2 test scores each. Then
compute the average of each test and verify it outputs the correct value.
3. [1 pt.] Verify the stutest.cpp file is indented when opened in ‘gedit’ on the Linux
VM.
2
Last Revised: 4/1/2015