Monday 22 February 2016

NPTEL(WEEK-5.2)

Programming Assignment 5.2: Matrix ADT

Due on 2016-02-27, 23:55 IST



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
The 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. You are supposed to implement the following functions of the class MatrixADT matrixType add(matrixType M1, matrixType M2): The function should add matrices M1 and M2 and store the result in "resultMatrix". The function should return the "resultMatrix".
matrixType subtract(matrixType M1, matrixType M2):
The function should subtract matrix M2 from M1 and store the result in "resultMatrix". The function should return the "resultMatrix".
matrixType multiply(matrixType M1, matrixType M2):
The function should multiply matrices M1 and M2 and store the result in "resultMatrix". Be careful, it is M1*M2 and not M2*M1. The function should return the "resultMatrix".

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.



PROGRAM :


#include <cstring>

#include <cstdio>

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();
};

//Intialize Result Matrix entries to zero
void MatrixADT::intializeResultMatrix(int dim){
        int i,j;
        
        resultMatrix.matDimension = dim;
        for (i=0;i<resultMatrix.matDimension;i++){
                for (j=0; j<resultMatrix.matDimension;j++){
                        resultMatrix.matValues[i][j] = 0;
                }
        }
}

CODING:

matrixType MatrixADT::add(matrixType M1, matrixType M2){
  int i=0,j=0;
  for (i=0;i<resultMatrix.matDimension;i++)
        {
            for (j=0; j<resultMatrix.matDimension;j++){
                resultMatrix.matValues[i][j] = M1.matValues[i][j] + M2.matValues[i][j];
            }
        }


        return resultMatrix;
}
//SUBTRACT
matrixType MatrixADT::subtract(matrixType M1, matrixType M2)
{
    int i=0,j=0;
  for (i=0;i<resultMatrix.matDimension;i++)
        {
            for (j=0; j<resultMatrix.matDimension;j++){
                resultMatrix.matValues[i][j] = M1.matValues[i][j] - M2.matValues[i][j];
        }
        }
    return resultMatrix;
}
//MULTIPLY
matrixType MatrixADT::multiply(matrixType M1, matrixType M2){
    int i=0,j=0,ii,jj;
  for (i=0;i<resultMatrix.matDimension;i++)
        {
            for (j=0; j<resultMatrix.matDimension;j++)
            {
                for(ii=0;ii<resultMatrix.matDimension;ii++)
                {
                    for(jj=0;jj<resultMatrix.matDimension;jj++)
                    {
                        if(j==ii)
                        {
                             resultMatrix.matValues[i][jj] += M1.matValues[i][j] * M2.matValues[ii][jj];
                        }
                    }
                }
            }
        }
        return resultMatrix;
}



PROGRAM :

int main(){ MatrixADT maX; matrixType M1, M2; char op; int i,j,dim; scanf("%d %c",&dim, &op); M1.matDimension = dim; M2.matDimension = dim; maX.intializeResultMatrix(dim); //Accept matrix entries for (i=0;i<dim;i++) for (j=0; j<dim;j++){ scanf("%d",&M1.matValues[i][j]); } for (i=0;i<dim;i++) for (j=0; j<dim;j++){ scanf("%d",&M2.matValues[i][j]); } //Call MatrixADT member function depending on operator if (op=='+'){ maX.add(M1,M2); } else if (op=='-'){ maX.subtract(M1,M2); } else{ maX.multiply(M1,M2); } maX.printResult(); return 0; } void MatrixADT::printResult(){ int i,j; for (i=0;i<resultMatrix.matDimension;i++){ for (j=0; j<resultMatrix.matDimension-1;j++){ printf("%d ",resultMatrix.matValues[i][j]); } printf("%d\n",resultMatrix.matValues[i][j]); } printf("Done"); }

No comments:

Post a Comment

How to install google-chrome in redhat without redhat subscription

Install google-chrome in redhat  Download the .rpm file of chrome https://www.google.com/chrome/thank-you.html?installdataindex=empty&st...