create a class Matrix the stores it in a safe 2D array .that is it it should check for array index bounds .a constructor should allow the programmer to specify the actual dimensions of the matrix. define number functions : putel() for taking 3 argument row index , column index and the element storing it in the corresponding location. getel() for taking 2 arguments row and column indexes and returns the elements from that location . overload the operators +,-,and * appropriately
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
class Matrix {
public:
Matrix(int n, int m);
~Matrix();
void putel(int row, int col, double val);
double getel(int row, int col);
Matrix operator+(Matrix& other);
Matrix operator-(Matrix& other);
Matrix operator*(Matrix& other);
void print(ostream& os);
private:
int nRow;
int nCol;
double *values;
};
Matrix::Matrix(int n, int m)
{
nRow = n>0 ? n : 1;
nCol = m>0 ? m : 1;
values = new double[n*m];
}
Matrix::~Matrix()
{
delete [] values;
}
void Matrix::putel(int row, int col, double val) {
if (row <0 || row >= nRow) {
row = 0;
}
if (col < 0 || col >= nCol) {
col = 0;
}
values[row*nCol + col] = val;
}
double Matrix::getel(int row, int col)
{
if (row <0 || row >= nRow) {
row = 0;
}
if (col < 0 || col >= nCol) {
col = 0;
}
return values[row*nCol + col];
}
Matrix Matrix::operator+(Matrix& other)
{
if (nRow != other.nRow || nCol != other.nCol) {
cerr << "Addition of incompatible matrix" << endl;
exit(1);
}
Matrix res(nRow, nCol);
for (int i=0; i<nRow*nCol; i++) {
res.values[i] = values[i] + other.values[i];
}
return res;
}
Matrix Matrix::operator-(Matrix& other)
{
if (nRow != other.nRow || nCol != other.nCol) {
cerr << "Subtraction of incompatible matrix" << endl;
exit(1);
}
Matrix res(nRow, nCol);
for (int i=0; i<nRow*nCol; i++) {
res.values[i] = values[i] - other.values[i];
}
return res;
}
Matrix Matrix::operator*(Matrix& other)
{
if (nCol != other.nRow) {
cerr << "Multiplixation of incompatible matrix" << endl;
exit(1);
}
Matrix res(nCol, other.nRow);
for (int i=0; i<nRow; i++) {
for(int j=0; j<other.nCol; j++) {
double x = 0.0;
for (int k=0; k<nCol; k++) {
x += getel(i, k) * other.getel(k, j);
}
res.putel(i, j, x);
}
}
return res;
}
void Matrix::print(ostream& ostr)
{
for (int i=0; i<nRow; i++) {
for (int j=0; j<nCol; j++) {
ostr << setw(3) << getel(i, j) << " ";
}
ostr << endl;
}
}
ostream& operator<<(ostream& ostr, Matrix& A) {
A.print(ostr);
return ostr;
}
int main()
{
srand(time(nullptr));
Matrix A(2, 3), B(2, 3), C(3, 2);
for (int i=0; i<2; i++) {
for (int j=0; j<3; j++) {
double x = rand() % 20 - 9;
A.putel(i, j, x);
x = rand() % 20 - 9;
B.putel(i, j, x);
x = rand() % 20 - 9;
C.putel(j, i, x);
}
}
cout << "Matrix A:" << endl << A << endl;
cout << "Matrix B:" << endl << B << endl;
cout << "Matrix C:" << endl << C << endl;
cout << endl << "A + B:" << endl << A + B << endl;
cout << endl << "A - B:" << endl << A - B << endl;
cout << endl << "A * C:" << endl << A * C << endl;
}
Comments
Leave a comment