Question

In: Computer Science

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

}

Solutions

Expert Solution

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){
   int i=0;  
   while(i<size){  
       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() {
   //Declare variables and pointers
   int numberOfTeams;
   int *wins;
   string *names;
   //Read from user number of teams
   cout << "How many teams will be there? ";
   cin >> numberOfTeams;
  
   //Create dynamic array with the number of teams as size
   //'new' will help to allocate memory for a data structure or data type on heap
   names = new string[numberOfTeams];  
   wins = new int[numberOfTeams];
  
   //Call methods
   initializeArrays(names,wins,numberOfTeams);  
   sortData(names,wins,numberOfTeams);  
   displayData(names,wins,numberOfTeams);
  
   //After execution of methods free up the memory used by the dynamic arrays
   //Using delete[]
   delete[] names;
   delete[] wins;
   return 0;
}

###########################################################

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


Related Solutions

Having trouble with this assignment: Rewrite your most recent high scores program (shown below) so that...
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,...
1.This C++ assignment regards pointers and the league with DMA. First part asks to perform tasks...
1.This C++ assignment regards pointers and the league with DMA. First part asks to perform tasks using pointers. The instructions below are a sequence of tasks that are loosely related to each other. Start the assignment by creating a file names pointerTasks.cpp with an empty main function , then add the statements in main() to complete each of the tasks listed below. Some tasks only require a single C++ statement while others will require more. For each step, include a...
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...
Rewrite Program to store the pairs of states and capitals so that the questions are displayed...
Rewrite Program to store the pairs of states and capitals so that the questions are displayed randomly. import java.util.*; public class quiz { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String arr[][]= {{"Alabama","Montgomery"},{"Alaska","Juneau"},{"Arizona","Phoenix"}}; int n =arr.length; int count=0; for(int i=0;i<n;i++) { System.out.printf("What is the capital of %s? ",arr[i][0]); String capital=sc.next(); if (arr[i][1].equalsIgnoreCase(capital)) { count++; System.out.println("Your answer is correct"); } else { System.out.printf("The correct answer should be %s\n",arr[i][1]); } } System.out.printf("The correct count is %d",count); } }
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...
Suppose that a system uses DMA for data transfer from disk controller to main memory. Further...
Suppose that a system uses DMA for data transfer from disk controller to main memory. Further assume that it takes t1 nsec on average to acquire the bus and t2 nsec to transfer one word over the bus (t1 >> t2). After the CPU has programmed the DMA controller, how long will it take to transfer 1000 words from the disk controller to main memory, if (a) word-at-a-time mode is used? (b) burst mode is used? Assume that commanding the...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
Rewrite your program for part 1. Do not declare the array globally, declare it in the...
Rewrite your program for part 1. Do not declare the array globally, declare it in the loop function. This now requires that you add two parameters to your fill array and print array functions. You must now pass the array name and array size as arguments, when the program calls these functions. The program has the same behavior as problem 1, but illustrates the difference between globally and locally declared variables. The program code for part 1 was: int Array[15]...
The third task is to further refine the program so that it uses a loop to...
The third task is to further refine the program so that it uses a loop to keep asking the user for numbers until the user gets the correct number. HINT: You need functions eight from the sample code (see previous activity). Steps Please use Netbeans to revise your NumberGuessGame program to satisfy the above requirements. Please submit a print screen of your successful code including the Output window with the "Build Successful" message to this assignment.
In this assignment, you will select a program, quality improvement initiative, or other project from your...
In this assignment, you will select a program, quality improvement initiative, or other project from your place of employment. Assume you are presenting this program to the board for approval of funding. Write an executive summary (850-1,000 words) to present to the board, from which they will make their decision to fund your program or project. The summary should include: 1. The purpose of the program or project. 2. The target population or audience. 3. The benefits of the program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT