Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include #include using namespace std; template class Matrix { private: vector > data; size _ t rows; size _ t cols; public: / / Constructor
#include
#include
using namespace std;
template
class Matrix
private:
vector data;
sizet rows;
sizet cols;
public:
Constructor to initialize a matrix with given rows and columns
All elements initialized to zero
Matrixsizet r sizet c : rowsr colsc
data.resizerows vectorcols;
Overload operator for easy access to matrix elements
vector& operatorsizet i
return datai;
const vector& operatorsizet i const
return datai;
Overload the addition operator
Matrix operatorconst Matrix& other const
Matrix resultrows cols;
for sizet i ; i rows; i
for sizet j ; j cols; j
resultij dataij otherij;
return result;
Overload the subtraction operator
Matrix operatorconst Matrix& other const
Matrix resultrows cols;
for sizet i ; i rows; i
for sizet j ; j cols; j
resultij dataij otherij;
return result;
Overload the multiplication operator for matrix multiplication
Matrix operatorconst Matrix& other const
Matrix resultrows other.cols;
for sizet i ; i rows; i
for sizet j ; j other.cols; j
resultij;
for sizet k ; k cols; k
resultij dataik otherkj;
return result;
Overload the multiplication operator for scalar multiplication
Matrix operatorT scalar const
Matrix resultrows cols;
for sizet i ; i rows; i
for sizet j ; j cols; j
resultij dataij scalar;
return result;
Friend function to enable scalar matrix multiplication
friend Matrix operatorT scalar, const Matrix& matrix
return matrix scalar;
Overload the stream insertion operator for easy output
friend ostream& operatorostream& os const Matrix& matrix
for sizet i ; i matrix.rows; i
for sizet j ; j matrix.cols; j
os matrixij;
os endl;
return os;
;
int main
Matrix m; row size, column size & all elements are initialized to s
m; m; m;
m; m; m;
m; m; m;
Matrix m; row size, column size
m; m; m;
m; m; m;
m; m; m;
Matrix m;
m m m;
cout m endl;
m m m;
cout m endl;
m m m;
cout m endl;
m m;
cout m endl;
return ;
What should be changed in the code to get the following outcome:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started