Question: Assignment 2.1 [25 points] Here is a high scores program. Rewrite this program using an STL vector instead of an array. (Note, you will be

Assignment 2.1 [25 points]

Here is a high scores program. Rewrite this 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.)

#include using namespace std; const int MAX_NAMESIZE = 24; struct Highscore{ char name[MAX_NAMESIZE]; int score; }; void getArraySize(int& size); void readData(Highscore highScores[], int size); void sortData(Highscore highScores[], int size); int indexOfLargest(const Highscore highScores[], int startingIndex, int size); void displayData(const Highscore highScores[], int size); int main() { Highscore* highScores; int size; getArraySize(size); highScores = new Highscore[size]; readData(highScores, size); sortData(highScores, size); displayData(highScores, size); delete [] highScores; } void getArraySize(int& size){ cout << "How many scores will you enter?: "; cin >> size; cin.ignore(); } void readData(Highscore highScores[], int size) { for(int index = 0; index < size; index++) { cout << "Enter the name for score #" << (index + 1) << ": "; cin.getline(highScores[index].name,MAX_NAMESIZE,' '); cout << "Enter the score for score #" << (index + 1) << ": "; cin >> highScores[index].score; cin.ignore(); } cout << endl; } void sortData(Highscore highScores[], int size) { int largestIndex; Highscore tempRecord; for (int count = 0; count < size - 1; count++){ largestIndex = indexOfLargest(highScores, count, size); tempRecord = highScores[largestIndex]; highScores[largestIndex] = highScores[count]; highScores[count] = tempRecord; } } int indexOfLargest(const Highscore highScores[], int startingIndex, int size){ int targetIndex = startingIndex; for (int count = startingIndex + 1; count < size; count++){ if (highScores[count].score > highScores[targetIndex].score){ targetIndex = count; } } return targetIndex; } void displayData(const Highscore highScores[], int size) { cout << "Top Scorers: " << endl; for(int index = 0; index < size; index++) { cout << highScores[index].name << ": " << highScores[index].score << endl; } }

Clarifications and Additional Requirements:

  • Documentation is not required for this assignment.

  • Your program must use three functions that accept the vector of Highscore structs (the size parameter from the given code won't be needed now, since a vector knows its own size). You must use these function headers:

    void readData(vector& scores) void sortData(vector& scores) void displayData(const vector& scores) 
  • The name field in the struct must still be a c-string

  • 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 myExampleVector(100);
  • 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.

Here is the client file.

#include #include #include #include "orderedpair.h" using namespace std; using namespace cs_pairs; int main() { int num1, num2; OrderedPair myList[10]; srand(static_cast(time(0))); cout << "default value: "; myList[0].print(); cout << endl; for (int i = 0; i < 10; i++) { myList[i].setFirst(rand() % 50); myList[i].setSecond(rand() % 50 + 50); } myList[2] = myList[0] + myList[1]; if (myList[0] < myList[1]) { myList[0].print(); cout << " is less than "; myList[1].print(); cout << endl; } for (int i = 0; i < 10; i++) { myList[i].print(); cout << endl; } cout << "Enter two numbers to use in an OrderedPair. Make sure they are different numbers: "; cin >> num1 >> num2; OrderedPair x; /* use this before you've implemented the exception handling in the class: */ x.setFirst(num1); x.setSecond(num2); /* use this after you've implemented the exception handling in the class: try { x.setFirst(num1); x.setSecond(num2); } catch (OrderedPair::DuplicateMemberError e) { x.setFirst(OrderedPair::DEFAULT_VALUE); x.setSecond(OrderedPair::DEFAULT_VALUE); } */ cout << "The resulting OrderedPair: "; x.print(); cout << endl; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!