Answered step by step
Verified Expert Solution
Link Copied!

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;
size_t rows;
size_t cols;
public:
// Constructor to initialize a matrix with given rows and columns
// All elements initialized to zero
Matrix(size_t r, size_t c) : rows(r), cols(c){
data.resize(rows, vector(cols,0));
}
// Overload operator() for easy access to matrix elements
vector& operator[](size_t i){
return data[i];
}
const vector& operator[](size_t i) const {
return data[i];
}
// Overload the addition operator
Matrix operator+(const Matrix& other) const {
Matrix result(rows, cols);
for (size_t i =0; i < rows; ++i){
for (size_t j =0; j < cols; ++j){
result[i][j]= data[i][j]+ other[i][j];
}
}
return result;
}
// Overload the subtraction operator
Matrix operator-(const Matrix& other) const {
Matrix result(rows, cols);
for (size_t i =0; i < rows; ++i){
for (size_t j =0; j < cols; ++j){
result[i][j]= data[i][j]- other[i][j];
}
}
return result;
}
// Overload the multiplication operator for matrix multiplication
Matrix operator*(const Matrix& other) const {
Matrix result(rows, other.cols);
for (size_t i =0; i < rows; ++i){
for (size_t j =0; j < other.cols; ++j){
result[i][j]=0;
for (size_t k =0; k < cols; ++k){
result[i][j]+= data[i][k]* other[k][j];
}
}
}
return result;
}
// Overload the multiplication operator for scalar multiplication
Matrix operator*(T scalar) const {
Matrix result(rows, cols);
for (size_t i =0; i < rows; ++i){
for (size_t j =0; j < cols; ++j){
result[i][j]= data[i][j]* scalar;
}
}
return result;
}
// Friend function to enable scalar * matrix multiplication
friend Matrix operator*(T scalar, const Matrix& matrix){
return matrix * scalar;
}
// Overload the stream insertion operator for easy output
friend ostream& operator<<(ostream& os, const Matrix& matrix){
for (size_t i =0; i < matrix.rows; ++i){
for (size_t j =0; j < matrix.cols; ++j){
os << matrix[i][j]<<"";
}
os << endl;
}
return os;
}
};
int main(){
Matrix m1(3,3); // row size, column size & all elements are initialized to 0s
m1[0][0]=1; m1[0][1]=2; m1[0][2]=3;
m1[1][0]=4; m1[1][1]=5; m1[1][2]=6;
m1[2][0]=7; m1[2][1]=8; m1[2][2]=9;
Matrix m2(3,3); // row size, column size
m2[0][0]=9; m2[0][1]=8; m2[0][2]=7;
m2[1][0]=6; m2[1][1]=5; m2[1][2]=4;
m2[2][0]=3; m2[2][1]=2; m2[2][2]=1;
Matrix m(3,3);
m = m1+ m2;
cout << m << endl;
m = m1- m2;
cout << m << endl;
m = m1* m2;
cout << m << endl;
m =10* m1;
cout << m << endl;
return 0;
}
What should be changed in the code to get the following outcome:
101010
101010
101010
864
20-2
-4-6-8
302418
846954
13811490
102030
405060
708090

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions

Question

Do you set targets to reduce complaints?

Answered: 1 week ago