In: Computer Science
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, making the maximum length of the name 23. (If you prefer to use a char pointer and a dynamically allocated array, that is fine as well. However, this may result in a number of complications, so be prepared for the challenge.)
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 initializeData(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. Getting this to work correctly if the user enters names that are too long -- that is, making it so that you put the first 23 characters in the name variable and ignore the remaining characters on the line -- is complicated. You can do this as an extra challenge if you want, but it's not required.
My high score program:
#include<iostream>
using namespace std;
int main()
{
int num;
int i;
int j;
int tmp;
string t;
cout << "How many scores will you enter?: ";
cin >> num;
int *a = new int [num];
string *b = new string [num];
for(i=0;i<num;i++){
cout << "Enter the name for score #" << i+1 << ": ";
cin >> b[i];
cout << "Enter the score for score #" << i+1 << ": ";
cin >> a[i];
}
cout << "\nTop Scorers:" << endl;
for(i=0;i<num;i++){
for(j=i+1;j<num;j++){
if(a[i]<a[j]){
tmp=a[i];
t=b[i];
a[i]=a[j];
a[j]=tmp;
b[i]=b[j];
b[j]=t;
}
}
}
for(i=0;i<num;i++){
cout<<b[i]<<": "<<a[i]<<endl;
}
}
#include<iostream>
using namespace std;
struct highscore//struct declaration with name highscore
{
int score;//to store score
char name[23];//to store name
struct highscore *next;//to link next struct
};
struct highscore* initializeData(struct highscore *h, int
size)
{
int i,n=size;
for(i=0;i<n;i++)
{
struct highscore *t = new
highscore();
cout << "Enter the name for
score #" << i+1 << ": ";
cin >> t->name;
cout << "Enter the score for
score #" << i+1 << ": ";
cin >> t->score;
t->next = h;
h=t;
}
return h;
}
struct highscore* sortData(struct highscore *h,int size)//sorting
using selection sort
{
struct highscore *temp =
h,*r=h,*min=h,*mprev,*prev=h,*l=h;
int i=0;
while(i<size)
{
temp = r;
min =r;
mprev =r;
prev =r;
// cout<<"Hello 1\n";
while(temp!=NULL)
{
if(min->score<temp->score)
{
mprev = prev;
min = temp;
}
prev =
temp;
//
cout<<temp->score<<" Hello 3\n";
temp =
temp->next;
}
// cout<<"Hello 2\n";
if(mprev == min)
{
r=r->next;
}
else
{
mprev->next =
min->next;
min->next=NULL;
min->next =
r;
h=min->next;
}
i++;
// cout<<"Hello 2\n";
}
return h;
}
void displayData(struct highscore *h,int size)
{
struct highscore *temp = h;
while(temp!=NULL)
{
cout<<"name:
"<<temp->name<<"\n"<<"score:
"<<temp->score<<"\n\n";
temp=temp->next;
}
}
int main()
{
int n,i;
struct highscore *h=NULL;//initially empty
cout << "How many scores will you enter?:
";
cin >> n;
h=initializeData(h,n);
h=sortData(h,n);
displayData(h,n);
return 0;
}
ouput:-