Course Home | Assignments | Computing Resources | Lab Hours/Tutoring | Questionnaire | Schedule | Submit

Saint Louis University

Computer Science 146
Object-Oriented Practicum

Michael Goldwasser

Spring 2012

Dept. of Math & Computer Science

Assignment 05

Matrix Expansion

Overview

Topic: Matrix Expansion
Due: 11:59pm Tuesday, 29 February 2012

Please make sure you adhere to the policies on academic integrity.


Introduction

In this assignment, we add additional functionality based upon similar features found in MATLAB. In particular, we examine the way that MATLAB handles assignments to an element of a matrix, particularly when the indices given appear to be beyond the current bounds of a matrix. In our C++, consider working with a matrix A having current contents

 5 15  3  8
20  2 14  3
With our standard zero-indexing of rows and columns, we have seen that a syntax such as A(1,2) would be used to retrieve the element having value 14 and that an assignment syntax A(1,2) = 35 can be used to modify that value, resulting in the overall contents
 5 15  3  8
20  2 35  3

If a user were to attempt to access a non-existing element, perhaps with a command such as cout << A(3,1) << endl;, it is reasonable for our class to report an error condition, as there is no element with such indices. That is what MATLAB does as well with such an invalid attempt to read a value from the matrix. Interestingly, MATLAB will allow you to write to an entry of a matrix that isn't currently present. In this case, it pads the matrix with sufficient zeros to give it the minimal number of rows and columns needed for the new entry.

As an example, using the zero-indexing convention of C++, we might expect a command A(3,1) = 7 to modify the previous contents resulting in a new setting of

 5 15  3  8
20  2 35  3
 0  0  0  0
 0  7  0  0
adding two additional rows so that entry A(3,1) can be properly placed. Continuing with this example, an assignment of A(0,4) = 14 would cause one additional column to be added to the rightside of the matrix, leading to
 5 15  3  8 14
20  2 35  3  0
 0  0  0  0  0
 0  7  0  0  0
Finally, we note that a single assignment may require the expansion of new rows and new columns. Continuing with our example, the result of the assignment A(4,7) = 20 would be
 5 15  3  8 14  0  0  0
20  2 35  3  0  0  0  0
 0  0  0  0  0  0  0  0
 0  7  0  0  0  0  0  0
 0  0  0  0  0  0  0 20


Your task

Your task is to modify your matrix.h code to support the new functionality of expanding a matrix, if necessary, during an assignment. You will find that our original version of this file contains two related definitions:
  double operator()(int r, int c) const
and
  double operator()(int r, int c)
The only difference in the signatures is the use of the keyword const in the first of these. C++ uses that differentiation to allow us to vary the behaviors of retrieving an existing entry (the const signature is applied) versus assinging a new value to an entry (the non-const signature is applied). Therefore, you will be modifying the second of those functions.

In terms of the underlying algorithm, we want you to carefully consider how much data movement is required. Keep in mind that the underlying data is stored in column-major order in a one-dimensional vector. Therefore, if a new matrix entry causes us to add columns (but not rows), it should be reasonably easy to make the necessary adjustment by adding more entries to the end of the underlying vector. However, you will find that adding new rows to a table stored in column-major order is non-trivial. In this case, you should still be able to use the same (expanded) vector, but you will have to be much more careful about how to move data around without losing any information.


Files we are providing

You can continue to add to the matrix.h code from the previous assignment, but we do need to provide a new makefile and new test driver.

If working on turing, change directories into a desired subdirectory of your home folder if desired, and to execute the following command verbatim

cp -R /Public/goldwasser/146/asgn05 .
This will cause a new folder named asgn05 to appear in your working directory including all files that you need for this assignment (however, with our earlier verison of matrix.h). Alternatively, these files can be downloaded individual from this website. The matrix definition is essentially the same one we have been using for past assignments. However, we have written a new driver for this assignment to make it easier for you to test your code. That driver is named testExpand and can be built with the make command.

Documentation

Since you will need to work with the underlying vector object, it will be helpful to browse through documentation for the vector class and in particular for the resize method of that class.


Submitting your project

Your revised matrix.h file should be submitted electronically (details on the submission process).


Michael Goldwasser
CSCI 146, Spring 2012
Last modified: Monday, 26 September 2011
Course Home | Assignments | Computing Resources | Lab Hours/Tutoring | Questionnaire | Schedule | Submit