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 {
private:
double *array2D;
int rows;
int columns;
public:
Matrix(int nRows, int nCols){
rows = nRows>0 ? nRows : 1;
columns = nCols>0 ? nCols : 1;
array2D = new double[nRows*nCols];
}
Matrix::~Matrix()
{
delete [] array2D;
}
void putel(int selectedRow, int selectedColumn, double element) {
if (selectedRow <0 || selectedRow >= rows) {
selectedRow = 0;
}
if (selectedColumn < 0 || selectedColumn >= columns) {
selectedColumn = 0;
}
array2D[selectedRow*columns + selectedColumn] = element;
}
double getel(int selectedRow, int selectedColumn){
if (selectedRow <0 || selectedRow >= rows) {
selectedRow = 0;
}
if (selectedColumn < 0 || selectedColumn >= columns) {
selectedColumn = 0;
}
return array2D[selectedRow*columns + selectedColumn];
}
Matrix* operator+(Matrix& otherMatrix)
{
if (rows != otherMatrix.rows || columns != otherMatrix.columns) {
cout << "Wrong matrix dimention.\n";
return NULL;
}
Matrix* matrixResult=new Matrix(rows, columns);
for (int i=0; i<rows*columns; i++) {
matrixResult->array2D[i] = array2D[i] + otherMatrix.array2D[i];
}
return matrixResult;
}
Matrix* operator-(Matrix& otherMatrix){
if (rows != otherMatrix.rows || columns != otherMatrix.columns) {
cout << "Wrong matrix dimention.\n";
return NULL;
}
Matrix* matrixResult=new Matrix(rows, columns);
for (int i=0; i<rows*columns; i++) {
matrixResult->array2D[i] = array2D[i] - otherMatrix.array2D[i];
}
return matrixResult;
}
Matrix* operator*(Matrix& other)
{
if (columns != other.rows) {
cout << "Wrong matrix dimention.\n";
return NULL;
}
Matrix* matrixResult=new Matrix(rows,other.columns);
for (int i=0; i<rows; i++) {
for(int j=0; j<other.columns; j++) {
double sum = 0.0;
for (int k=0; k<columns; k++) {
sum += getel(i, k) * other.getel(k, j);
}
matrixResult->putel(i, j, sum);
}
}
return matrixResult;
}
void display()
{
for (int i=0; i<rows; i++) {
for (int j=0; j<columns; j++) {
cout<< setw(3) << getel(i, j) << " ";
}
cout<< endl;
}
}
};
int main() {
srand(time(nullptr));
Matrix* matrix1=new Matrix(4, 6);
Matrix* matrix2=new Matrix(4, 6);
Matrix* matrix3=new Matrix(6, 4);
for (int i=0; i<4; i++) {
for (int j=0; j<6; j++) {
matrix1->putel(i, j, (rand() % 10));
matrix2->putel(i, j,(rand() % 10));
matrix3->putel(j, i,(rand() % 10));
}
}
cout << "Matrix 1:\n";
matrix1->display();
cout << "\nMatrix 2:\n";
matrix2->display();
cout << "\nMatrix 3:\n";
matrix3->display();
Matrix* matrixResult=((*matrix1)+(*matrix2));
cout << "\nMatrix 1 + Matrix 2:\n";
matrixResult->display();
cout << "\nMatrix 1 - Matrix 2:\n";
matrixResult=((*matrix1)-(*matrix2));
matrixResult->display();
cout << "\nMatrix 1 * Matrix 3:\n";
matrixResult=((*matrix1)*(*matrix3));
matrixResult->display();
delete matrix1;
delete matrix2;
delete matrix3;
delete matrixResult;
int pause;
cin>>pause;
return 0;
}
Comments
Leave a comment