he data must be stored in two dynamic arrays: an array of strings named names, and an array of ints named scores. These arrays must be declared and allocated using "new" in the main function. The user input of the names and scores should be done in a function named readData(). It should have the following signature: void readData(string names[], int scores[], int size) You must also write two more functions: one to sort both arrays in descending order by score, and one to display the final list of names and scores. They should have the following signatures. void sortData(string names[], int scores[], int size) void displayData(const string names[], const int scores[], int size) The main function should be very short. It should just get the number of scores, allocate the two arrays, invoke these three functions, and then deallocate the arrays. Some of you may not have studied sorting algorithms. Sorting is covered in lesson 9, section 9.6. You can copy as much code from that lesson as you like. You may use C++'s built-in swap() function, but do not use C++'s sort() function. #include #include 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; } /* This function will get the user inputs* and populate those values into an array*/ 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]; } } /* This function will sort the array based on scores*/ 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; } } } } // This function will display the output 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;
he data must be stored in two dynamic arrays: an array of strings named names, and an array of ints named scores. These arrays must be declared and allocated using "new" in the main function.
The user input of the names and scores should be done in a function named readData(). It should have the following signature:
void readData(string names[], int scores[], int size)
You must also write two more functions: one to sort both arrays in descending order by score, and one to display the final list of names and scores. They should have the following signatures.
void sortData(string names[], int scores[], int size) void displayData(const string names[], const int scores[], int size)
The main function should be very short. It should just get the number of scores, allocate the two arrays, invoke these three functions, and then deallocate the arrays.
Some of you may not have studied sorting
#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;
}
/* This function will get the user inputs* and populate those values into an array*/
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];
}
}
/* This function will sort the array based on scores*/
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;
}
}
}
}
// This function will display the output
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 4 steps with 3 images









