Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want code in c++ to Sort records by scores in ascending order from 5 to 0 for game and show the record for players

I want code in c++ to Sort records by scores in ascending order from 5 to 0 for game and show the record for players id,name, score and date of birth and to complete the code below.
/*
Menu:
1 - Add a player
2 - Search a player
3 - Update a student
4 - Delete a player
5 - Sort rank
6 - Display players record
7 - Sort records by scores
8 - Show students by grade
9 - Show students by gender
10 - Show students born in month ##
*/
#include
#include
using namespace std;
//Global variables
const int SIZE=20; //global constant
int count=0; //actual size of array
//Structures Declaration
struct Date
{
int day;
int month;
int year;
};//end of struct Date
struct player
{
string id;
string user;
Date dob;
double score;
string rank; //computed by an internal function
} pl[SIZE]; //global array of struct Student
//Functions Prototypes
int menu(); //bav=
string compute_rank(double score);
void add_record();
void delete_record();
void update_record();
void sort_records();
bool emptyArray();
int find_max();
int search();
void search(char gender);
void show_record(int i);
void print_records();
void print_records(int month);
void print_records(char grade);
int main()
{
int choice;
int Size;
cout << "enter a size : " ;
cin >> Size ;
//Show a menu to the user
do{
choice=menu();
switch (choice)
{
case 1: //Add new student
if (count
add_record();
else
cout<<" ERROR: The array is full.You need to delete items first!";
break;
case 2: //delete a student
if (!emptyArray())
delete_record();
break;
case 3: //update student's score
if (!emptyArray())
update_record();
break;
case 4: //print all records
if (!emptyArray())
print_records();
break;
case 5: //find the max score
if (!emptyArray())
{
int i=find_max();
show_record(i);
}//end if
break;
case 6: //find a student by ID
if (!emptyArray())
{
int i=search();
if (i>=0)
show_record(i);
else
cout<<" Not found!! ";
}//end if
break;
case 7: //sort records by scores
if (!emptyArray())
sort_records();
break;
case 8: //Show students by rank
if (!emptyArray())
{
string rank;
cout<<" Please enter a grade (A/B/C/D/F): ";
cin>>rank; //add do while and check later
print_records(string rank);
}//end if
break;
case 9: cout<<" Thanks for using our program! ";
break;
default: cout<<" Incorrect menu option.";
}//end switch
}while(choice!=9);
return 0;
}//end of main
int menu()
{
int choice;
cout<<" Please choose a task:"
<<" 1 - Add a student"
<<" 2 - Delete a student"
<<" 3 - Update a student"
<<" 4 - View all records"
<<" 5 - Find the max score"
<<" 6 - Find a student by ID"
<<" 7 - Sort records by scores "
<<" 8 - Show students by grade"
<<" 9 - Show students by gender"
<<" 10 - Show students born in month ##"
<<" 11 - Exit"
<<" ***********************************"
<<" >> ";
cin>>choice;
return choice;
}//end of menu
string compute_rank(double score)
{
if (score>=5.0)
return "master grand ";
else if (score>=4.0)
return "master ";
else if (score>=3.0)
return "paltinum";
else if (score>=2.0)
return "gold ";
else
return "silver";
}//end of compute_grade
void add_record()
{
cout<<"Please enter player info: ";
cout<<" ID: "; cin>>pl[count].id;
cout<<"Name: "; cin>>pl[count].user;
cout<<"Date of birth (DD-MM-YYYY): ";
cout<<" Day: "; cin>>pl[count].dob.day;
cout<<"Month: "; cin>>pl[count].dob.month;
cout<<"Year: " ; cin>>pl[count].dob.year;
//check_dob(st[count].dob); //by ref
cout<<"Score: "; cin>>pl[count].score;
//check_score(st[count].score);
pl[count].rank=compute_rank(pl[count].score);
count++;
}//end of add_record
void print_records()
{
for (int i=0;i
show_record(i);
}//end of print_records
void show_record(int i)
{
cout<<" Student "<<(i+1)
<<": \tID :"<
<<" \tName : "<
<<" \tScore :"<
<<" \tDate of Birth (DD-MM-YYYY): "<
<<" ------------------------------------------------";
}//end of show_record
void print_records(int month)
{
for (int i=0;i
if (pl[i].dob.month==month)
show_record(i);
}//end of print records by month
void print_records(string rank)
{
for (int i=0;i
if (pl[i].rank==rank)
show_record(i);
}//end of print_records by rank
void delete_record()
{
int index=search();
if (index<0)
{
cout<<" Not found!! ";
return; //exit from the function
}//end if
//delete if found i.e. index>=0
for (int j=index; j< count; j++) //shifting up
pl[j]=pl[j+1];
count--;
cout<<" player deleted successfully. ";
}//end of delete_record
void update_record()
{//update student's score only
int index=search();
if (index<0)
{
cout<<" Not found!! ";
return; //exit from the function
}//end if
//update if found i.e. index>=0
cout<<" Old score: "<
cout<<" New score: ";
cin>>pl[index].score;
//check_score(st[index].score); //later
pl[index].rank=compute_rank(pl[index].score); //update student's grade
cout<<" player score updated successfully. ";
}//end of update_record
bool emptyArray()
{
if (count==0)
{
cout<<" ERROR: Array is empty!! ";
return true;
}//end if
return false;
}//end of empty Array
/*int find_max()
{
double max=st[0].score;
int max_indx=0;
for (int i=1;i
if (st[i].score>max)
{
max=st[i].score;
max_indx=i;
}//end if
return max_indx;
}//end of find_max
*/
int search()
{//classic search by ID
string id;
cout<<" Please enter a student ID:";
cin>>id;
for (int i=0;i
if (pl[i].id==id)
return i;
return -1; //i.e. not found
}//end of search by ID
void sort_records()
{//sort by scores descendingly max-to-min
bool ordered=false;
player temp;
if (count < 2)
cout<<" Nothing to sort!!";
for(int i = 0; i < count-1 && ordered==false ; ++i)
{
ordered=true;
for(int j = 0; j < count-1;++j)
if(pl[j].score < pl[j+1].score)
{
ordered=false;
temp = pl[j];
pl[j] = pl[j+1];
pl[j+1] = temp;
}//end if
}//end for
cout<<" Scores in descending order:";
print_records();
}//end of sort_records

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Murach's SQL Server 2012 For Developers

Authors: Bryan Syverson, Joel Murach, Mike Murach

1st Edition

1890774693, 9781890774691

More Books

Students also viewed these Databases questions

Question

What are the purposes of promotion ?

Answered: 1 week ago

Question

=+6. Did your solution clearly highlight the main consumer benefit?

Answered: 1 week ago