-
Notifications
You must be signed in to change notification settings - Fork 1
W5 ‐ Matrices operation (ASSESSED)
Join the fifth workshop (the second assessed) at the dedicated LINK 💻
You are tasked with writing a C++ program that performs basic matrix operations, including addition and multiplication. Your program should demonstrate your understanding of dynamic memory allocation, pointer arithmetic, and matrix manipulation.
Instructions:
- Define a class named
Matrixwith the following private attributes:- An integer pointer named
datato store the matrix data as a 2D array. - Integer variables
rowsandcolsto store the dimensions of the matrix.
- An integer pointer named
- Implement the following methods for the Matrix class:
- A constructor that takes integer parameters
randcto initialize the matrix dimensions. It should allocate memory for the matrix data. - A destructor that deallocates memory for the matrix data.
-
int get(int row, int col): Returns the value at the given row and column indices. -
void set(int row, int col, int value): Sets the value at the given row and column indices.
- A constructor that takes integer parameters
- Implement the following operations as member methods of the Matrix class:
-
Matrix add(const Matrix &other): Returns a new matrix that is the sum of the current matrix and the other matrix. -
Matrix multiply(const Matrix &other): Returns a new matrix that is the product of the current matrix and the other matrix.
-
Test case:

Make sure to handle memory allocation, deallocation, and matrix operations correctly in your program. This assessment will help assess your understanding of memory management, pointers, and matrix operations in C++.
Note: Suppose
Note2: The addition between two matrices is allowed only if they have the same size.
Note3: Consider matrix
For