Assignment 3: Matrix Template

Objectives

Assignment

Create a class template called Matrix in the cs427527 namespace so that instantiations of that class represent 2-D matrices (rectangular arrays) in which each element is of the same type. The template should have one parameter to determine that type, which must be default-constructable, copy-constructable, and assignable.

Instantiations of your Matrix template must provide the following operations (T here represents the element type).

You may add additional members as necessary to satisfy other requirements, and members should be defined as const where appropriate.

To support iterating through rows and columns of matrices, there must be four nested types slice, const_slice, iterator and const_iterator. The slice types represent 1-D views of the matrix, where the elements accessed through the view are the actual elements in the matrix, so modifying an element through the view modifies what is in the matrix. The iterator types are then movable views of single elements in the slices.

The Matrix class template must have the following methods to create slices:

The slice types must then have the following methods to access elements and create iterators:

Finally, the iterator types must have the following methods

Note that the behavior of const slices and const iterators is intentionally different than the corresponding behavior in the STL – STL const iterators may still allow modification of the underlying container (but a const iterator cannot be moved).

Graduate Credit

The comparison operators for iterators must allow for comparisons between iterators and const_iterators and vice versa.

Other Requirements and Non-requirements

Unit Tests

There is also file matrix_unit.cpp in /c/cs427/hw3/Required that contains a main function that runs unit tests on your Matrix template. Your makefile should build an executable called Unit from that file. Your makefile must assume that the matrix_unit.cpp file is in the current directory; do not hard-code the directory containing that file. There will be private unit tests added to matrix_unit.cpp for grading.

Example

The following is the output when run with the given tests.
[jrg94@monkey Matrix]$ for N in `seq 0 15`; do ./Unit $N; done
2x2 IDENTITY MATRIX
1 0
0 1
2x2 IDENTITY MATRIX
1 0
0 1
ORIGINAL
99 0 0 0
0 98 0 0
0 0 0 0
COPY
99 0 0 0
0 98 0 0
0 0 0 0
ORIGINAL
0 0 0 0
0 0 0 0
0 0 0 0
MODIFIED COPY
99 0 0 0
0 98 0 0
0 0 0 0
RESET COPY TO ORIGINAL
0 0 0 0
0 0 0 0
0 0 0 0
3x3 MULTIPLICATION TABLE
0 0 0
0 1 2
0 2 4
middle row, last column: 33
PRINT USING SLICES
99 0 0 0
0 98 2 3
0 2 4 6
PRINT TRANSPOSED USING COLUMN SLICES
99 0 0
0 98 2
0 2 4
0 3 6
ORIGINAL WITH ITERATORS
99 0 0 0
0 98 2 3
0 2 4 6
COPY AFTER MODIFYING LAST COLUMN
99 0 0 42
0 98 2 42
0 2 4 42
SUM OF ORIGINAL BOTTOM ROW
12
SUM OF BOTTOM ROW OF CONST REFERENCE
12
SUM OF ENTIRE ORIGINAL
18
ITERATOR COMPARISONS
begin == begin: 1
iterator == const iterator: 1
same position, different matrices: 0
same position through row and column: 1
FINAL is defined

Submissions

Submit whatever source code (.cpp and .hpp) files you created and a makefile that builds the Unit executable when make is run with no arguments or with target Unit.