Many mathematical problems require the addition, subtraction, and multiplication
ID: 3542170 • Letter: M
Question
Many mathematical problems require the addition, subtraction, and multiplication of two
matrices. Write an ADT Matrix. You may use the following declarations:
#define MAX_ROWS 10
#define MAX_COLS 10
struct matrix
{
int values[MAX_ROWS][MAX_COLS];
int numRows;
int numCols;
};
typedef struct matrix MatrixType;
MatrixType *CreateMatrix(void);
void DestroyMatrix(MatrixType *m);
void MakeEmpty(MatrixType *m);
void SetSize(MatrixType *m, int rowSize, int colSize);
void StoreItem(MatrixType *m, int item, int row, int col);
MatrixType* Add(MatrixType *m1, MatrixType *m2);
MatrixType* Subtract(MatrixType *m1, MatrixType *m2);
MatrixType* Multiply(MatrixType *m1, MatrixType *m2);
void Print(MatrixType *m, FILE *fp);
int MultCompatible(MatrixType *m1, MatrixType *m2);
int AddSubCompatible(MatrixType *m1, MatrixType *m2);
Before you start looking at how to implement this ADT, you must determine the appropriate
preconditions and postconditions for each operation. Note that the ADT provides the functions
(MultCompatible() and AddSubCompatible()) to determine if the binary matrix operations are
possible.
Write a test program to test all the functions of your MatrixType. The test program must ensure
the preconditions of the ADT functions of MatrixType. Print an error message if a precondition is
violated.
Explanation / Answer
The menu should contain the following options.
GetNewMatrix <matrix>
Number of rows and number of columns are on the next line.
Number of column values on the next number of rows lines
AddMatrices <matrix> <matrix> <matrix>
Add first and second, leaving the result in the third
SubMatrices <matrix> <matrix> <matrix>
Subtract second from first, leaving the result in the third
MultiplyMatrices <matrix> <matrix> <matrix>
Multiply first and second, leaving the result in the third
PrintMatrix <matrix>
Print the matrix one row per line on DataOut
Quit
Processing Notes
1. <matrix> is a number between 0 and 9. This value is used as an index into an array of
MatrixType.
2. The main function must include a Switch statement where the case expression is a userdefined enumeration type. This means that the command is recognized and its enumeration
equivalent is sent back to be used in the case statement.
3 The driver must ensure the preconditions of the member functions of MatrixType. Throw
an exception if an error occurs and continue processing.
Deliverables
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.