Sample Assignment: The Dynamic Matrix Class
(Chapter 4) 
Data Structures and Other Objects Using C++
Third Edition
by Michael Main and Walter Savitch
ISBN 032119716X

 The Assignment:
 You will revise your matrix class from the previous
assignment so that it uses a dynamic twodimensional array.
 Purposes:
 Ensure that you can make use of a dynamic twodimensional array.
 Before Starting:
 Read Sections 4.1 to 4.4, and come to all lectures this week
because some of the material is not in the book.
 Files that you must revise:


matrix.h:
The header file for the revised matrix
class.
matrix.cxx:
The implementation file for the revised
matrix class.

matrixtest.cxx
A simple interactive test program. You may probably use the
same progam from last week. It may be written
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
Following the prescription in Section 4.4 of the textbook, you will
revise your matrix class from last week to use a dynamic
twodimensional array. Since this is your first time using dynamic
memory, please follow these steps precisely. If you come for help, we
will ask you which steps you have completed and which one you are
working on now.
 In the old header file, you had a twodimensional array of size
MAX x MAX. Please remove the MAX definition from your implementation,
and replace the twod array with a pointer to a twod array. You
will also still have member variables for the current number of rows and
the current number of columns:
value_type** data;
size_type many_rows, many_columns;

Our plan is to have the constructor create the twodimensional dynamic
arrayand its size is no longer limited to MAX. Therefore, if you
have a precondition in your constructor that limits the number of rows
and columns, please delete that precondition now. Also, please check
any other preconditions that you may have. Either those preconditions
are no longer needed, or they should make use of many_rows and
many_columns rather than MAX. Make sure that you adjust your
documentation, too.
 At this point, you should have no remaining uses of MAX. To test
that this is the case, try recompiling. Everything will recompile
correctly (though it won't yet run!)

It will be useful to write this helper function next:
void matrix::make_dynamic_array(size_type rows, size_type columns)
// Precondition: data does not yet point to dynamic memory.
// Postcondition: data points to a new 2d array of size rows x columns
// containing all zeros, and num_rows and num_columns are correctly set.
The techniques for creating the 2d array will be covered in
class. Remember: you must put the prototype for the helper function in
the .h file, but the implementation and the precondition/postcondition
contract go in the .cxx file.

Modify your constructors so that they call the helper function to
create the 2d dynamic array. In fact, my constructors do nothing but
call that helper function! At this point, your program should
compilebut it still won't run right because you don't yet have the
value semantics defined.

Implement a second helper function:
void matrix::delete_dynamic_array( )
// Postcondition: All dynamic memory used by data has been released, and
// num_rows and num_columns are both zero.
Though this is
based on ideas for onedimensional dynamic arrays from Chapter 4,
you'll also need new techniques for this from lecture topics.

Implement the value semantics for your class following these outlines:
matrix::matrix(const matrix& source)
{
1. Make this matrix's array the same size as source's.
2. Use loops to copy the data from the source to this matrix.
}
matrix& matrix::operator = (const matrix& source)
{
if (this != &source)
{ // This is not a selfassignment, so do the work:
1. Delete the old memory
2. Do the same work as the copy constructor:
}
return *this;
}
 You're not quite done, but all of your test program should now
work.
 Last step: implement a destructor. This merely calls delete_dynamic_array.
Michael Main
(main@colorado.edu)