CS 340 - Software Design

Machine Problem 3, Fall 2001

Matrices

Due: Wednesday, October 10, 2001 at 11:59 pm

In this program you will create a simple matrix manipulation engine. The main purpose of this assignment is not to program all of the possible matrix calculations but to learn how to write C++ classes. With this in mind, the use of the C++ Standard Template Library will not be allowed. You MUST write all classes used in this program yourself with the one exception of the String class.

Your program is to perform the following operations on matrices:

  1. transposition
  2. cross product (also called matrix multiplication)
  3. matrix addition
  4. dot product (also called scalar multiplication)
Your will be required to create a class called Matrix. THis class must be able to hold a matrix of integer values of any size. The size must be dynamic. The above operations are to be written using operator overloading using the operators indicated in the following table. The table also describes each of the operations. You may add any other methods that you feel are important to the creation of your Matrix class.
OperatorOperation
!
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 follow a specific form. The number of columns of A must equal the number of rows of B and the resulting matrix will have the same number of rows as A and the same number of columns as B. If A has size of R1xC1 and B has size of R2xC2, C1 must equal R2 and the resulting matrix will have size R1xC2. 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].
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 int 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.

Your program will have to keep track of multiple matrices. The total number of matrices to be stored in your program is undefined; therefore you must have a dynamic storage class to hold the matrices. Each matrix is identified by a name (a String). The names are used as the keys to store and retreive the matrices. While the use of a fast dynamic storage structure such as a hash table is encouraged, it is not required. You may use any dynamic storage structure that you wish. This storage structure must be a C++ class, but there are no requirements on the methods that must be included in this class. Also all names used will be any sequence of non-white space characters.

