Sample Assignment: The Dynamic Matrix Class
(Chapter 4)



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

The Assignment:
You will revise your matrix class from the previous assignment so that it uses a dynamic two-dimensional array.
Purposes:
Ensure that you can make use of a dynamic two-dimensional 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:
  1. matrix.h: The header file for the revised matrix class.
  2. matrix.cxx: The implementation file for the revised matrix class.
  3. 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 two-dimensional 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.

  1. In the old header file, you had a two-dimensional array of size MAX x MAX. Please remove the MAX definition from your implementation, and replace the two-d array with a pointer to a two-d 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;
    
  2. Our plan is to have the constructor create the two-dimensional dynamic array--and 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.
  3. 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!)
  4. 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 2-d array of size rows x columns
        // containing all zeros, and num_rows and num_columns are correctly set. 
    
    The techniques for creating the 2-d 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.
  5. Modify your constructors so that they call the helper function to create the 2-d dynamic array. In fact, my constructors do nothing but call that helper function! At this point, your program should compile--but it still won't run right because you don't yet have the value semantics defined.
  6. 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 one-dimensional dynamic arrays from Chapter 4, you'll also need new techniques for this from lecture topics.
  7. 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 self-assignment, so do the work:
    	    1. Delete the old memory
    	    2. Do the same work as the copy constructor:
            } 
    
    	return *this;
        }
    
  8. You're not quite done, but all of your test program should now work.
  9. Last step: implement a destructor. This merely calls delete_dynamic_array.

Michael Main (main@colorado.edu)