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