NPTEL Online Course Week 5 Solution Implement a Matrix ADT Assignment
Solutions:
#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”;
}
*/
You have to implement a matrix ADT using concepts of C++ classes taught in the lectures. The input matrices would be square matrices. Theclass 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
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.Solutions:
#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”;
}
*/
No comments:
Post a Comment