Answer to Question #216059 in C++ for Hemambar

Question #216059

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-18T01:24:31-0400
#include <iostream>
using namespace std;




class Matrix{
private:
	float** array2D; 
	int N;
	int M;
public:
	//constructor should allow the programmer to specify the actual dimensions of the matrix.
	Matrix(int N,int M){
		this->N=N;
		this->M=M;
		this->array2D = new float*[N];
		// dynamically allocate memory of size `M` for each row
		for (int i = 0; i < N; i++) {
			this->array2D[i] = new float[M];
		}
	}


	//putel() for taking 3 argument row index , column index and the element storing it in the corresponding location.
	void putel(int rowIndex,int columnIndex,float element){
		this->array2D[rowIndex][columnIndex] = element;
	}
	//getel() for taking 2 arguments row and column indexes and returns the elements from that location .
	float getel(int row, int column)
	{
		return this->array2D[row][column];
	}
	//overload the operators +,-,and * appropriately
	float** Matrix::operator+(Matrix otherMatrix)
	{
		float** sumMatrix=new float*[N];
		for (int i = 0; i < N; i++) {
			sumMatrix[i] = new float[N];
		}
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				sumMatrix[i][j] = array2D[i][j]+ otherMatrix.array2D[i][j];
			}
		}
		return sumMatrix;
	}
	float** Matrix::operator-(Matrix otherMatrix)
	{
		float**diffMatrix=new float*[N];
		for (int i = 0; i < N; i++) {
			diffMatrix[i] = new float[N];
		}
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				diffMatrix[i][j] = array2D[i][j]- otherMatrix.array2D[i][j];
			}
		}
		return diffMatrix;
	}


	float**  Matrix::operator*(Matrix otherMatrix)
	{
		float** productMatrix=new float*[N];
		for (int i = 0; i < N; i++) {
			productMatrix[i] = new float[N];
		}
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				productMatrix[i][j] = 0;


				for (int k = 0; k < N; k++) {
					productMatrix[i][j] += array2D[i][k]
					* (otherMatrix.array2D[k][j]);
				}
			}
		}
		return productMatrix;
	}
};
int main() {
	Matrix matrix1(3,3);
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			matrix1.putel(i,j,i+j);
		}
	}
	cout<<"Matrix 1\n";
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cout<<matrix1.getel(i,j) << " ";
		}
		cout << endl;
	}
	Matrix matrix2(3,3);
	for(int i=0;i<3;i++){
		for(int j=0;j<3;j++){
			matrix2.putel(i,j,i+j);
		}
	}
	cout<<"\nMatrix 2\n";
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cout<<matrix2.getel(i,j) << " ";
		}
		cout << endl;
	}
	//	 Display the sum of Matrices
	float** matrixSum=(matrix1+matrix2);
	cout<<"\nMatrix sum:\n";
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cout<<matrixSum[i][j] << " ";
		}
		cout << endl;
	}
	//	 Display the difference of Matrices
	float** matrixDiff=(matrix1-matrix2);
	cout<<"\nMatrix difference:\n";
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cout<<matrixDiff[i][j] << " ";
		}
		cout << endl;
	}




	//	 Display the product of Matrices
	float** matrixProduct=(matrix1*matrix2);
	cout<<"\nMatrix product:\n";
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cout<<matrixProduct[i][j] << " ";
		}
		cout << endl;
	}
	for (int i = 0; i < 3; i++) {
        delete[] matrixSum[i];
    }
    delete[] matrixSum;
	for (int i = 0; i < 3; i++) {
        delete[] matrixDiff[i];
    }
    delete[] matrixDiff;


	for (int i = 0; i < 3; i++) {
        delete[] matrixProduct[i];
    }
    delete[] matrixProduct;


	cout<<"\n\n";
	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