Solutions to Selected Exercises
a w a w a w
(Version 7.0) aw
Data Abstraction & Problem Solving with C++
aw aw aw aw aw aw
Seventh Edition a w
Frank M. Carrano aw aw
University of Rhode Island
aw aw aw
Timothy M. Henry aw aw
New England Institute of Technology
aw aw aw aw
, 2
Solution Manual & Test Bank for Data Abstraction & Problem Solving with C++: Walls and Mirrors, 7th Edition by Frank M. Carrano
aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw
Chapter 1 Data Abstraction: The Walls aw aw aw aw aw
1
const CENTS_PER_DOLLAR = 100;
aw aw aw
/** Computes the change remaining from purchasing an item costing dollarC
aw aw aw aw aw aw aw aw aw aw
ost dollars and centsCost cents with d dollars and c cents. Preconditi
aw aw aw aw aw aw aw aw aw aw aw
on: dollarCost, centsCost, d and c are all nonnegative integers and ce
aw aw aw aw aw aw aw aw aw aw aw
ntsCost and c are both less than CENTS_PER_DOLLAR. Postcondition: d a
aw aw aw aw aw aw aw aw aw aw
nd c contain the computed remainder values in dollars and cents respec
aw aw aw aw aw aw aw aw aw aw aw
tively. If input value d < dollarCost, the proper negative values for
aw aw aw aw aw aw aw aw aw aw aw aw
the amount owed in d dollars and/or c cents is returned. */
aw aw aw aw aw aw aw aw aw aw aw
void computeChange(int dollarCost, int centsCost, int& d, int& c);
aw aw aw aw aw aw aw aw
2a
const MONTHS_PER_YEAR = 12;
aw aw aw
const DAYS_PER_MONTH[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
aw aw aw aw aw aw aw aw aw aw aw aw aw aw
/** Increments the input Date values (month, day, year) by one day.
aw aw aw aw aw aw aw aw aw aw aw
Precondition: 1 <= month <= MONTHS_PER_YEAR, aw aw aw aw aw
1 <= day <= DAYS_PER_MONTH[month - 1], except aw aw aw aw aw aw aw
when month == 2, day == 29 and isLeapYear(year) is true. Postcondit aw aw aw aw aw aw aw aw aw aw aw
ion: The valid numeric values for the succeeding month, day,
aw aw aw aw aw aw aw aw aw
and year are returned. */ aw aw aw aw
void incrementDate(int& month, int& day, int& year);
aw aw aw aw aw aw
/** Determines if the input year is a leap year.
aw aw aw aw aw aw aw aw aw
Precondition: year > 0. aw aw aw
Postcondition: Returns true if year is a leap year; false otherwise. */ aw aw aw aw aw aw aw aw aw aw aw
bool isLeapYear(int year);
aw aw
3a
changeAppointmentPurpose(apptDate: Date, apptTime: Time, purpose: string): boolean aw aw aw aw aw aw
{
if (isAppointment(apptDate, apptTime)) cancelAppointment(apptDate
aw aw aw
, apptTime) aw
return makeAppointment(apptDate, apptTime, purpose)
aw aw aw
}
© 2017 Pearson Education, Inc., Hoboken, New Jersey 070
aw aw aw aw aw aw aw aw
30
, 3
3b
displayAllAppointments(apptDate: Date): void aw aw
{
time = START_OF_DAY aw aw
while (time < END_OF_DAY) aw aw aw
if (isAppointment(apptDate, time)) displa
aw aw aw
yAppointment(apptDate, time) aw
time = time + HALF_HOURaw aw aw aw
}
This implementation requires the definition of a new operation
aw aw aw aw aw aw aw aw
displayAppointment()
as well as definitions for the constants START_OF_DAY, END_OF_DAY and HALF_HOUR.
aw a w aw aw aw aw aw aw aw a w
4
// Assume that storeBag is defined and contains your purchased items Bag<std::string>
aw aw aw aw aw aw aw aw aw aw aw aw
fragileBag;
while (storeBag.contains("eggs"))
aw
{
storeBag.remove("eggs"); fragileBag.add("eggs" aw
);
} // end while
a w aw aw
while (storeBag.contains("bread"))
aw
{
storeBag.remove("bread"); fragileBag.add("brea aw
d");
} // end while
a w aw aw
// Transfer remaining items from storeBag to groceryBag; Bag<std::st
aw aw aw aw aw aw aw aw
ring> groceryBag; aw
v = storeBag.toVector();
aw aw
for (int i = 0; i < v.size(); i++) groceryBag.add
aw aw aw aw aw aw aw aw aw
(v.at(i));
© 2017 Pearson Education, Inc., Hoboken, New Jersey 070
aw aw aw aw aw aw aw aw
30
, 4
5
/** Removes and counts all occurrences, if any, of a given string from
aw aw aw aw aw aw aw aw aw aw aw aw aw
a given bag of strings.
aw aw aw aw
@param bag A given bag of strings. @pa
aw aw aw aw aw aw aw
ram givenString A string.
aw aw aw
@return The number of occurrences of givenString that occurred and
aw aw aw aw aw aw aw aw aw aw
were removed from the given bag. */
aw aw aw aw aw aw
int removeAndCount(ArrayBag<std::string>& bag, std::string givenString)
aw aw aw aw
{
int counter = 0; aw aw aw
while (bag.contains(givenString))
aw
{
counter++; bag.remove(givenString aw
);
} // end while
a w aw aw
return counter; aw
} // end removeAndCount
a w aw aw
6
/** Creates a new bag that combines the contents of this bag and a second ba
aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw
g without affecting the contents of the original two bags.
aw aw aw aw aw aw aw aw aw
@param anotherBag The second bag.
aw a w aw aw
@return A bag that is the union of the two bags. */
a w aw aw aw aw aw aw aw aw aw aw
public BagInterface<ItemType> union(BagInterface<ItemType> anotherBag);
aw aw aw
7
/** Creates a new bag that contains those objects that occur in both this
aw aw aw aw aw aw aw aw aw aw aw aw aw
bag and a second bag without affecting the contents of the original two bags. @par
aw aw aw aw aw aw aw aw aw aw aw aw aw aw
am anotherBag The given bag.
aw aw aw aw
@return A bag that is the intersection of the two bags. */
a w aw aw aw aw aw aw aw aw aw aw
public BagInterface<ItemType> intersection(BagInterface<ItemType> anotherBag);
aw aw aw
8
/** Creates a new bag of objects that would be left in this bag after removing those ob
aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw
jects that also occur in a second bag without the contents of the original two bag
aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw
s.
@param anotherBag The given bag.
aw a w aw aw
@return A bag that is the difference of the two bags. */
a w aw aw aw aw aw aw aw aw aw aw
public BagInterface<T> difference(BagInterface<T> anotherBag);
aw aw aw
© 2017 Pearson Education, Inc., Hoboken, New Jersey 070
aw aw aw aw aw aw aw aw
30
a w a w a w
(Version 7.0) aw
Data Abstraction & Problem Solving with C++
aw aw aw aw aw aw
Seventh Edition a w
Frank M. Carrano aw aw
University of Rhode Island
aw aw aw
Timothy M. Henry aw aw
New England Institute of Technology
aw aw aw aw
, 2
Solution Manual & Test Bank for Data Abstraction & Problem Solving with C++: Walls and Mirrors, 7th Edition by Frank M. Carrano
aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw
Chapter 1 Data Abstraction: The Walls aw aw aw aw aw
1
const CENTS_PER_DOLLAR = 100;
aw aw aw
/** Computes the change remaining from purchasing an item costing dollarC
aw aw aw aw aw aw aw aw aw aw
ost dollars and centsCost cents with d dollars and c cents. Preconditi
aw aw aw aw aw aw aw aw aw aw aw
on: dollarCost, centsCost, d and c are all nonnegative integers and ce
aw aw aw aw aw aw aw aw aw aw aw
ntsCost and c are both less than CENTS_PER_DOLLAR. Postcondition: d a
aw aw aw aw aw aw aw aw aw aw
nd c contain the computed remainder values in dollars and cents respec
aw aw aw aw aw aw aw aw aw aw aw
tively. If input value d < dollarCost, the proper negative values for
aw aw aw aw aw aw aw aw aw aw aw aw
the amount owed in d dollars and/or c cents is returned. */
aw aw aw aw aw aw aw aw aw aw aw
void computeChange(int dollarCost, int centsCost, int& d, int& c);
aw aw aw aw aw aw aw aw
2a
const MONTHS_PER_YEAR = 12;
aw aw aw
const DAYS_PER_MONTH[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
aw aw aw aw aw aw aw aw aw aw aw aw aw aw
/** Increments the input Date values (month, day, year) by one day.
aw aw aw aw aw aw aw aw aw aw aw
Precondition: 1 <= month <= MONTHS_PER_YEAR, aw aw aw aw aw
1 <= day <= DAYS_PER_MONTH[month - 1], except aw aw aw aw aw aw aw
when month == 2, day == 29 and isLeapYear(year) is true. Postcondit aw aw aw aw aw aw aw aw aw aw aw
ion: The valid numeric values for the succeeding month, day,
aw aw aw aw aw aw aw aw aw
and year are returned. */ aw aw aw aw
void incrementDate(int& month, int& day, int& year);
aw aw aw aw aw aw
/** Determines if the input year is a leap year.
aw aw aw aw aw aw aw aw aw
Precondition: year > 0. aw aw aw
Postcondition: Returns true if year is a leap year; false otherwise. */ aw aw aw aw aw aw aw aw aw aw aw
bool isLeapYear(int year);
aw aw
3a
changeAppointmentPurpose(apptDate: Date, apptTime: Time, purpose: string): boolean aw aw aw aw aw aw
{
if (isAppointment(apptDate, apptTime)) cancelAppointment(apptDate
aw aw aw
, apptTime) aw
return makeAppointment(apptDate, apptTime, purpose)
aw aw aw
}
© 2017 Pearson Education, Inc., Hoboken, New Jersey 070
aw aw aw aw aw aw aw aw
30
, 3
3b
displayAllAppointments(apptDate: Date): void aw aw
{
time = START_OF_DAY aw aw
while (time < END_OF_DAY) aw aw aw
if (isAppointment(apptDate, time)) displa
aw aw aw
yAppointment(apptDate, time) aw
time = time + HALF_HOURaw aw aw aw
}
This implementation requires the definition of a new operation
aw aw aw aw aw aw aw aw
displayAppointment()
as well as definitions for the constants START_OF_DAY, END_OF_DAY and HALF_HOUR.
aw a w aw aw aw aw aw aw aw a w
4
// Assume that storeBag is defined and contains your purchased items Bag<std::string>
aw aw aw aw aw aw aw aw aw aw aw aw
fragileBag;
while (storeBag.contains("eggs"))
aw
{
storeBag.remove("eggs"); fragileBag.add("eggs" aw
);
} // end while
a w aw aw
while (storeBag.contains("bread"))
aw
{
storeBag.remove("bread"); fragileBag.add("brea aw
d");
} // end while
a w aw aw
// Transfer remaining items from storeBag to groceryBag; Bag<std::st
aw aw aw aw aw aw aw aw
ring> groceryBag; aw
v = storeBag.toVector();
aw aw
for (int i = 0; i < v.size(); i++) groceryBag.add
aw aw aw aw aw aw aw aw aw
(v.at(i));
© 2017 Pearson Education, Inc., Hoboken, New Jersey 070
aw aw aw aw aw aw aw aw
30
, 4
5
/** Removes and counts all occurrences, if any, of a given string from
aw aw aw aw aw aw aw aw aw aw aw aw aw
a given bag of strings.
aw aw aw aw
@param bag A given bag of strings. @pa
aw aw aw aw aw aw aw
ram givenString A string.
aw aw aw
@return The number of occurrences of givenString that occurred and
aw aw aw aw aw aw aw aw aw aw
were removed from the given bag. */
aw aw aw aw aw aw
int removeAndCount(ArrayBag<std::string>& bag, std::string givenString)
aw aw aw aw
{
int counter = 0; aw aw aw
while (bag.contains(givenString))
aw
{
counter++; bag.remove(givenString aw
);
} // end while
a w aw aw
return counter; aw
} // end removeAndCount
a w aw aw
6
/** Creates a new bag that combines the contents of this bag and a second ba
aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw
g without affecting the contents of the original two bags.
aw aw aw aw aw aw aw aw aw
@param anotherBag The second bag.
aw a w aw aw
@return A bag that is the union of the two bags. */
a w aw aw aw aw aw aw aw aw aw aw
public BagInterface<ItemType> union(BagInterface<ItemType> anotherBag);
aw aw aw
7
/** Creates a new bag that contains those objects that occur in both this
aw aw aw aw aw aw aw aw aw aw aw aw aw
bag and a second bag without affecting the contents of the original two bags. @par
aw aw aw aw aw aw aw aw aw aw aw aw aw aw
am anotherBag The given bag.
aw aw aw aw
@return A bag that is the intersection of the two bags. */
a w aw aw aw aw aw aw aw aw aw aw
public BagInterface<ItemType> intersection(BagInterface<ItemType> anotherBag);
aw aw aw
8
/** Creates a new bag of objects that would be left in this bag after removing those ob
aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw
jects that also occur in a second bag without the contents of the original two bag
aw aw aw aw aw aw aw aw aw aw aw aw aw aw aw
s.
@param anotherBag The given bag.
aw a w aw aw
@return A bag that is the difference of the two bags. */
a w aw aw aw aw aw aw aw aw aw aw
public BagInterface<T> difference(BagInterface<T> anotherBag);
aw aw aw
© 2017 Pearson Education, Inc., Hoboken, New Jersey 070
aw aw aw aw aw aw aw aw
30