Skip to main content

Section 23.16 Exercises

Checkpoint 23.16.1.

Write a templated function doubleIt with one parameter that doubles the value by adding it to itself and returns the answer.
For example: doubleIt(3.5) should produce 7.0 doubleIt("hello") should produce "hellohello"

Checkpoint 23.16.2.

Write a templated function sumList that will take in a vector of the template type. It should sum up all the elements using + then return the total.
Hint.
Use T sum{}; to default initialize a sum variable of any type.

Checkpoint 23.16.3.

Write a templated function countMatches that will take in a vector of the template type and a value of the same type. It should return the number of elements in the vector that match the given value.
Hint.
Your return type wonโ€™t be T, it will always be an integer.

Checkpoint 23.16.4.

Make a templated struct ItemCounter. An item counter should store item, a value of the templated type, and an int count, representing the number of that item. For instance, an ItemCounter<string> could store the string "apple" and the number 3.
Hint.
Your return type wonโ€™t be T, it will always be an integer.

Checkpoint 23.16.5.

Class ItemCounter has been declared. Write the constructor and all three methods for the class. A newly constructed ItemCounter should have a count of 0.
Hint.
You built the logic in Checkpointย 23.13.1.

Checkpoint 23.16.6.

Implement the ArrayListโ€™s removeAt method. (Class listing is in Sectionย 23.17
Hint.
You built the logic in Checkpointย 23.13.1.
You have attempted of activities on this page.