Project 3: Matrix Operations
Project Objective:
1. user interface de sign and implementation
2. basic matrix operations
3. multi-dimensional array manipulation
4. function and recursion
Project Description:
In this project, you will design and implement a program to perform the basic
matrix
operations: negation, transpose, addition, multiplication, and de terminant
calculation . Your
program should 1) read in a list of matrices from an input file, 2) provide a
user interface to ask
users to enter the matrix operation they want to perform, and 3) print out the
result on the screen.
The executable of a master program and a sample input file will be provided as
usual.
Basics on Matrix
A matrix is a rectangular (two-dimensional) array of elements (integers in this
project),
each element is labeled by its row and column. For example, the following is a
2x3 matrix
2 5 8
1 3 6
It has two rows and three columns, which we refer to as the size (2x3) of
the matrix. The
element 5 is said to be at coordinates (1,2), meaning that it is located at the
intersection of the
first row and the second column.
A general matrix is denoted by Anxm, where n and m are the numbers of
rows and
columns in the matrix. aij(or a(i,j)) stands for the element in the
i-th row and j-th column.
The negation of a matrix Anxm is a matrix Bnxm of
the same size where bij
= -aij
for all
1≤i≤n and 1≤j≤m. In other words, each element is negated. The negation of a
matrix A is usually
denoted as –A.
The transpose of a matrix Anxm is a matrix Bmxn
where bji = aji
for all 1≤i≤n and 1≤j≤m.
In other words, the first row of matrix Anxm becomes the first column
of matrix Bmxn, the second
row of matrix Anxm becomes the second column of matrix Bmxn,
etc. Note that the size of B is
different from that of A because the order of n and m (i and j) has been
switched. The transpose
of a matrix A is usually denoted as AT (we use T(A) in this project).
For example, given the 2x3 matrix A defined above, we have

You can add two matrices of the same size. The sum
will be a matrix of the same size as
the operand matrices, and is calculated by adding the matrices element by
element. That is, Anxm
+ Bnxm = Cnxm, where cij = bij + aij
for all 1≤i≤n and 1≤j≤m.
Matrix multiplication can be d one between to matrices if the number of
columns in the
first matrix equals the number of rows in the second matrix. The product will be
a matrix that has
the same number of rows as the first matrix and the same number of columns as
the second
matrix. That is,
for all 1≤i≤n and
1≤j≤k. For example,

where, 93 = 2*2+5*5+8*8, 65 = 2*1+5*3+8*6, 65 =
1*2+3*5+6*8, and 46 = 1*1+3*3+6*6.
Note that A·B are B·A are generally different.
The determinant of a square matrix Anxn (matrix that has the same
number of rows and
columns) is denoted det (Anxn) and can be calculated as follows:

where
is an (n-1)x(n-1)
matrix constructed by deleting the first row and the i-th column of
Anxn. For example,

Input and Output
(1). Your program will read in a list of matrices from an input file. Each
matrix starts with
one line that has a single character matrix name, an integer for the
number of rows (n), and an
integer for the number of columns (m), followed by n lines of integers
with m integers on each
line. A new matrix starts on the next line with the same format.
For example, the input for the above matrix A is:
A 2 3
2 5 8
1 3 6
For this project, the input file contains at most six (6) matrices and
each matrix has no
more than ten (10) rows and no more than ten (10) columns.
(2). Your program will print out the following user interface that lists the
above five matrix
ope rations and the option to quit:
Please select from the following matrix operations:
1. Negate matrix
2. Transpose matrix
3. Calculate determinant
4. Add matrices
5. Multiply matrices
6. Quit
Please select option:
(3). Following this prompt, the user will enter an option by its number (1 – 6),
and your
program must check if the option entered is valid. If an invalid option is
entered, not 1 – 6, you
must print the following error message on the next line
Invalid matrix operation.
and return to step (2).
(4). Based on the option that the user enters, your program will prompt the user
to select
which matrix/matrices to use, by name. The first three operations are unary
operations and will
prompt the user for 1 matrix, while the addition/multiplication operations are
binary and will
prompt the user for 2 matrices. Your prompt must include the names of the
matrices listed in the
input file. For example, if the input file contains three matrices named A, B,
and C, then on the
following line, you should print out:
Select matrix (by name: A, B, C):
Note that for binary operations, this line will be printed twice.
If an invalid matrix name is entered (not among the names in the input file),
then you will print
on the following line
Invalid matrix selection.
and proceed back to step (2). For binary operations, this message will be
printed only once if
either matrix is incorrect, and after the prompt for the second matrix.
(5). After the program reads in the operation and valid matrix name(s), it will
perform the
operation, print out the result, and go back to step (2). Not all operations
will be possible for the
matrices selected. If an operation cannot be performed on the matrix/matrices
selected, you will
print the appropriate error message (see below) and return to step (2).
When a valid option is entered along with valid matrix/matrices, you need to
print out the result
starting from the next line and then return to step (2). If the result is an
integer, print this integer.
If the result is a matrix, print the matrix out in the tabular format, with the
columns aligned.
Note that none of these operations should change the original matrices. For
example, when
negating a matrix, you print out the negated matrix, but the original matrix
remains unchanged.
(6). When the user enters option 6, quit, you will print the following quit
message and exit the
program. The quit message is:
Thanks for using the matrix operation program.
Project Requirements:
1. You must program using C under GLUE UNIX system and name your program
p3.c.
2. Your program must be properly documented.
3. The determinant of a matrix must be calculated using a recursive function.
4. Submit your program p3.c electronically before the due time. Use the
submit program to
do this. Also please do NOT send your input file.
IMPORTANT: Your program’s output should be exactly the same as that produced by
the
master program. For your convenience, all the printf() statements used in the
master program
are listed below:
// The option prompt:
printf("Please select from the following matrix operations:\n");
printf("\t1. Negate matrix\n");
printf("\t2. Transpose matrix\n");
printf("\t3. Calculate determinant\n");
printf("\t4. Add matrices\n");
printf("\t5. Multiply matrices\n");
printf("\t6. Quit\n");
printf("\nPlease select option: ");
printf("Thanks for using the matrix operation program.\n");
printf("Select matrix (by name: ");
// matrix selection prompt. use %c to print the matrix names
// separate different matrix names by comma and a space:“, “
// Error messages:
printf("Usage: executable input\n");
printf("ERROR: input file not found.\n");
printf("Invalid matrix selection.\n");
printf("Invalid matrix operation.\n");
printf("Cannot add these matrices.\n"); // if two matrices are of different size
printf("Cannot multiply these matrices.\n"); // if two matrices are of different
size
printf("Cannot calculate the determinant of this matrix.\n"); // if the matrix
is not a square matrix
// Print out formats:
printf("\n");
printf("%5d", aij); // aij is an element of the matrix, this is used to print
out matrix
printf("\t%d\n", det); // use this format to print out the determinant of a
square matrix
Grading Criteria:
| Correctness: |
80% |
|
| Good coding style: |
10% |
|
| Proper documentation: |
10% |
|
| Penalty: |
late submission
(within the first 24 hours): |
-40% |
| |
(after the first 24 hours): |
-100% |
| |
program does not compile under GLUE UNIX: |
-100% |
| |
wrong file name (other than p3.c): |
-100% |
These criteria will be strictly enforced and no exceptions will
be made. Make sure that the program that you submit is named
correctly (p3.c, with lower case p) and does compile.