Chapter 13 Exercise Set 0: Chapter Review¶
Add the
Deck
object developed in this chapter to theCards.h
andCards.cpp
files you created in Chapter 13 Exercise Set 0: Chapter Review.Rewrite the
find_card
function from Searching as aDeck
member function that has aCard
parameter.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:// Returns positive pseudorandom integer between l and h inclusive int random_between(int l, int h);
Then create a
random.cpp
that has the body of the function. The parametersl
andh
represent the lower and upper ends of a range or integers. Your function should return random integers betweenl
andh
inclusive. Note: Your function should not concern itself with the need to seedrand
by callingsrand
. Callsrand
in themain
function.Write a
test_random.cpp
that has#include "random.h"
at the top and has several calls torandom_between
in itsmain
function designed to test it. Try simulating the roll of a 6 sided die. Don’t forget to callsrand
so that you get a different sequence of numbers each time you run your program.Handle the case where
l
is greater thanh
by swappingl
andh
so that bothrandom_between(3, 6)
andrandom_between(6, 3)
return an integer between 3 and 6 inclusive.Add a modifier member function to the
Deck
structure namedswap_cards
that takes integers as arguments (the indices of the two cards) and swaps the cards at these indices.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 indexh
inclusive.Add a modifier member function to the
Deck
structure namedsort
that uses the selection sort algorithm sketched out in the Sorting section to put the deck’s cards into sorted order.Complete the
merge
function sketched out in the Merge sort section. Then use it to complete the first version ofmerge_sort
described in the section.