You are on page 1of 4

CSE 232

Fall 2014

Programming Project #9
Assignment Overview
You will create a sparse matrix data structure using an underlying STL map and overload
various binary operators to exercise the data structure
This assignment is worth 50 points (5.0% of the course grade) and must be completed
and turned in before 11:59PM on Monday, Nov 17th . This is two weeks because of the
midterm on Nov 6th.
Background
A sparse matrix is a regular mathematical matrix where most of the elements have a 0
value. They are often used in science and engineering applications (see
http://en.wikipedia.org/wiki/Sparse_matrix ). Because they have 0's as most of the values,
special data structures and algorithms are required to be efficient in their use.
A Different Specification
The specification for this project will be a little different. In the past we have provided
both a main and a header file. From now on we will only provide the main. Your job is to
design the class to meet the specifications as provided by the main and that meets the
notes below.
One file
What you will turn in is a single file that contains the class declaration and the definitions.
If you need examples of how to do that, look at worksheets 14 and 15. This makes the
grading process easier. No other reason than that.
Updated 11/6. You will turn in two files: class-09.h and class-09.cpp Handin will allow
turning in both files.
Specifications
the underlying data structure will be a map. It will map a pair<long,long>
(representing the row,column address respectively) to a long .
constructors
o two arg: two longs indicating the number of rows and columns respectively.
SparseMatrix mat(100,50) makes mat with 50 rows each with 100
colums 100 rows with 50 columns each
o three arg: two longs indicating row and column, and an initializer list of longs.
The longs come in triples where each triple indicates: row, column, value to
be set in the matrix. SparseMatrix
mat2(5,5,{1,1,100,2,2,200}) makes mat2 a 5x5 matrix with the
value at 1,1 set to 100 and the value at 2,2 set to 200
operator(row,col) . Takes two args (row,col) and returns a long.

o The two argument longs indicate row and column and return either 0 or, if the
location's value has been set, the value. mat(1,1) will return either a 0 or
the value set at 1,1.
o You could overload a subscript operator (operator[]) but only with one
argument, the index of the element. If you want two indicies like
(row,column), you overload operator(). This operator is usually
interpreted as the function operator, but for now it will be the accessor
function. Note: It cannot be used to set a value, only return it.
.set(row,col,val) method. Takes 3 args: row, column and value. No return.
o Sets the value of the matrix at location row,column to value.
.dimensions() method. No args, returns a pair<long,long>
o returns the dimension (the row and column size) of the matrix
.element_count() method. No args, returns a long
o returns the number of non-default values in the matrix

Operations supported
addition between two matrices. Example shown below
addition between a matrix and a long.
o Should be commutative: 5 + mat == mat + 5
o Note: we make the decision that, for scalar addition, we only modify the
non-default values. Could be done differently, but that is what we do here.
multiplication between two matrices. Example shown below
multiplication between a matrix and a long
o should be commutative as addition with long
o since the default value is 0, the only values potentially changed are the
non-default values
printing. Should be able to print using cout << mat << mat2 <<endl;
o only non-default values are printed
o those values are printed in order
smallest row first
within the row, smallest column first
Errors
Certain errors require that you throw an exception. We cover exceptions in lab10 this
week. Summary, throw a runtime_error if you run into any of the following issues:
matrix-matrix addition requires that the two matrices have the same dimensions, the
same row and column size
matrix-matrix multiplication require that the row column size of the first matrix have
the same size as the column row size of the second matrix
Anything else you need!!!
You are defining your class, so any other functions/members you might need, feel free to
add them. The test will only involve using the main provided.
Important things to Note

1. You should try to make your code efficient. One of the things you learn is that
efficiency should come after you get the thing to work. So getting it to work should
be the most important goal. But afterwards, think about how to make it more efficient.
If you have a 1000x1000 matrix (106 elements) and only 1000 non-default values (say
the identity matrix), it would be wasteful, though not incorrect, to do 106 adds or 106
multiplies to find the answer.
a. it's easier to be efficient with adds, much easier, than with multiplies.
2. Don't worry about any of the errors to begin with. Write correct code and see if it runs
Add Matricies
Take a look at http://en.wikipedia.org/wiki/Matrix_addition

matrix + matrix

matrix + scalar
o adds the scalar to every element of the matrix.
o Remember, for our SparseMatrix, we only modify non-default values
1 2
6
7
3 4 + 5 = 8
9
5 6
10 11

Multiply Matricies
See http://en.wikipedia.org/wiki/Matrix_multiplication

matrix * long

matrix * matrix

Multiply Matricies
Deliverables
class-09.cpp and class-09.h -- your source code solution and your header
file will be required to solve the problem.(remember to include your section, the date,
project number and comments). Remember, you do not provide main-09.cpp
1. Please be sure to use the specified file names
2. Save a copy of your file in your CSE account disk space (H drive on CSE
computers).
3. You will electronically submit a copy of the file using the "handin" program:
http://www.cse.msu.edu/handin/webclient
Assignment Notes

You might also like