Sample Assignment: A Bag with Receipts (Using Dynamic Arrays)
(Chapter 4)

Data Structures and Other Objects Using C++
Third Edition
by Michael Main and Walter Savitch
ISBN 0-321-19716-X

The Assignment:
You will implement and test the Bag with Receipts as described in the programming projects at the end of Chapter 3 -- but use dynamic arrays, not fixed-size arrays.
Ensure that you can write a small class that uses a dynamic array as a private member variable. You'll also be exposed to parallel arrays.
Before Starting:
Read all of Chapters 3 and 4.
Files that you must write:
  1. receipts.h: The header file for the bag_with_receipts class. Actually, you don't have to write much of this file. Just start with our version and add your name and other information at the top, and add your private member variables at the bottom. If some of your member functions are implemented as inline functions, then you may put those implementations in this file too. . Note that the header file has no CAPACITY constant because the items are stored in a dynamic array that grows as needed. But there is a DEFAULT_CAPACITY constant, which provides the initial size of the arrays created by the default constructor.
  2. receipts.cxx: The implementation file for the new bag_with_receipts class. You will write all of this file, which will have the implementations of all the member functions.

The Bag with Receipts Using Dynamic Arrays
Discussion of the Assignment

Some helpful notes:

Start by declaring the new bag_with_receipts's private member variables in receipts.h. See the project description in the projects of Chapter 3 for some ideas, but remember that the arrays must be dynamic rather than fixed-size. You will also need two size_t variables to keep track of the number of items in the List and the total size of the dyamic arrays. After you've declared your member variables, write an invariant for the top of receipts.cxx.

Once again, do your work in small pieces. For example, my first version of the bag_with_receipts had only a constructor, insert, and occurrences. My other member functions started out as stubs.

Write an interactive test program and use the debugger to track down errors in your implementation. If you have an error, do not start making changes until you have identified the cause of the error.

When a member functions needs to increase the size of the dynamic array, it is a good idea to increase that size by at least 10% (rather than by just one item).

Michael Main (