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...
Note: Please rewrite it and it is plagiarized so please remove the plagiarism from it ....
Note: Please rewrite it and it is plagiarized so please remove the plagiarism from it . Business environment in UAE – The UAE Country has a lively economy. It is worth noting the amount of revenue generated from oil and gas tariffs. Effective efforts have been made to move away from the reliance on hydrocarbons and a strong mechanical foundation has been built, along with a very strong portion of the administration. The basis of free zones has been an...
Note: Please rewrite it and it is plagiarized so please remove the plagiarism from it ....
Note: Please rewrite it and it is plagiarized so please remove the plagiarism from it . Contracts Under labor Laws of UAE. :-- This is necessary to know which is the official language of the UAE Country is Arabic, so all contracts are made in Arabic and English. Workers and employees should understand the contract before signing. According to the UAE Labor Law, these are divide into Pvt. and Public Sectors. The Public Sector Act is governed by Cabinet Decision...
Using c# rewrite/edit the following program so that it runs the simulation 10,000 times (play the...
Using c# rewrite/edit the following program so that it runs the simulation 10,000 times (play the games 10,000 times) and count the number wins. Then calculate the winning probability by using the formula: the expected winning probability = the number of wins / 10,000 using System; class lottery { static void Main() { int n, random, choice = 1; Random randnum = new Random();    while (choice == 1) {    Console.Write("\nEnter a integer from 1 to 5:"); n =...
*****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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT