CS 10C Programming Concepts and Methodologies 2

Project 24.1

Reminder: all of your development must be done inside the zyBooks environment. This means no copy/pasting code into the zyBooks environment and no developing your code elsewhere and then typing your code into the zyBooks environment all at once. If this requirement is not followed, a programming project may receive a score of 0.

To clarify: you can always copy/paste code that I am not expecting you to develop yourself, such as code that is given in the assignment, or code from a previous assignment if a new assignment builds on it.

Rewrite this high scores program using an STL vector instead of an array. (Note, you will be using an STL vector, not the MyVector class developed in lesson 19.)

Clarifications and Additional Requirements:

  • Since you are working with code written by someone else, no documentation is required for this project.

  • Your program must use the following five function headers exactly as they appear here:

    void getVectorSize(int& size);
    void readData(vector<Highscore>& scores);
    void sortData(vector<Highscore>& scores);
    vector<Highscore>::iterator findLocationOfLargest(
                                    const vector<Highscore>::iterator startingLocation,
                                    const vector<Highscore>::iterator endingLocation);
    void displayData(const vector<Highscore>& scores);
    
    
  • The size parameter from the given code won't be needed now, since a vector knows its own size.

  • Notice that the findLocationOfLargest() function does not need the vector itself as a parameter, since you can access the vector using the provided iterator parameters.

  • The name field in the struct must still be a c-string

  • The focus of this assignment is to use iterators. You must use iterators wherever possible to access the vector. As a result, you must not use square brackets, the push_back() function, the at() function, etc. Also, the word "index" shouldn't appear in your code anywhere. You won't get full credit if you miss an opportunity to use iterators.

  • You should still ask the user to enter the number of scores there will be, and then you should create a vector with the required capacity. You can do this by using the vector class's constructor that creates a vector with the capacity indicated by its parameter. For example, to create a vector of size 100, use this:

    vector<sometype> myExampleVector(100);

    It is possible to write our program without bothering to indicate a size if we simply use push_back() to add each high score struct to the vector. We aren't doing it that way, because I want you to practice using iterators.

  • You could sort the scores by simply calling the STL sort() algorithm. I would suggest that you try this out because it's something you should know, but for your submitted program you are required to sort the vector as it is done in the given code, except using iterators to access the items in the vector. You won't get credit for the assignment if you use the STL sort() function.

  • In your displayData() function you'll need to use const_iterator instead of iterator. See lesson 19.4.

  • Using an iterator to access a particular member of a struct is a little tricky.

    The short version: to access the "firstmember" member of a struct that is being pointed to by an iterator named "iterator1", use

    iterator1 -> firstmember
    

    The long version: We need to first use the * operator to dereference the iterator to get to the struct, then use the dot (selection) operator to get to the "firstmember" member of the struct. The natural first attempt might be:

    *iterator1.firstmember
    

    However, you'll get a compiler error. The problem is that the dot operator has higher precedence than the * operator, so the first thing the compiler sees is "iterator1.firstmember". You'll get an error because the dot operator can't occur if the left operand is an iterator. We need to force the compiler to apply the * operator first, and then apply the dot operator to that result. Here is a correct form:

    (*iterator1).firstmember
    

    This will work just fine, but there is a C++ operator that combines the dot operator and the * operator. You can think of it as the "dereference first and then select" operator. Here's that form:

    iterator1 -> firstmember