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.
Rewrite the
find_cardfunction from Searching as aDeckmember function that has aCardparameter.Reread the discussion in Random numbers and use that to write a function named
random_between. Create a header file,random.h, with the following:Then create a
random.cppthat has the body of the function. The parameterslandhrepresent the lower and upper ends of a range or integers. Your function should return random integers betweenlandhinclusive. Note: Your function should not concern itself with the need to seedrandby callingsrand. Callsrandin themainfunction.Write a
test_random.cppthat has#include "random.h"at the top and has several calls torandom_betweenin itsmainfunction designed to test it. Try simulating the roll of a 6 sided die. Don’t forget to callsrandso that you get a different sequence of numbers each time you run your program.Handle the case where
lis greater thanhby swappinglandhso that bothrandom_between(3, 6)andrandom_between(6, 3)return an integer between 3 and 6 inclusive.Add a modifier member function to the
Deckstructure namedswap_cardsthat takes integers as arguments (the indices of the two cards) and swaps the cards at these indices.Add a member function to the
Deckstructure with prototype:int find_lowest_between(int l, int h);
that returns the index of the lowest card in the deck between index
land indexhinclusive.Add a modifier member function to the
Deckstructure namedsortthat uses the selection sort algorithm sketched out in the Sorting section to put the deck’s cards into sorted order.Complete the
mergefunction sketched out in the Merge sort section. Then use it to complete the first version ofmerge_sortdescribed in the section.