Question
#include #include using namespace std; void initializeArrays(string names[], int scores[], int size) { for(int i = 0; i < size; i++) { cout < <
#include
#include
using namespace std;
void initializeArrays(string names[], int scores[], int size)
{
for(int i = 0; i < size; i++)
{
cout << "Enter the name for score #" << i+1 << ": "; cin >> names[i];
cout << "Enter the score for score #" << i+1 << ": "; cin >> scores[i];
}
}
void sortData(string names[], int scores[], int size)
{
for(int i = size-1; i > 0; i--)
for(int j = 0; j < size-1; j++)
{
if(scores[j] < scores[j+1])
{
swap(names[j], names[j+1]);
swap(scores[j], scores[j+1]);
}
}
}
void displayData(const string names[], const int scores[],int size)
{
cout << " Top scores: ";
for(int i = 0; i < size; i++)
cout << names[i] << ": " << scores[i] << endl;
}
int main()
{
int i;
cout << "How many scores will you enter?: "; cin >> i;
int* scores = new int[i];
string* names = new string[i];
initializeArrays(names, scores, i);
sortData(names, scores, i);
displayData(names, scores, i);
delete[] scores;
delete[] names;
}
I need Rewrite the program above 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.
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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started