Question

In: Computer Science

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 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

Solutions

Expert Solution


Related Solutions

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: 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...
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...
The National Basketball League is a professional sports league in North America. Each team in the...
The National Basketball League is a professional sports league in North America. Each team in the league must participate the revenue sharing system. For the purposes of the revenue sharing plan, there are no limits to the amount each team may contribute to the plan. Each team is subject to a receipt limits based their designated market area (DMA). For teams with a DMA between 1.5-2.249 households, the final receipt limit is set at 75% of the initial receipt; for...
The English Premier League has 20 teams, throughout the season, each team plays each of the...
The English Premier League has 20 teams, throughout the season, each team plays each of the other teams once at home and once away. (e.g. Everton plays Liverpool at Anfield, which is Liverpool’s home field, and Everton plays Liverpool at Goodison Park, which is Everton’s home field.) Teams are awarded 3 points for a win, 1 point for a draw, and 0 points for a loss (a) How many combinations of wins, losses, and draws are possible for a team...
1. When a league expands, each new team usually gets to select players from the rosters...
1. When a league expands, each new team usually gets to select players from the rosters of existing teams. Existing teams, however, get to “protect” a certain number of players on their rosters from such a draft. Assuming players differ in “quality,” explain why such an expansion draft virtually guarantees that the new team(s) will be of inferior average quality than existing teams. What will happen to the overall league average in terms of quality? 2. An expansion team in...
Modify the following 'MessageBoxes' application so it uses a single action listener for each button. This...
Modify the following 'MessageBoxes' application so it uses a single action listener for each button. This will require you to separate the single action listener logic into multiple listeners, one for each button. Then modify the code to provide additional options to two or more buttons. /* * The source code for this assignment started with * a sample from "Thinking in Java" 3rd ed. page 825 * by Bruce Eckel. I have finished adding the rest of the action...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT