Programming Exercise: Lists & Recursion - Intermediate
In this exercise we will play with STL lists and write some useful
recursive functions.
3 points
- Declare two lists in your main function and add 10 elements to each.
- Write a recursive function list<int> sums(const
list<int>& l1, list<int>& l2) that takes two
lists of integers and returns a list that contains the pairwise sum of
those ints. For example, the lists [1, 2, 3] and [4, 5, 6] would sum
to [5, 7, 9]. (Note: you should make sure that the lists are the same
size. You may return an empty list if they are not.)
- Write a recursive function list<int> genSquares(unsigned
int max) that generates a list of all of the squares of ints up to
the value max.
- Write a recursive function list<int>
keepEvens(list<int> l) that takes a list and returns all of
the elements of the list that are even, in order. For example, the
list [2, 1, 5, 4, 9, 6, 8, 11, 14] would return [2, 4, 6, 8, 14].
Extra Credit Options
- 2 points: Rewrite all of these functions to pass their
lists by reference and leave the lists intact after the recursive calls.
- 1 points: Rewrite all of these functions only using
slists rather than lists. Are any of your operations
made inefficient now? Which?
- 1 points: Rewrite all of these functions only using
vector rather than list. Are any of your operations
made inefficient now? Which?