Answer to Question #218867 in C++ for Hemambar

Question #218867

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


1
Expert's answer
2021-07-27T07:43:29-0400
#include <iostream>


using namespace std;


class Matrix {


private:
	double *matrix2D;
	int numberRows;
	int numberColumns;
public:
	Matrix(int numberRows, int numberColumns){
		this->numberRows = numberRows;
		this->numberColumns = numberColumns;
		if(numberRows<=0){
			this->numberRows = 1;
		}
		if(numberColumns<=0){
			this->numberColumns = 1;
		}
		this->matrix2D = new double[this->numberRows*this->numberColumns];
	}
	Matrix::~Matrix()
	{
		delete [] matrix2D;
	}
	void putel(int r, int c, double value) {
		if (r <0 || r >= numberRows) {
			r = 0;
		}
		if (c < 0  || c >= numberColumns) {
			c = 0;
		}
		matrix2D[r*this->numberColumns + c] = value;
	}
	double getel(int r, int c){
		if (r <0 || r >= this->numberRows) {
			r = 0;
		}
		if (c < 0  || c >= this->numberColumns) {
			c = 0;
		}
		return matrix2D[r*this->numberColumns + c];
	}
	Matrix* operator+(Matrix& otherMatrix) 
	{
		if (this->numberRows != otherMatrix.numberRows || this->numberColumns != otherMatrix.numberColumns) {
			cout << "Wrong matrix dimension.\n";
			return NULL;
		}
		Matrix* sum=new Matrix(this->numberRows, this->numberColumns);
		for (int i=0; i<this->numberRows*this->numberColumns; i++) {
			sum->matrix2D[i] = matrix2D[i] + otherMatrix.matrix2D[i];
		}
		return sum;
	}
	Matrix* operator-(Matrix& otherMatrix){
		if (numberRows != otherMatrix.numberRows || numberColumns != otherMatrix.numberColumns) {
			cout << "Wrong matrix dimension.\n";
			return NULL;
		}
		Matrix* difference=new Matrix(this->numberRows,this->numberColumns);
		for (int i=0; i<this->numberRows*this->numberColumns; i++) {
			difference->matrix2D[i] = matrix2D[i] - otherMatrix.matrix2D[i];
		}
		return difference;
	}
	Matrix* operator*(Matrix& otherMatrix)
	{
		if (numberColumns != otherMatrix.numberRows) {
			cout << "Wrong matrix dimension.\n";
			return NULL;
		}
		Matrix* product=new Matrix(this->numberRows,otherMatrix.numberColumns);
		for (int i=0; i<this->numberRows; i++) {
			for(int j=0; j<otherMatrix.numberColumns; j++) {
				double sum = 0.0;
				for (int k=0; k<this->numberColumns; k++) {
					sum += getel(i, k) * otherMatrix.getel(k, j);
				}
				product->putel(i, j, sum);
			}
		}
		return product;
	}


	void print()
	{
		for (int i=0; i<numberRows; i++) {
			for (int j=0; j<numberColumns; j++) {
				cout<< "\t" << getel(i, j) << " ";
			}
			cout<< endl;
		}
	}


};






int main() {
	int N,M;


	cout<<"Enter N: ";
	cin>>N;
	cout<<"Enter M: ";
	cin>>M;
	Matrix* A=new Matrix(N, M);
	Matrix* B=new Matrix(N, M);
	Matrix* C=new Matrix(M, N);
	double value;
	cout << "Matrix A:\n";
	for (int i=0; i<N; i++) {
		for (int j=0; j<M; j++) {
			cout<<"Enter ["<<i<<","<<j<<"]: ";
			cin>>value;
			A->putel(i, j,value);
		}
	}
	cout << "\nMatrix B:\n";
	for (int i=0; i<N; i++) {
		for (int j=0; j<M; j++) {
			cout<<"Enter ["<<i<<","<<j<<"]: ";
			cin>>value;
			B->putel(i, j,value);
		}
	}
	cout << "\nMatrix C:\n";
	for (int i=0; i<N; i++) {
		for (int j=0; j<M; j++) {
			cout<<"Enter ["<<i<<","<<j<<"]: ";
			cin>>value;
			C->putel(j, i,value);
		}
	}
	cout << "\nMatrix A:\n";
	A->print();
	cout << "\nMatrix B:\n";
	B->print();
	cout << "\nMatrix C:\n";
	C->print();
	Matrix* result=((*A)+(*B));
	cout << "\nA + B:\n";
	result->print();
	cout << "\nA - B:\n";
	result=((*A)-(*B));
	result->print();
	cout << "\nA * C:\n";
	result=((*A)*(*C));
	result->print();




	delete A;
	delete B;
	delete C;
	delete result;


	system("pause");


	return 0;
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS