Lab4
Lab4
Objective:
Create a class Matrix that provides various functionalities for performing operations on a
mathematical matrix. Using this class, you will implement a menu-driven program in main()
that acts as an application for users to work with matrices.
Class Structure
Private:
o int row; // Number of rows in the matrix
o int col; // Number of columns in the matrix
o int **ptr; // Double pointer for dynamically creating a 2D array (matrix)
Constructors:
Public:
o Matrix(); // Default constructor
o Matrix(int r, int c); // Parameterized constructor: r for rows, c for columns
o Matrix(Matrix &obj); // Copy
Destructor:
int get(int i, int j); // Returns the value of the element at position (i, j) in the matrix
void set(int i, int j, int val); // Sets the value of the element at position (i, j) in
the matrix to val
Matrix Operations (member functions):
int& cell(int i, int j); // Use this function to both get and set the (i,j)th element of
the Matrix.
bool isSquare(); // Checks if the matrix is a square matrix
bool isIdentity(); // Checks if the matrix is an identity matrix
bool isDiagonal(); // Checks if the matrix is a diagonal matrix
bool isNullMatrix(); // Checks if the matrix is a null (zero) matrix
bool isUpperTriangular(); // Checks if the matrix is an upper triangular matrix
bool isLowerTriangular(); // Checks if the matrix is a lower triangular matrix
bool isEqual(const Matrix &m); // Compares the calling matrix with matrix m to see if
they have the same size and values
Matrix transpose(); // Returns the transpose of the calling matrix
Matrix addition(Matrix &m); // Adds the calling matrix and matrix m (ensure
compatibility before addition)
Matrix subtraction(Matrix &m); // Subtracts matrix m from the calling matrix (ensure
compatibility before subtraction)
Main Program:
In main(), allow the user to input two matrices and their values.
Present a menu with the following options for the user to select:
o Perform matrix operations (addition, subtraction, multiplication, transpose, etc.).
o Check properties (isSquare, isIdentity, etc.).
o Re-enter the matrices (if the user wants to change the matrices initially entered).
o Quit: The menu should run indefinitely until the user selects the option to quit.
Ensure the program properly validates matrix compatibility where necessary (e.g., for addition,
subtraction, multiplication).
Single Function to Access and Modify data in a cell (using return by reference):