Sample Assignment: The Matrix Class
(Chapter 3)



Data Structures and Other Objects Using C++
Third Edition
by Michael Main and Walter Savitch
ISBN 0-321-19716-X

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.
Purposes:
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:
  1. matrix.h: The header file for the new matrix class.
  2. 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.
  3. 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).

The Matrix Class
Discussion of the Assignment

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).

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:

  1. 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.
  2. Two const member functions called many_rows and many_columns which return the number of rows or columns in a matrix.
  3. A const member function that allows you to retrieve the value of any coefficient.
  4. A member function that allows you to set the value of any coefficient.
  5. 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;
  6. 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).
  7. A const member function to print a matrix to any ostream.


Michael Main (main@colorado.edu)