YOU HAVE TO IMPLEMENT A MATRIX ADT USING CONCEPTS OF C++ CLASSES TAUGHT IN THE LECTURES. THE INPUT MATRICES WOULD BE SQUARE MATRICES. THE CLASS MUST SUPPORT THE FOLLOWING FUNCTIONS:
1. MATRIX ADDITION
2. MATRIX SUBTRACTION
3. MATRIX MULTIPLICATION
YOUR PROGRAM SHOULD TAKE AS INPUT: DIMENSION OF A SQUARE MATRIX N, TWO MATRICES OF SIZE N X N WITH INTEGER VALUES, AND ONE OPERATOR SYMBOL (+, - ,*). IT MUST PERFORM THE CORRESPONDING OPERATION USING MEMBER FUNCTIONS OF MATRIX CLASS.
INPUT:
IN THE FIRST LINE, ONE INTEGER WHICH IS THE DIMENSION OF THE MATRIX AND ONE OPERATOR (ONE OF +, - OR *)
TWO NXN MATRICES ONE AFTER THE OTHER, SUPPLIED ONE ROW AT A TIME.
OUTPUT:
RESULTANT MATRIX AFTER PERFORMING THE SPECIFIED OPERATION, ONE ROW AT A TIME. FOR SUBTRACTION, IF A IS THE FIRST MATRIX AND B IS THE SECOND MATRIX, PERFORM A-B.
CONSTRAINTS:
THE INPUTS WILL SATISFY THE FOLLOWING CONSTRAINTS:
1<=N<=10 THERE IS NO NEED TO VALIDATE THE VALUE OF N. THERE ARE NO CONSTRAINTS ON THE VALUES OF THE ENTRIES OF THE MATRICES.
#include <iostream>
#include <cstring>
using namespace std;
struct matrixType{
int matDimension;
int matValues[10][10];
};
class MatrixADT{
private:
matrixType resultMatrix;
public:
//Member function declarations
void intializeResultMatrix(int);
matrixType add(matrixType, matrixType);
matrixType subtract(matrixType,matrixType);
matrixType multiply(matrixType,matrixType);
void printResult();
};
//Member functions of Matrix class to be defined here
matrixType MatrixADT::add(matrixType M1, matrixType M2){
//Insert code here
int i,j,temp=0;
matrixType mat;
temp=resultMatrix.matDimension;
for(i=0;i<temp;i++){
for(j=0;j<temp;j++)
{
resultMatrix.matValues[i][j]=M1.matValues[i][j]+M2.matValues[i][j];
mat.matValues[i][j]=resultMatrix.matValues[i][j];
}
}
return mat;
}
matrixType MatrixADT::subtract(matrixType M1, matrixType M2){
//Insert code here
int i,j,temp=0;
matrixType mat;
temp=resultMatrix.matDimension;
for(i=0;i<temp;i++){
for(j=0;j<temp;j++)
{
resultMatrix.matValues[i][j]=M1.matValues[i][j]-M2.matValues[i][j];
mat.matValues[i][j]=resultMatrix.matValues[i][j];
}
}
return mat;
}
matrixType MatrixADT::multiply(matrixType M1, matrixType M2){
//Insert code here
int i,j,temp=0,k;
matrixType mat;
temp=resultMatrix.matDimension;
for(i=0;i<temp;i++){
for(j=0;j<temp;j++)
{
mat.matValues[i][j]=0;
for(k=0;k<temp;k++)
{
resultMatrix.matValues[i][j]=mat.matValues[i][j]+(M1.matValues[i][k]*M2.matValues[k][j]);
mat.matValues[i][j]=resultMatrix.matValues[i][j];
}
}
}
return mat;
}
void MatrixADT::intializeResultMatrix(int dim){
resultMatrix.matDimension=dim;
/*for(i=0;i<dim;i++){
for(j=0;j<dim;j++)
{
cin>>resultMatrix.matValues[i][j];
}
}*/
}
int main(){
MatrixADT maX;
matrixType M1, M2;
char op;
int dim,i,j;
cin>>dim>>op;
//intialize the dimension for the matrices
maX.intializeResultMatrix(dim);
//enter value for matrix M1
for(i=0;i<dim;i++){
for(j=0;j<dim;j++)
{
cin>>M1.matValues[i][j];
}
}
//enter value for matrix M2
for(i=0;i<dim;i++){
for(j=0;j<dim;j++)
{
cin>>M2.matValues[i][j];
}
}
switch(op)
{
case '+': maX.add(M1,M2);
break;
case '-': maX.subtract(M1,M2);
break;
case '*': maX.multiply(M1,M2);
break;
}
/*Enter your code here to accept two input matrices as instances of class Matrix and perform the operations using member functions, display the result matrix using member function*/
/* DO NOT EDIT the code below; if you edit it, your program will not give correct output
maX.printResult();
}
void MatrixADT::printResult(){
int i,j;
for (i=0;i<resultMatrix.matDimension;i++){
for (j=0; j<resultMatrix.matDimension-1;j++){
cout<<resultMatrix.matValues[i][j]<<" ";
}
cout <<resultMatrix.matValues[i][j]<<"\n";
}
cout <<”Done”;
}
*/