Practice Programs can generally be solved with a short program that directly applies the programming principles presented in this chapter.
Write a program in which you declare a deque
to store values of type double
, read in ten double
numbers, and store them in the deque
. Then call the generic sort
function to sort the numbers in the deque
and display the results.
Write a program that uses the map
template class to compute a histogram of positive numbers entered by the user. The map
’s key should be the number that is entered, and the value should be a counter of the number of times the key has been entered so far. Use −1 as a sentinel value to signal the end of user input. For example, if the user inputs:
5
12
3
5
5
3
21
−1
then the program should output the following (not necessarily in this order):
The number 3 occurs 2 times.
The number 5 occurs 3 times.
The number 12 occurs 1 times.
The number 21 occurs 1 times.
Given a variable of type string
set to arbitrary text, write a program that uses the stack
template class of type char
to reverse the string.
You have a list of student ID’s followed by the course number (separated by a space) that the student is enrolled in. The listing is in no particular order. For example, if student 1 is in CS100 and CS200 while student 2 is in CS105 and MATH210 then the list might look like this:
1 CS100
2 MATH210
2 CS105
1 CS200
Write a program that reads data in this format from the console. If the ID is −1 then stop inputting data. Use the map
template class to map from an integer (the student ID) to a vector
of type string
that holds each class that the student is enrolled in.
After all data is input, iterate through the map and output the student ID and all classes stored in the vector for that student. The result should be a list of classes organized by student ID.
If you aren’t using C++11 or higher then don’t forget that you need a space between the >> characters when defining the map of vectors.