The program is to use text input to get commands from standard input or a file. Each command will be given on its own line in the input and can be in either upper or lower case. The command will be the first non-white space character on the line. If the first non-white space character is not a command some error message should be produced. Each command (except for the # command) is to produce a message stating the command occurred. The commands are:

q
Quit the program.
h
Display help information for the program. This information must include all commands and a short description on how to use each command.
#
Do nothing. This indicates the command is a comment. This is primarily to be use to document the commands given in a file. The comment will be given on the same line as the # character. Any input line in which the first non-white space is a #, the input line should be ignored. Note, a line starting with a # can be given while processing input lines for other commands.
i <name> [<filename>]
Create an instance of a matrix with the given name.
This command has an optional filename (the square brackets around <filename> indicate that this is optional). If a filename is given, read the data for the matrix from the file. If a filename is not given, read the data for the matrix from the current input source (i.e. the next few lines of data will contain the matrix data).

The data will consist of integer values. The first two integer values will specify the size of the matrix in rows and columns (the first integer will specify the number of rows, the second integer will specify the number of columns). After this, the data values for each position in the matrix will be given in row-major order (all of the values for the first row will be given, then all of the values for the second row, then all of the values for the third row, etc). You may assume that the input will always contain the proper number of integer values for the size specified. You must check that the size values given are both values of one or greater. If either size value is incorrect, print an error message and continue processing the input for valid commands from the current input source.

If the name given in the command corresponds to a matrix that already exists, remove (and deallocate) that matrix than create a new matrix with the current information. If the name does not correspond to an existing matrix, create a new matrix with a new name.

Here are a list of data files that we created. These do NOT test all possible input to your program. So you will have to create your own tests for this. Also, these files are not the only ones that will be used when grading your program.

f <filename>
Get commands from the specified file.
Your program is to begin reading commands from the specified file. This file will become the "current input source". We will allow the f command to be contained inside of files, allowed for a nesting of files to read from. Your program must remember the previous input source and be able to remember any number of previous input sources (the use of a stack will be helpful here). When you get the the end of the file (assuming the file does not contain the q command), your program is to continue processing commands from the previous input source.

Here are a list of data files that we created. These do NOT test all possible input to your program. So you will have to create your own tests for this. Also, these files are not the only ones that will be used when grading your program.

l
List all matrices known to your program with sizes
This command is to produce a listing of the names and sizes of all matrices known to your program. The matrices are to be listed sorted by the sizes (number of rows, then number of columns).
r <name>
Remove a matrix
This command will remove the matrix with the given name from the program.
m <name1> <name2>
Make a copy of a matrix
Copy the matrix with <name2> into a matrix with name <name1>. If the original matrix does not exist, print an error message and don't create the new matrix. If a matrix already exist with <name1>, remove the existing matrix and create a new one under that name.
p <name> [<filename>]
Print out the matrix
First print out the size of the specified matrix, then print out the values for the matrix specified. If the optional <filename> is given, output the information to the indicated file; otherwise, output the information to standard output.
t <name>
Tranpose a matrix
Replace the specified matrix with the tranposition of itself.
c <name1> <name2> <name3>
Perform the cross product
Store into <name1> the result of the cross product of <name2> and <name3>. If either of the original matricies does not exist, print an error message and don't create a new matrix. If the size of the two original matricies is incorrect, print an error message and don't create a new matrix. If a matrix already exist with <name1>, remove the existing matrix and create a new one under that name.
a <name1> <name2> <name3>
Perform matrix addition
Store into <name1> the result of adding <name2> and <name3>. If either of the original matricies does not exist, print an error message and don't create a new matrix. If the size of the two original matricies is incorrect, print an error message and don't create a new matrix. If a matrix already exist with <name1>, remove the existing matrix and create a new one under that name.
d <name1> <int> <name2>
Perform the dot product
Store into <name1> the result of the dot product from <int> with <name2>. If the original matrix does not exist, print an error message and don't create a new matrix. If a matrix already exist with <name1>, remove the existing matrix and create a new one under that name.
o <filename>
Write out the entire knowledge of program to a file.
If the file already exists, you are to append to the end of the file. The knowledge of the program is to be written such that the information can be read in using the f command. This means that the output is to be given as a sequence of i commands with the data for each matrix being specified following each i command. The names specified in the file should be the same name as specified in the program.

Your program must also take an optional command line argument. The argument would be the name of a file that would contain commands for the program to use. If this command line argument is given, your program should use the command line value as the filename for an f command. So if a command line argument is given, the file is opened and the commands are read from the file. Once the end of the file is reached, commands should be taken from the standard input.

As specified, your program will need three dynamic classes.

  1. The class to hold a matrix
  2. The class to store all of the matricies
  3. The class to store the stack of "previous input sources" for the f command.
Since all of these classes are dynamic, these classes should have a copy constructur, assignment operator and destructor written for them. Refer to these notes for some ideas on the creation of these classes.

This program will require the use of multiple source code files and separate compilation. The division of the subroutines between the multiple source code files must be logical. One suggestion would be to have each class in its own file (or one file with all classes) and the command interface code in another source code file. Note: a "source code file" is not the same as a "header file". Source code files will have a file extension of .c (or .cpp, .C, .CC, etc.), while a header file will have a file extension of .h. To perform separate compilation, your makefile must separately compile the source code files into ".o" files and then link the ".o" files together. Using a #include statement with a source code file (a .c file) will NOT satisfy a requirement of separate compilation.

In addition to using and submitting a makefile, this program will also require a 1-2 page write up of the data structures used in the program and the logical division of your program into multiple source code files (i.e. which routines are where). Remember that this write-up is to be written in ASCII format and is to be electronically turned in with your program. The name of this file should be "readme.txt". Also recall that your program will be given to another student to write a critique. Therefore, it is suggested that you do not include your Social Security Number in your program. Instead use your name and your CS User ID to identify yourself.

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 to submit this project using the CS Department's UNIX machine's turnin command. The project name for this assignment is mp3. Be sure to submit all source code, header files, makefile as well as your program description. Failure to turnin all required pieces will result in a lower grade for the assignment.