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.

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter8: Arrays
Section: Chapter Questions
Problem 9PE
icon
Related questions
Question

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:

  1. 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.
  2. The data should be stored in a single array, a dynamically allocated array of Highscore structs.
  3. 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)
  4. 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.
  5. 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.
  6. You may assume that the user enters names that are 23 characters or less.
  7. 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;

        }

    }

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 6 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT