For this homework exercise you will be exploring the implementation of matrix mu
ID: 3699453 • Letter: F
Question
For this homework exercise you will be exploring the implementation of matrix multiplication using C++ There are third party libraries that provide matrix multiplication, but for this homework you will be implementing your own class object and overloading some C+ operators to achieve matrix multiplication. 1. [10pts] Create a custom class called Matrix a. Private Members i. int rows; ii. int cols iii. double*data; b. Provide NO default constructor c. Constructor i. Matrix(int,int) takes rows and cols ii. Inside constructor allocate and initialize data d. Destructor iMatrix() ii. Inside destructor delete memory allocation of data completely 2. [10pts] Overload Copy constructor 3. [10pts] Overload a. For input, user inputs the values of the matrix in row then column format i. HINT: using a space you can separate column data and enter for row b. For output, use tab and return line to create a matrix-like output 4. [10pts] Overload the assignment operatorExplanation / Answer
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Matrix{
private:
int rows;
int cols;
double *data;
public:
Matrix(const int r,const int c){
rows = r;
cols = c;
data = new double[r*c];
}
Matrix(const Matrix &src) {
Matrix(src.rows,src.cols);
memcpy(data,src.data,sizeof(double)*rows*cols);
}
~Matrix(){
delete data;
}
Matrix& operator = (const Matrix &src){
Matrix(src.rows,src.cols);
memcpy((void*)data,(void*)src.data,sizeof(double)*rows*cols);
}
Matrix operator ++ (){
Matrix iden(rows,cols);
int i,j;
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
iden.data[i*cols+j]=i==j?1:0;
}
}
*this = *this + iden;
return *this;
}
Matrix operator -- (){
Matrix iden(rows,cols);
int i,j;
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
iden.data[i*cols+j]=i==j?1:0;
}
}
*this = *this - iden;
return *this;
}
Matrix operator ++ (int){
Matrix iden(rows,cols),*p=this;
int i,j;
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
iden.data[i*cols+j]=i==j?1:0;
}
}
*this = *this + iden;
return *p;
}
Matrix operator -- (int){
Matrix iden(rows,cols),*p=this;
int i,j;
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
iden.data[i*cols+j]=i==j?1:0;
}
}
*this = *this - iden;
return *p;
}
Matrix& operator += (const Matrix &b){
*this = *this + b;
return *this;
}
Matrix& operator -= (const Matrix &b){
*this = *this - b;
return *this;
}
friend istream& operator >> (istream& in,const Matrix &m);
friend ostream& operator << (ostream &out,const Matrix &m);
friend Matrix operator + (const Matrix &a,const Matrix &b);
friend Matrix operator - (const Matrix &a,const Matrix &b);
friend Matrix operator * (const Matrix &a,const Matrix &b);
};
istream& operator >> (istream& in,const Matrix &m){
int i,j;
for(i=0;i<m.rows;i++){
for(j=0;j<m.cols;j++){
in>>m.data[i*m.cols+j];
}
}
return in;
}
ostream& operator << (ostream &out,const Matrix &m){
int i,j;
for(i=0;i<m.rows;i++){
out<<m.data[i*m.cols];
for(j=1;j<m.cols;j++){
out<<' '<<m.data[i*m.cols+j];
}
out<<' ';
}
return out;
}
Matrix operator + (const Matrix &a,const Matrix &b){
Matrix res(a.rows,a.cols);
int i,j;
for(i=0;i<res.rows;i++){
for(j=0;j<res.cols;j++){
res.data[i*res.cols+j]=a.data[i*a.cols+j]+b.data[i*b.cols+j];
}
}
return res;
}
Matrix operator - (const Matrix &a,const Matrix &b){
Matrix res(a.rows,a.cols);
int i,j;
for(i=0;i<res.rows;i++){
for(j=0;j<res.cols;j++){
res.data[i*res.cols+j]=a.data[i*res.cols+j]-b.data[i*res.cols+j];
}
}
return res;
}
Matrix operator * (const Matrix &a,const Matrix &b){
Matrix res(a.rows,b.cols);
int i,j,k;
if(a.cols!=b.rows){
cerr<<"Ensure Proper dimensions before multiplication";
exit(1);
}
for(i=0;i<res.rows;i++){
for(j=0;j<res.cols;j++){
res.data[i*res.cols+j]=0;
for(k=0;k<a.cols;k++){
res.data[i*res.cols+j]+=a.data[i*a.cols+k]*b.data[k*b.cols+j];
}
}
}
return res;
}
int main(){
int n,m;
cout<<"Enter dimensions:";
cin>>n>>m;
Matrix mat(n,m);
cout<<"Enter values: ";
cin>>mat;
//print the Matrix
cout<<mat;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.