I wish someone can help me out with this code in c++ Please implement the follow
ID: 3710241 • Letter: I
Question
I wish someone can help me out with this code in c++
Please implement the following operators in class Matrix.
Overload the following operators
1. << // Matrix output
2. >> // Matrix input
3. = // assignment operator
4. + // Matrix + Matrix
5. + // Matrix + double,
// all values plus the double value
6. += // m1 = m1 + m2
7. ++ // m5++, all values plus 1.0
8. * // Matrix * Matrix
9.() // implement the transpose function,
// return the transpose of itself
10.== // test if two matrices contain the same values
__________________________________Matrix.h_____________________
#ifndef Matrix_h
#define Matrix_h
#include <iostream>
using namespace std;
class Matrix {
private:
double ** data;
int row;
int col;
void setUp(int, int);
public:
Matrix();// default constructor
Matrix(int, int);//constructor with input r row and c column
Matrix(int, int, double [], int);//populate matrix with a one dimensional array
Matrix(const Matrix &);//copy constructor
~Matrix();// destructor
void setData(int, int, double);//set matrix cell(r,c) to a double data value
int getRow() const;//get matrix row
int getCol() const;//get matrix column
double getData(int, int) const;//get matrix cell (r,c) data value
Matrix add(const Matrix &);//add an incoming matrix with actual class matrix
Matrix multiply(const Matrix &);//multiply an incoming matrix with actual class matrix
Matrix transpose();//return current class matrix transpose
void displayData() {
for (int i = 0;i<row; i++) {
for (int j = 0;j<col; j++) {
cout<<data[i][j]<<" ";
}
cout<<""<<endl;
}
}
};
#endif /* Matrix_h */
__________________________Matrix.cpp__________________
#include "Matrix.h"
#include <iostream>
using namespace std;
void Matrix::setUp(int r, int c){
row = r;
col = c;
data = new double*[row];
for(int i = 0; i < row; ++i)
data[i] = new double [col];
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
data[i][j] = 0;
}
Matrix:: Matrix(){//default constructor
setUp(2,2);
}
Matrix::Matrix(int r, int c){//constructor with input r row and c column
setUp(r,c);
}
Matrix::Matrix(int r, int c, double a[], int size){//populate matrix with a one dimensional array
setUp(r, c);
int k = 0;
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
data[i][j] = a[k++];
}
Matrix::Matrix(const Matrix &m)//copy constructor
{
setUp(m.row, m.col);
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
data[i][j] = m.data[i][j];
}
Matrix::~Matrix()// destructor
{
for(int i = 0; i < row; i++)
delete[] data[i];
delete[] data;
}
void Matrix::setData(int r , int c, double val) //set matrix cell(r,c) to a double data value
{
data[r][c] = val;
}
int Matrix::getRow() const//get matrix row
{
return row;
}
int Matrix::getCol() const//get matrix column
{
return col;
}
double Matrix::getData(int r , int c) const//get matrix cell (r,c) data value
{
return data[r][c];
}
Matrix Matrix::add(const Matrix &m)//add an incoming matrix with actual class matrix
{
Matrix result(row, col);
for(int i = 0; i< row; i++)
for(int j = 0; j < col; j++)
result.data[i][j] = data[i][j] + m.data[i][j];
return result;
}
Matrix Matrix::multiply(const Matrix &m)//multiply an incoming matrix with actual class matrix
{
Matrix result(row, m.col);
for(int i = 0; i < row; i++)
{
for(int j = 0; j < m.col; j++)
{
for(int k = 0; k < col; k++)
result.data[i][j] += data[i][k] * m.data[k][j];
}
}
return result;
}
Matrix Matrix::transpose(){//return current class matrix transpose
Matrix result(col, row);
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
result.data[j][i] = data[i][j];
return result;
}
--------------------------------------------------demo.cpp--------------( to check the implementation of operators)
#include "Matrix.h"
#include<iostream>
using namespace std;
int main()
{
Matrix m1;
double d[9] = {1,2,3,4,5,6,7,8,9};
Matrix m2(3,3,d,9);
Matrix m3(m2);
cout<<"Matrix m1 is : "<<endl;
m1.displayData();
cout<<"Matrix m2 is : "<<m2<<endl;
cout<<"Matrix m3 is : "<<m3<<endl;
if(m2==m3)
cout<<"m2 and m3 are the same.";
else
cout<<"m2 and m3 are different";
m3.setData(1,1,99);
cout<<"m3 after setup is : "<<m3<<endl;
if(m2==m3)
cout<<"m2 and m3 are the same.";
else
cout<<"m2 and m3 are different";
Matrix m4(3,3);
cout<<"Please enter 9 doubles for matrix m4:"<<endl;
cin>>m4;
cout<<"Matrix m4 is : "<<m4<<endl;
Matrix m5 = m2+m3;
cout<<"m2+m3 is : "<<m5<<endl;
m5 = m2 + 3;
cout<<"m2 + 3 is : "<<m5<<endl;
m5 += m2;
cout<<"m2 + m2 + 3 is : "<<m5<<endl;
m5++;
cout<<"m2 + m2 + 4 is : "<<m5<<endl;
m5 = m2 * m3;
cout<<"m2 * m3 is : "<<m5<<endl;
m5 = m4();
cout<<"transpose of m2 is : "<<m5<<endl;
cout<<"number of matrices now is : "<<Matrix:: getCount() <<endl;
return 0;
}
Explanation / Answer
Its not good to implement every thing n a simple progarm so i am giving it individuall if you want as onee the kindly culb everything.
Multiplication of matrices:
Transpose of a matrix:
Addition of matrices:
Matrix eqaulity:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],b[3][3];
cout<<"enter first matrix row wise ";
for(int r=0;r<3;++r)
{
for(int q=0;q<3;++q)
{
cin>>a[r][q];
}
cout<<" ";
}
cout<<"enter second matrix row wise";
for(r=0;r<3;++r)
{
for(int q=0;q<3;++q)
{
cin>>b[r][q];
}
cout<<" ";
}
int flag=0;
for(int c=0;c<3;++c)
{
for(int o=0;o<3;++o)
{if(a[c][o]!=b[c][o])
{
flag=1;
break;
}
}
if(flag==1)
break;
}
if(flag==1)
cout<<"matrices are not equal";
else if(flag==0)
cout<<"matrices are equal";
getch ();
}
Thanks.