Sample Assignment: The Matrix Class
Data Structures and Other Objects Using C++
by Michael Main and Walter Savitch
- The Assignment:
- You will implement and test a class for matrixes that have up
to ten rows and ten columns. The coefficients of the matrix will
be stored in a fixed 10x10 array of double numbers.
- Ensure that you can write a small collection class.
- Before Starting:
- Read Sections 3.1 and 3.3. Section 3.2 might also be
helpful, but it is not required reading.
- Know how to compile and run C++ programs on your system.
- Files that you must write:
The header file for the new matrix
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.
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).
The Matrix Class
Discussion of the Assignment
As indicated above, you will implement a new class called matrix,
using a header file and an
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).
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:
- A constructor with two arguments: the number of rows and the
number of columns in the matrix. Both have a default value of 1. The
constructor sets all coefficients of the matrix to zero.
- Two const member functions called
many_columns which return the number of rows or columns
in a matrix.
- A const member function that allows you to retrieve the value of
- A member function that allows you to set the value of any
- A non-member function that is an operator * to allow a matrix to
be multiplied by a scalar. For example, if s and t are matrixes, I
may write the statement:
s = 4.2*t;
- A non-member function that is an operator * allows two matrixes to be multiplied
together using the usual matrix multiplication. For example, if s and
t are two matrixes with sizes that are compatible for matrix
multiplication, then I may write a statement such as
r = s*t; (where r is another matrix).
- A const member function to print a matrix to any ostream.