CS 340 - Classes and Operator Overloading

Machine Problem 6

Due date: Thursday April 27, 2006 at 11:59 pm

Driver programs to test out you library:

  1. MatrixMain.cpp
  2. matrix5x4.data

For this assignment you are write a library "Matrix.h" that will be used by others. The library will contain the class Matrix. This library should be accompanied by a source code file "Matrix.cpp". A program using the Matrix class library should only include the Matrix.h file and should link to the object file of Matrix.o during the address resolution step of compilation (i.e. at link time).

The class: Matrix

The class Matrix will be a two dimensional matrix of real (floating point) values. This storage space for this class must be dynamic. Since dynamic two dimensional arrays seem to give people a fair amount of trouble, you may wish to synthesize a two dimensional array with a single dimensional array.

The operators to overload are as follows:

!transpose of the matrix

If matrix T is the transpose of matrix A, the value at position T[i][j] = A[j][i]. The dimensions of A and T will be exactly opposite. That is, the number of rows in A will equal the number of columns in T and the number of columns in A will equal the number of rows in T.

*matrix multiplication (the cross product)

When multiplying matrices A and B, the dimensions must be exactly opposite. That is, the number of rows in A must equal the number of columns in B and the number of columns in A must equal the number of rows in B. If matrix R is the result of adding A and B, the value at R[i][j] = Sum of (A[i][1]*B[1][j] + A[i][2]*B[2][j] + ... + A[i][m]*B[m][j]), where m is the number of columns in A. Note the A*B is not the same as B*A.

+matrix addition

When adding matrices A and B, the dimensions must be the same. That is the number of rows in A must equal the number of rows in B and the number of columns in A must equal the number of columns in B. If matrix R is the result of adding A and B, the value at R[i][j] = A[i][j] + B[i][j].

*scalar multiplication (the dot product)

This is when a singular value will be multiplied by a matrix or a matrix will be multiplied by a singular. The resulting matrix R will have the same dimensions as the original matrix A and R[i][j] = value*A[i][j].

=assignment

The left-hand side is to be made to be an exact copy of the right-hand side. Make sure the assignment operation deallocates any dynamic memory that had been allocated to the left-hand side matrix.

==equality

For two matrices A and B to be equal, they must have the same dimensions and A[i][j] must equal B[i][j] for all possible combinations of i and j.

!=inequality

Two matrices A and B are not equal if they have different dimensions or A[i][j] is not equal to B[i][j] for any combination of i and j.

<<Stream Output

You are to first print the dimensions of the matrix (number of rows, then number of columns) followed by the values in the matrix list one row at a time.

>>Stream Input

The first two values read indicate the number of rows followed by the number of columns. After this all of the values from the matrix will be read in row by row.

The overloaded operator * will perform two different operations based on the type of the operands. If both operands are of type Matrix, then matrix multiplication will be performed. If one operand is of type Unum and the other is of type Matrix, then scalar multiplication will be performed. For the matrix multiplication and addition, the code must validate the two matrices are of compatable dimensions to perform the operation. If they are not of compatable dimensions, print an error message.

The class is also to have the following:

Write any other constructors, destructor or methods that you feel are necessary.

Your program must be written in good programming style. This includes (but is not limited to) meaningful identifier names, a file header at the beginning of each source code file, a function header at the beginning of the function, proper use of blank lines and indentation to aide in the reading of your code, explanatory "value-added" in-line comments, etc.

The work you turn in must be 100% your own. You are not allowed to share code with any other person (inside this class or not). You may discuss the project with other persons; however, you may not show any code you write to another person nor may you look at any other person's written code.

You are also to write a one to two page program description. This write up must be submitted with your program and be in ASCII text format. This description is to explain your internal data structures, code structures and the algorithms used in your program. Remember, this program description will be read by another student when the critiques are done for this assignment. Often the title of "readme" is used for these types of documents.

You are to submit this project using the CS Department's UNIX machine's turnin command. The project name for this assignment is mp6. Be sure to submit all source code and header files as well as your makefile and program description. Failure to turnin all required pieces will result in a lower grade for the assignment.