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 additional 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. 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 readData(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. Be sure to declare your struct above your prototypes, since the word "Highscore" appears in your prototypes.
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 additional 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.
- 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 readData(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.
- Be sure to declare your struct above your prototypes, since the word "Highscore" appears in your prototypes.
Current Code:
#include <iostream>
#include<string>
using namespace std;
// function declarations
void initializeArrays(string names[], int scores[], int size);
void sortData(string names[], int scores[], int size);
void displayData(const string names[], const int scores[], int size);
int main()
{
int size;
cout << "How many scores will you enter?:";
cin >> size;
string* names = new string[size];
int* scores = new int[size];
initializeArrays(names, scores, size);
sortData(names, scores, size);
displayData(names, scores, size);
cin.get();
system("PAUSE");
return 0;
}
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)
{
string temp;
int tempScore;
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (scores[i] < scores[j])
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
tempScore = scores[i];
scores[i] = scores[j];
scores[j] = tempScore;
}
}
}
}
void displayData(const string names[], const int scores[], int size)
{
cout << "Top Scores :" << endl;
for (int i = 0; i < size; i++)
{
cout << names[i] << ": " << scores[i] << endl;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 6 steps with 6 images