In: Computer Science
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 structs. You must ask the user how many teams are in the league to determine how big this array needs to be and then dynamically allocate memory for it using the new operator. It must deallocate the memory when it is done with the array using delete [].
3. Your program must use three functions that accept the array of WinRecord structs by address (i.e., pass a WinRecord pointer):
void initializeData(WinRecord* standings, int size) void sortData(WinRecord* standings, int size) void displayData(WinRecord* standings, int size)
4. Note that the name field of each WinRecord struct is just a char* which you need to use to store a C-String. For this assignment, you must use C-strings, not C++ string objects. Unlike a C++ string object, the memory to store the actual character array for the C-String is not allocated for you automatically! I encourage you to develop a function to do this on your own, but I have provided the getLine() function (Links to an external site.)getLine.cpp (Links to an external site.) to use if you wish. Note that this function returns a line of text from the keyboard contained in a dynamically allocated array. You will thus need to deallocate this memory using delete [] when you are done using any arrays you allocated using this function. Towards the end of main() is a reasonable place to do this allocation. Note that this is in addition to deallocating the array of WinRecord structs discussed in step 2 above!
#include <iostream>
using namespace std;
void initializeArrays(string names[],int wins[],int size){
int i=0;
while(i<5){
cout<<"Enter team #"<<i+1<<":";
cin>>names[i];
cout<<"Enter wins for team #"<<i+1<<":";
cin>>wins[i];
i++;
}
}
void sortData(string names[],int wins[],int size){
int max=0;
for(int i=0;i<size-1;i++){
for(int j=0;j<size-i-1;j++){
if(wins[j]<wins[j+1]){
int temp = wins[j];
wins[j] = wins[j+1];
wins[j+1] = temp;
string t = names[j];
names[j] = names[j+1];
names[j+1] = t;
}
}
}
}
void displayData(string names[],int wins[],int size){
cout<<"\nLeague Standings:\n";
int num=0;
while(num<size){
cout<<names[num]<<":
"<<wins[num]<<"\n";
num++;
}
}
int main() {
string names[5];
int wins[5];
initializeArrays(names,wins,5);
sortData(names,wins,5);
displayData(names,wins,5);
}
OUTPUT:
Enter team #1:Padres
Enter wins for team #1:71
Enter team #2:Dodgers
Enter wins for team #2:55
Enter team #3:Rays
Enter wins for team #3:57
Enter team #4:Rockies
Enter wins for team #4:46
Enter team #5:Diamondbacks
Enter wins for team #5:40
League Standings:
Padres: 71
Rays: 57
Dodgers: 55
Rockies: 46
Diamondbacks: 40