0% found this document useful (0 votes)
9 views

Lab4

Lab for c++

Uploaded by

akashsandhu4x4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab4

Lab for c++

Uploaded by

akashsandhu4x4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 4 (45 points)

Object Oriented Programming

Task: Matrix Class and Application

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

Data Members (Attributes/Variables):

 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:

 ~Matrix(); // Destructor to properly clean up dynamic memory (given to be studied at home)

Getter and Setter (member functions):

 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)

Not part of lab, you may try at home

 Matrix multiplication(Matrix &m); // Multiplies the calling matrix with matrix m


(ensure compatibility before multiplication)

Input/Output Functions (member functions):

 void inputMatrix(); // Takes user input for the matrix (row-wise)


 void outputMatrix(); // Prints the matrix in row-wise order

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):

You might also like