The purpose of this project is to test your ability to use templates, dynamic memory (mixed with classes), operator overloading, and libraries effectively in program design.
Create a template class for a dynamic 1D array. You can model your class off of our String class or the dynamic list class.
Changes from String class:
Show how useful your template array class is by creating arrays of
— all in one test application.
Make sure your test application is a good/thorough test of your class. (Your test application might utilize the apply and accumulate functions from lecture to facilitate testing. Also note how a typical template test application is structured — using templates to ease development.
This assignment is (Level 4).
Add (Level 1.5) to add (mutable) members for strings (Strings?) to print before, between the elements of, and after the array. (Hint: You'll need set functions for these items. I'll even throw in an extra (Level 1) to use a flag to turn the printing of these members on/off.)
Add (Level 2.5) to allow the programmer to specify an input format using a flag syntax (similar to how we did for the String's failure state and how the setf mechanism works for the iostreams). Remember that array input formats are typically: known sentinel, pre-read sentinel, pre-read count, or fail/eof terminated. So you should have three flags with which they can choose the basic format and a fourth saying whether fail should terminate entry or simply be cleared/re-tried. EOF should probably always terminate entry since we don't know if the stream we are reading from is the keyboard or a file. You will also need a helper function to allow the programmer to set the sentinel value if they choose the known sentinel format of input.
Add (Level 1) to use a block/doubling scheme for memory allocation/growth to avoid fragmentation of the heap and allow speedier addition of individual elements. This will require making sure their provided size is the next power of 2 so that the array's sizes are always powers of 2.
Add (Level 1) to shrink the array in a pattern analogous to that you've chosen for growth (per-element, block, doubling). If the shrinkage is optimistic (only shrink when twice the empties are present that would normally make you shrink — and still only shrink by the normal amount), I'll throw in an extra (Level 0.5). And I'll raise that another (Level 0.5) if your growth scheme is doubling.
Add (Level 1.5) to allow the programmer to specify a different beginning index than 0. For instance, they could specify that the array's indices should start at -5. If the array is 10 long, that would be the indices -5, -4, -3, ... 2, 3, 4. (Tip: This can allow more natural implementation of solutions to some real-world problems. Even simple languages like Pascal have had this feature for a long time.)
(Hint: Does this change anything except your subscript operators and constructor(s)? Does it add anything other than the beginning index member variable? Should that member be mutable? Consider that the programmer using your class will need to pass your subscript operator something. How will they know what the initial and final values of that something are for their for loop?)
Add (Level 2) to allow the programmer to specify the index range/type. In other words, the programmer might say that the indices are from A through F (and obviously are char). (Note: They still only specify the beginning index and the size — not individual values you are to use as indices.) The index type should default to size_t.
Add (Level 4) to use a separate library of template functions for array copy, array concatenate, array find, etc. Also include in this library functions to make subrange copies by index and length as well as by Boolean function. That is, if the programmer says to copy from element 4 for 5 elements, you should create a new array to hold copies of those 5 elements (4, 5, 6, 7, and 8) and return it. And if the programmer says to copy all elements which are greater than value 10.5, you should create a new array whose elements are copies of all of the original array's elements having values greater than 10.5. (Hint: This bool should be a function argument.) Also include functions to remove elements by index/range and bool function.
The find functions should be removed from the array class, since they are now in the template library (and should be able to work on Array class objects).
Either add testing to your test app for this template library or make it its own test app.
Add (Level 1.5) to have only 2 flags for the input formats: one that chooses sentinel or counted and one that chooses pre-read or known. This will allow 4 input formats in only two flags...since the flags can be set/unset at the same time. (This also means another mutator for the known count format — if you didn't already support it.)
Add (Level 2) to make your class(es) support 2D as well as 1D. (You may want to add a second class for the second dimension instead of making one class do both.) For 2D dynamic memory help see the notes. For some more advanced thinking on this subject, check out this C/C++ User's Journal article. (No, you cannot just use Mr. Bavestrelli's code.)