Please help me understand :) Please comment all code and follow instructions car
ID: 3754173 • Letter: P
Question
Please help me understand :) Please comment all code and follow instructions carefully, making sure the code works Build two grids which are each X wide and Y long where Xand Y are user inputs to determine the size of the grids. Fill each cell of both grids with a '0'. Randomly fill 1/3rd of the grid's cells with a '1'. You must randomly fill each of the two grids separately so they do not have all the same squares filled with '1'. Compare the two grids (square by square comparison) to find squares which have a '1 in both grids. Create a third grid which contains a '1' in squares where both of the compared grids contain a '1' and has a '0' in any other square Example in a 2 by 2 grid Grid 1 Grid 2 Grid 3 (Result) For this assignment, you will need to create a Grid Class and the appropriate methods to perform the above operations. Also, please demonstrate the usage of the Grid class and its methods from main function. Documenting and submitting programs This assignment should be submitted electronically before the due date and time. We're going to use Canvas file upload system for submission. Click the link at the respective module page in Canvas. Each programming assignment should include your source code and makefile (makefile all lowercase, nottxt, not cmake)Explanation / Answer
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <vector>
template <typename T>
class Matrix {
private:
std::vector<std::vector<T> > mat;
unsigned rows;
unsigned cols;
public:
Matrix(unsigned _rows, unsigned _cols);
Matrix(const Matrix<T>& rhs);
virtual ~Matrix();
// Operator overloading, for "standard" mathematical matrix operations
Matrix<T>& operator=(const Matrix<T>& rhs);
// Matrix mathematical operations
Matrix<T> operator&(const Matrix<T>& rhs);
// Access the individual elements
T& operator()(const unsigned& row, const unsigned& col);
const T& operator()(const unsigned& row, const unsigned& col) const;
// Access the row and column sizes
unsigned get_rows() const;
unsigned get_cols() const;
};
template<typename T>
Matrix<T>::Matrix(unsigned _rows, unsigned _cols) {
mat.resize(_rows);
for (unsigned i=0; i<mat.size(); i++) {
mat[i].resize(_cols, 0);
}
rows = _rows;
cols = _cols;
std::srand(std::time(nullptr)); // use current time as seed for random generator
unsigned count = 0,limit = (rows*cols)/3;
for (unsigned i=0; i<rows; i++){
for (unsigned j=0; j<cols;j++){
if (std::rand() % 3 == 0){
mat[i][j] = 1;
count++;
}
}
if (count > limit)
break;
}
}
// Copy Constructor
template<typename T>
Matrix<T>::Matrix(const Matrix<T>& rhs) {
mat = rhs.mat;
rows = rhs.get_rows();
cols = rhs.get_cols();
}
// (Virtual) Destructor
template<typename T>
Matrix<T>::~Matrix() {}
// Assignment Operator
template<typename T>
Matrix<T>& Matrix<T>::operator=(const Matrix<T>& rhs) {
if (&rhs == this)
return *this;
unsigned new_rows = rhs.get_rows();
unsigned new_cols = rhs.get_cols();
mat.resize(new_rows);
for (unsigned i=0; i<mat.size(); i++) {
mat[i].resize(new_cols);
}
for (unsigned i=0; i<new_rows; i++) {
for (unsigned j=0; j<new_cols; j++) {
mat[i][j] = rhs(i, j);
}
}
rows = new_rows;
cols = new_cols;
return *this;
}
// And Operation on two matrices
template<typename T>
Matrix<T> Matrix<T>::operator&(const Matrix<T>& rhs) {
Matrix result(rows, cols);
for (unsigned i=0; i<rows; i++) {
for (unsigned j=0; j<cols; j++) {
result(i,j) = this->mat[i][j] & rhs(i,j);
}
}
return result;
}
// Access the individual elements
template<typename T>
T& Matrix<T>::operator()(const unsigned& row, const unsigned& col) {
return this->mat[row][col];
}
// Access the individual elements (const)
template<typename T>
const T& Matrix<T>::operator()(const unsigned& row, const unsigned& col) const {
return this->mat[row][col];
}
// Get the number of rows of the matrix
template<typename T>
unsigned Matrix<T>::get_rows() const {
return this->rows;
}
// Get the number of columns of the matrix
template<typename T>
unsigned Matrix<T>::get_cols() const {
return this->cols;
}
int main(){
Matrix<int> mat1(5,5);
Matrix<int> mat2(5,5);
Matrix<int> mat3 = mat1&mat2;
for (int i=0; i<mat1.get_rows(); i++) {
for (int j=0; j<mat1.get_cols(); j++) {
std::cout << mat1(i,j) << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
for (int i=0; i<mat2.get_rows(); i++) {
for (int j=0; j<mat2.get_cols(); j++) {
std::cout << mat2(i,j) << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
for (int i=0; i<mat3.get_rows(); i++) {
for (int j=0; j<mat3.get_cols(); j++) {
std::cout << mat3(i,j) << " ";
}
std::cout << std::endl;
}
return 0;
}