Sample Assignment: The Matrix Class


matrix.h:
The header file for the new matrix
class.
matrix.cxx:
The implementation file for the new
matrix class. You will write all of this file, which will
have the implementations of all the matrix's member functions.
matrixtest.cxx
A simple interactive test program. You may write this
program on your own, or you may work together with one or two
other students (sharing your test code).
As indicated above, you will implement a new class called matrix, using a header file and an implementation file. Each matrix object is a rectangular grid of double numbers. The maximum number of rows and columns for the matrix is defined by a static member constant of the matrix class called MAX, which should be set to ten for this assignment. Keep in mind that your implementation should continue to work correctly when we change the size of MAX (something that you can be sure the TA will do when grading your work). Also, the date type of the entries should be declared with a typedef statement, like this:
typedef double value_type;Everywhere else that the entries' type is referred to must use the name
value_type
. For example, you will need a
twodimensional array to store the coefficients of the matrix:
class matrix { ... private: value_type data[MAX][MAX]; ... };
The rows and columns of any matrix can be indexed by
size_t
numbers starting at zero. So, if a matrix has
seven rows, then the indexes of those rows are 0 through 6. All
functions that have indexes as arguments must use an assert statement
to check that the sizes of the indexes are valid for the matrix.
Your matrix class must have these functions:
many_rows
and
many_columns
which return the number of rows or columns
in a matrix.
s = 4.2*t;
r = s*t;
(where r is another matrix).
This is a pretty big assignment, so I will help you out with a few items. In my implementation, the function to retrieve an element is:
value_type entry(size_t row, size_t column) const;Using this member function, I can write the function that prints the array to an ostream, like this (you need <iomanip> for using setw, <
set
to set an entry in a matrix:
matrix operator * (matrix::value_type scalar, const matrix& m) { matrix answer(m.many_rows( ), m.many_columns( )); size_t i, j; // Row and column numbers for (i = 0; i < answer.many_rows( ); ++i) { for (j = 0; j < answer.many_columns( ); ++j) { answer.set(i, j, scalar*m.entry(i,j)); } } return answer; }You may use these functions as I have written them, or you may make changes. For example, you might change things so that the WIDTH is no longer a constant. Instead, it could be a new member variable that is set by some new member function that you design.