Starting from:

$29.99

COP3503C Lab 7 – Maps Solution

Overview
In this assignment you are going to use the map classes from the standard template library to store different types of data. For this assignment, main() is mostly implemented for you; you just need to fill in the blanks.
Description
Part 1
For the first part, you are going to store the results of some randomly generated numbers. A map is a
great data structure for storing information with identifiers, but especially when encountering data that you don’t know about before the program runs. This is where maps can really shine.
An effective method for generating random numbers is provided for you, but here’s what it looks like:
// An "engine" for generating random numbers std::mt19937 random_mt;

// Using that engine generate a random number between the two parameters (inclusively) int Random(int min, int max)
{
uniform_int_distribution<int> dist(min, max); return dist(random_mt);
}

You need to create a function that has the following signature: void RollDice(int numberOfRolls, int numberOfSides);
In this function, you’re going to create a map<key,value> object to store the results of these rolls. The number rolled is the key, and the number of times it is rolled is the value. Your output will look something like this:

Since you will be recording the results that are rolled, how do you get an initiali 0? This isn’t something you would always do necessarily, but for this assignment you should “seed” the map object with a default value for all key/value pairs. So if you were rolling a 6-side die, then all of the keys 1-6 should have a starting value of 0. That way, when a particular result comes up you can easily increment the value connected to a particular key.
Part 2
For this part, you are going to store data in a slightly more complex format. You’re going to load a file that has information about states in the US. The data will in a CSV file, and is structured like this:
State Per capita income Population Median household income Number of households
Mississippi 21036 2994079 39680 1095823
West Virginia 22714 1850326 41059 735375
Arkansas 22883 2966369 41262 1131288
Alabama 23606 4849377 42830 1841217
New Mexico 23683 2085572 44803 760916
Kentucky 23684 4413457 42958 1712094
Idaho 23938 1634464 47861 591587
Etc…

You will need to create a class/structure to store one of these rows of data as an object, and ultimately store all of the states in a map<string,WhateverYouCallYourClass>.
After that, you will need to get input for one of two options: either print out all the key/values in the map object, or do a search for a particular key, and then print the key/value pair based on the search result. The search is case-sensitive (the first letter of the key is capitalized).

If you’re searching for a specific key, you can use the find() function. This function takes in a key (the same data type as what you specified when creating the map), and it returns an iterator to the key/value pair that matches the search term, OR… if the key can’t be found, the iterator returned is equal to end().
The key to going through STL containers is to use iterators. While some objects like vectors and strings store their data contiguously, making simple for loop iteration possible, that’s not true of all containers. For that, you need an iterator. In C++ iterators commonly revolve around permutations of 2 functions: begin(), and end().
Want to start with the first element in a list? That’s begin(). Want to reach the end of the list? That’s just BEFORE end(). Before? Why not on end()? The end() function returns an iterator that is beyond the range of elements. Refer to lecture notes or recordings for examples on using iterators with maps.

More products