Chapter 13 Exercise Set 2: It’s War!

We will be modifying the Card objects we created in the Vectors of objects chapter, using the same Cards.h and Cards.cpp files. The new Deck objects will be added there too.

  1. Rewrite the find_card function from Searching as a Deck member function that has a Card parameter.

  2. Write a test_random.cpp that has #include "random.h" at the top and has several calls to random_between in its main function designed to test it.

    Add a series of tests that evaluate the simulation of a roll of 6 sided die. Don’t forget to call srand so that you get a different sequence of numbers each time you run your program.

    Test the handling the case where l is greater than h. so that both random_between(3, 6) and random_between(6, 3) return an integer between 3 and 6 inclusive.

  3. Add a modifier member function to the Deck structure named swap_cards that takes integers as arguments (the indices of the two cards) and swaps the cards at these indices.

  4. Add a member function to the Deck structure with prototype:

    int find_lowest_between(int l, int h);
    

    that returns the index of the lowest card in the deck between index l and index h inclusive.

  5. Add a modifier member function to the Deck structure named sort that uses the selection sort algorithm sketched out in the Sorting section to put the deck’s cards into sorted order.

  6. Complete the merge function sketched out in the Merge sort section. Then use it to complete the first version of merge_sort described in the section.