Question: C++ Assignment 8.0 [no points, but must be completed in preparation for assignment 8.1] Rewrite your most recent high scores program so that each name/score
C++
Assignment 8.0 [no points, but must be completed in preparation for assignment 8.1]
Rewrite your most recent high scores program so that each name/score pair is stored in a struct named highscore. Except as noted below, this new program will continue to meet all of the requirements of your most recent high scores program. Your new program should meet the following requirements:
The highscore struct should have two fields:
an int named score
and a char array named name. The char array should have 24 elements, making the maximum length of the name 23. (If you prefer to use a char pointer and a dynamically allocated array, that is fine as well. However, this may result in a number of complications, so be prepared for the challenge.)
The data should be stored in a single array, a dynamically allocated array of highscore structs.
Your program should use three functions that accept the array of highscore structs:
void initializeData(highscore scores[], int size) void sortData(highscore scores[], int size) void displayData(const highscore scores[], int size)
You may use any sort algorithm, but I would recommend using the selection sort from lesson 9.6. Don't use C++'s sort() function, but you can use the swap() function.
Note that when you swap your array elements, you can swap the entire struct. You don't need to swap the name and the score separately.
You may assume that the user enters names that are 23 characters or less. Getting this to work correctly if the user enters names that are too long -- that is, making it so that you put the first 23 characters in the name variable and ignore the remaining characters on the line -- is complicated. You can do this as an extra challenge if you want, but it's not required.
Assignment 8.1 [40 points]
You'll only need lesson 19.1 - 19.4 for this part of the assignment.
Rewrite your high scores program that uses a highscore struct -- but use an STL vector instead of an array. (Note, you will be using an STL vector, not the MyVector class developed in lesson 19.) You may use your own solution from the earlier assignment or the posted solution from that assignment.
Clarifications and Additional Requirements:
Documentation is not required for this assignment.
All requirements of the earlier assignment apply. Your program should use three functions that accept the vector of highscore structs (the size parameter from the earlier assignment won't be needed now, since a vector knows its own size). You must use these function headers:
void initializeData(vector& scores) void sortData(vector & scores) void displayData(const vector & scores)
The name field in the struct must still be a c-string
The focus of this assignment is to use iterators. I expect you to 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. 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 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 yourself as you did in the earlier high scores assignment, using iterators to access the items in the vector.
If you are using the indexOfLargest() function from the solution to the previous high scores program -- which I encourage -- you may run into some const/non-const difficulties. The function needs to return a regular iterator (so that the calling function can use it to modify the vector). But the vector passed into the function should be const, which means the iterator passed into the vector should be a const_iterator. But that makes it impossible to return a non-const iterator. To solve this we will have to make the second parameter a regular iterator, even though it could be used to modify the const vector.
In your displayData() function you'll need to use const_iterator instead of iterator. See lesson 19.4.
Here is an example that shows how to use an iterator to access a particular member of a struct:
swap((*iter1).firstmember, (*iter2).firstmember);
This will work just fine, but there is a C++ operator that combines these two (dereference and then select). I works like this:
swap(iter1 -> firstmember, iter2 -> firstmember);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
