Question

In: Computer Science

Rewrite your most recent high scores program in C++ so that each name/score pair is stored...

Rewrite your most recent high scores program in C++ 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.

Please help!! Heres my code below from the previous assignment that it asks for thank you!

#include

#include <iostream>

#include <cstdlib>

using namespace std;

void readData(int *scores, string *names, int n);

void sortData(int *scores, string *names, int n);

void displayData(int *scores, string *names, int n);

int main()

{

int n;

   cout<<"How many scores will you enter?: ";

   cin>>n;

int *scores = new int[n];

string *names = new string[n];

readData(scores, names, n);

sortData(scores, names, n);

  

displayData(scores, names, n);

delete [] scores;

delete [] names;

return 0;

}

void readData(int *scores, string *names,int n)

{

for (int i=0; i<n; 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(int *scores, string *names, int n)

{

int i, j, size_t;

for (i = 0; i < n-1; i++)

{

size_t = i;

for (j = i+1; j < n; j++)

if (scores[j] > scores[size_t])

size_t = j;

string tempName = names[size_t];

names[size_t] = names[i];

names[i] = tempName;

int tempScore = scores[size_t];

scores[size_t] = scores[i];

scores[i] = tempScore;

}

}

void displayData(int *scores, string *names, int n){

   cout<<"Top Scorers: "<<endl;

   for(int i=0; i<n;i++){

   cout<<names[i]<<" : "<<scores[i]<<endl;

   }

}

Solutions

Expert Solution

Please find your solution below and if doubt comment and do upvote.

CODE:

#include <iostream>
#include <cstdlib>
using namespace std;
//structure deifinition
struct Highscore{
    int score;
    char name[24];
};
//function prototypes
void readData(Highscore scores[], int size);
void sortData(Highscore scores[], int size);
void displayData(const Highscore scores[], int size);
//swap function 
void swap(Highscore *x, Highscore *y)  
{  
    Highscore temp = *x;  
    *x = *y;  
    *y = temp;  
}  
int main()
{

   int n;

   cout<<"How many scores will you enter?: ";

   cin>>n;
   
   //create array of structure
   struct Highscore *scores=new struct Highscore[n];

   readData(scores,n);

   sortData(scores,n);
  
   displayData(scores,n);

   delete [] scores;

   return 0;

}
//read the user input data
void readData(Highscore scores[], int size)
{
    for (int i=0; i<size; i++)
    {
        cout<<"Enter the name for score #"<<(i+1)<<": ";
        
        cin>>scores[i].name;
        
        cout<<"Enter the score for score #"<<(i+1)<<": ";
        
        cin>>scores[i].score;
      
        
    }

}
//sort the data using selection sort
void sortData(Highscore scores[], int size)
{
   int n=size;
   int i, j;
   int minIndex;
   for (i = 0; i < n-1; i++)
   {
       minIndex=i;
       for (j = i+1; j < n; j++)
       {
           if(scores[j].score<scores[minIndex].score)
           {
               minIndex=j;
           }
       }
     swap(&scores[minIndex],&scores[i]);
   }

}
//dispay the data
void displayData(const Highscore scores[], int size){

   cout<<"Top Scorers: "<<endl;

   for(int i=0; i<size;i++){

   cout<<scores[i].score<<" : "<<scores[i].name<<endl;

   }

}

OUTPUT:


Related Solutions

Having trouble with this assignment: Rewrite your most recent high scores program (shown below) so that...
Having trouble with this assignment: Rewrite your most recent high scores program (shown below) 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,...
Rewrite the league application so that each team/wins pair is stored in a struct named WinRecord....
Rewrite the league application so that each team/wins pair is stored in a struct named WinRecord. Your program must meet the following requirements: 1. The WinRecord struct must have two fields: an int named wins, and a char* named name. name will point to a dynamically allocated array of characters, see requirement 4 below. 2. Instead of using two parallel arrays like Assignment 6, the data must be stored in a single array -- a dynamically allocated array of WinRecord...
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each...
In C++ Modify the program #1 to allow the user to enter name-score pairs. For each student taking a test, the user types a string representing the name of the student, followed by an integer representing the student's score. (use a structure) Modify the average-calculating function so they take arrays of structures, with each structure containing the name and score of a single student. In traversing the arrays, use pointers notation rather than array indices. (myArray->name or *(myArray).name) Make it...
The League with DMA Rewrite your League program from Assignment 8 so that it uses Dynamic...
The League with DMA Rewrite your League program from Assignment 8 so that it uses Dynamic Memory Allocation (DMA) to create the team names and scores arrays. This is a good test of the modularity of your program. You will only need to make slight modifications to your main() function if you wrote your original program using functions similar to the following: void initializeData(string names[], int wins[], int size) void sort(string names[], int wins[], int size) void display(string names[], int...
C LANGUAGE Ask user how many scores there are, then ask for each score. Then calculate...
C LANGUAGE Ask user how many scores there are, then ask for each score. Then calculate the average score and scores below 60. Then display the average score and number of scores below 60
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template....
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template. And rewrite your main function to test your template for integer array and double array. Following is my complete code: #include <iostream> using namespace std; class Array { private: // Pointer to memory block to store integers int* data; // Maximum size of memory block int cap; // Stores number of integers in an array int num; public: // Constructor Array(int size); // Default...
Using c++, write a program that will display your name as a void function then will...
Using c++, write a program that will display your name as a void function then will perform the following by user-defined functions: a. to compute for the sum of two numbers (n1, n2) using function.
Write C program : Enter N and N pairs of positive integerpairs, convert each pair...
Write C program : Enter N and N pairs of positive integer pairs, convert each pair of positive integers into binary and add, and output the formula. Decimal to binary function prototype: int DecToBin(int integer);The range of input N is 1 <= N <= 100, and the range of positive integers input is 0 < n <= 255. The output format is %08d. If the entered N or positive integer is out of range, "Invalid input" is output. If a...
Write a C++ Program to print the first letter of your first and last name using...
Write a C++ Program to print the first letter of your first and last name using stars. Note: 1) Using nested For Loop 2) The number of lines is given by user. 3) Using one Outer loop to print your letters. 4) Print the letters beside each other.
C++ program to perform each of the area calculations in separate functions. Your program will take...
C++ program to perform each of the area calculations in separate functions. Your program will take in the relevant information in the main (), call the correct function that makes the calculation, return the answer to the main () and then print the answer to the screen. The program will declare a variable called “choice” of type int that is initialized to 0. The program will loop while choice is not equal to 4. In the body of the loop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT