In: Computer Science
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:
void readData(Highscore scores[], int size) void sortData(Highscore scores[], int size) void displayData(const Highscore scores[], int size)
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;
}
}
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: