In: Computer Science
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 wins[], int size)
Your modified league program should start out by asking the user how many teams will be entered. It should then dynamically allocate two appropriate arrays, and proceed just like the original League assignment, calling the above three functions. When your program is done using the arrays, don't forget to use delete [] to return their memory to the system.
Note: you must use dynamic memory allocation for your arrays, i.e. the new and delete [] operators, to receive credit.
The output from your program should look approximately like this (user input in orange bold):
How many teams will you enter?: 4 Enter team #1: Padres Enter the wins for team #1: 75 Enter team #2: Dodgers Enter the wins for team #2: 91 Enter team #3: Giants Enter the wins for team #3: 92 Enter team #4: Cubs Enter the wins for team #4: 65 League Standings: Giants: 92 Dodgers: 91 Padres: 75 Cubs: 65
THE CODE FOR ASSIGNMENT 8:
#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);
}
Here is the answer for your question in C++ Programming Language.
Kindly upvote if you find the answer helpful.
####################################################################
CODE :
#include <iostream> using namespace std; void initializeArrays(string names[],int wins[],int
size){ void sortData(string names[],int wins[],int size){ } void displayData(string names[],int wins[],int
size){ int main() { |
###########################################################
SCREENSHOTS :
Please see the screenshots of the code below for the indentations of the code.
######################################################################
OUTPUT :
Any doubts regarding this can be explained with pleasure :)