Programming Assignment #1

Due: Fri, Feb 2

Objective

This assignment will provide practice with a templated vector class, along with practice on the basic concept of an iterator over a container.

Task

In your prior course, you should have seen examples of building array-based classes with dynamically managed arrays inside. This is the core concept of a vector class -- a container that stores a dynamically managed array internally.

For this assignment you will implement a templated vector class, along with an associated iterator class for helping with generic container traversals. The following starter files are provided for you:

Your job will be to finish this templated set of classes by defining each of the functions in the tvector.h file. These should be defined in a file called:

Note that there is already a #include at the bottom of tvector.h where your definition file will be brought in. This illustrates a pretty standard format for setting up a templated class.
 

Iterators

The small class called TVectorIterator is a helper class that can be used in conjunction with the vector class. This is a common feature used in container classes like this. The purpose of an iterator is to provide a common and non-implementation-specific way of traversing a container, so that mulitple containers could potentially use common algorithms (like sorting and searching functions, for example). This will be explored and explained more in the course. For the iterator class in this assignment, here is a brief sample use:
  // suppose that V is a vector storing ints, and it has 
  //   already been populated with the values 3, 6, 9, 12, 15, 18, 21

  // this call would retrieve a vector iterator over the container V
  TVectorIterator<int> itr = V.GetIterator();

  // at this point, itr currently is positioned at the first element in 
  // the list (the 3).

  int x = itr.GetValue();		// x would now store 3
  itr.Next();				// itr has advanced to the 6
  int y = itr.GetValue();		// y would now store 6
  itr.Next();
  itr.Next();				// we have now advanced to the 12
  int z = itr.GetValue();		// z now stores 12
 
  itr.Previous();			// now we have moved backwards, to the 9
  int a = itr.GetValue();		// a stores 9.    etc.
This class essentially helps us walk through the container in a fairly easy way, with calls to Next() and Previous() to move around.

Program Details

Here are general descriptions of the two classes you are to define, along with a general description of each function's task.

1) class TVector

The member data of this class consists of a pointer to a dynamically managed array, along with tracking variables for the capacity and the size. Note that the capacity refers to how much space is currently allocated, and size refers to how many data items are currently stored. There is also a dummy variable of type T that can be used for error-checking situations. Specifically, some of the functions specify to return a stored data item, but if you encounter a situation like an empty container or other situation where there would not BE a valid data item, you can return a reference to the dummy object instead. This is needed because some such functions are pass-by-reference (so that the retrieved item can be modified by the caller under normal situations). There is also a static constant called SPARECAPACITY, which will be used to add some extra capacity when constructing vector objects.

Function descriptions

2) class TVectorIterator

The TVectorIterator class has three member data variables -- a pointer to the data to which it currently refers, the size of the associated vector, and the index from the associated vector. Note that the iterator is not intended to be built as a stand-alone item, but rather is created and returned BY member functions in the TVector class, so that it is associated with the vector. We need data variables for size and index, because the array itself is not inside an iterator object. The pointer will be used to access the data (i.e. it will be needed by the GetData function!)

A few general rules!


3) Test Program

Create a test program of your own in the file mydriver.cpp. You can use my provided driver.cpp as a general model of how to populate a vector. Your test program should contain the following tests/illustrations at a minimum:

4) makefile

Create a makefile that configures a build of both my provided driver (driver.cpp) and your test program (mydriver.cpp). i.e. when you type "make" in the directory, it should compile and build both executables. Make your executables named "driver.x" and "mydriver.x", respectively.

5) General Requirements


Deliverables and submitting

These are the deliverable files you should submit:
  tvector.h
  tvector.hpp
  mydriver.cpp
  makefile
To submit, package up your files in a tar archive and upload this at the assignment submission link on the Canvas course site (in the "Assignments" section). Your tar file should be named in this format, all lowercase:
   lastname_firstname_p1.tar

   Example:  My tar file would be:    myers_bob_p1.tar

Note that in addition to the provided test cases, we will also test your program/classes using additional test programs. Your program must be able to pass all such test cases to obtain a full score for the corresponding components.