Question

In: Computer Science

Implement a method that does the following: (a) Create a Map data structure to hold the...

Implement a method that does the following:
(a) Create a Map data structure to hold the associations between player name (key) and team affiliation (value)

(b) Add at least 5 mappings to the map

(c) Print out the current roster of mappings

(d) Assuming some time has passed, change some of the mappings to simulate the players changing their affiliations

(e) Again, print out the current roster of mappings
3. Implement a main method that calls the above methods, demonstrating their use with all applicable printed output

Solutions

Expert Solution

code:

#include<bits/stdc++.h>
using namespace std;

void display(map<string,int> &m) //it displays the map elements
{
   map<string, int>::iterator itr;
   for (itr = m.begin(); itr != m.end(); ++itr) {
cout << '\t' << itr->first<< '\t' << itr->second << '\n';
}
}

void add(map<string,int> &m,string s,int n) //it adds elements in map
{
   m[s]=n;
}

void update(map<string,int> &m,string s,int n) //it updates the elements in map
{
   m[s]=n;
}


int main()
{
   map<string,int> m;
   add(m,"john",1);
   add(m,"varun",2);
   add(m,"raman",3);
   add(m,"joseph",4);
   add(m,"vijay",5);
  
   cout<<"Before update"<<endl;
   display(m);
   update(m,"john",2);
   update(m,"varun",1);
   cout<<"after update"<<endl<<endl;
   display(m);
  
}

output


Related Solutions

Racket/Scheme language [15p] create-mapping A map data structure is described as either a set of key-value...
Racket/Scheme language [15p] create-mapping A map data structure is described as either a set of key-value pairs or a set of keys and a set of values whose relation is described by a function with a one-to-one mapping from keys to values. Write a function, create-mapping, which takes a list of symbols (keys) and a list of any type of Scheme values (vals) and returns a function that takes one symbol argument (the key) and returns the corresponding value. The...
0. Introduction. This laboratory assignment involves implementing a data structure called a map. A map associates...
0. Introduction. This laboratory assignment involves implementing a data structure called a map. A map associates objects called keys with other objects called values. It is implemented as a Java class that uses arrays internally. 1. Theory. A map is a set of key-value pairs. Each key is said to be associated with its corresponding value, so there is at most one pair in the set with a given key. You can perform the following operations on maps. You can...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called keys with other objects called values. It is implemented as a Java class that uses arrays internally. 1. Theory. A map is a set of key-value pairs. Each key is said to be associated with its corresponding value, so there is at most one pair in the set with a given key. You can perform the following operations on maps. You can test if...
Create a class BankAccount to hold at least the following data / information about a bank...
Create a class BankAccount to hold at least the following data / information about a bank account: Account balance Total number of deposits Total number of withdrawals Interest rate e.g., annual rate = 0.05 Service charges per month The class should have the following member functions: Constructor To set the required data. It may be parameterized or user’s input. depositAmount A virtual function used to accept an argument for the amount to be deposited. It should add the argument (amount)...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
CS 209 Data Structure 2. Create a method that takes a HashMap and returns the sum...
CS 209 Data Structure 2. Create a method that takes a HashMap and returns the sum of the keys of the HashMap. 3. Create a method that takes a HashMap and returns the sum of all keys and values of the HashMap. For example, if the input is [1=9, 3=6, 4=9, 6=8, 7=6] then the method should return 59.
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns...
CS 209 Data Structure 1. Create a method that takes an ArrayList of Integer and returns a sorted copy of that ArrayList with no duplicates. Sample Input: {5, 7, 4, 6, 5, 6, 9, 7} Sample Output: {4, 5, 6, 7, 9}
Develop a Java application to implement a binary tree data structure. A tree data structure starts...
Develop a Java application to implement a binary tree data structure. A tree data structure starts from the top node, called the root. Each node in the tree has a set of children, which are also nodes of the tree and can have children of their own, and so on. This keeps on going till we get to the bottom of the tree, to the “leaf” nodes. Each node in the tree, except for the root, has a parent. A...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT