Sie sind auf Seite 1von 11

#include<iostream> #include<cstdlib> using namespace std; //--------------------------------------------------------------------------------------------------------------class Matrix { private: int **data; //A two dimensional array of integers

dynamically allocated based on //rows and cols int row; //number of rows in matrix int col; //number of columns matrix public: Matrix (int row=3, int col=3); //----------Parameterized/Default constr uctor, create row*col matrix ,create 3*3 //---------if not specified and initialize it with zero. Matrix (Matrix &); //----------------------Copy constructor ~Matrix () //------------------------------Destructor: Free any mem ory resources occupied by an object. { for(int i=0;i<row;i++) delete [] data[i]; delete []data; data = NULL; } void input (); //--------------------------Takes input of a Matrix from user void output () const; //------------------Display matrix void setAt (int val, int row, int col); //--For setting some value (val) at a particular location of matrix int getAt (int row, int col) const; //------For getting some value a t a particular location of matrix bool isIdentity () const; //--------------return true if the matri x is an identity matrix; false otherwise i.e if all elements of main diagonal is 1 and other elements are zero //-------------If aij = 0 for i != j and aij = 1 for all i = j is identity matrix bool isRectangular () const; ot equal to number of columns. //---------In which number of rows are n

bool isDiagonal () const; //-------------Return true if the matrix is a diagonal matrix; false otherwise //-------------I f aij = 0 for all i != j and at least one aij != 0 for i = j; is //-------------d

iagonal matrix bool isNullMatrix () const; is a null matrix; false otherwise //-------------Return true if the matrix //-------------A matrix whose each element is zero is null matrix bool isTriangular () const; //-------------Return true if the matrix is a Triangular matrix; false otherwise //-------------A matrix that is either Lower-Triangular or Upper-Triangular bool isEqual(const Matrix& c2 ) const; //-Return true if *this and c2 a re equal otherwise false. bool isSymmetric (); //-----------------Return true if the matrix is a Symmetric matrix; false otherwise //-----------------A mat rix is a symmetric matrix if At = A void Transpose (); matrix. //---------------------Take the transpose of the //-Add *this (calling object) an

Matrix add (const Matrix& c2) const; d c2 and return the result in a //-new Matrix object.

Matrix minus (const Matrix& c2) const; //-Subtract c2 from *this (calli ng object) and return the result in a //-new Matrix object. Matrix multiply (const Matrix& c2) const; (calling object) and return the result in a //new Matrix object. }; //--------------------------------------------------------------------------------------------------------------//------------------------Main----------------int main() { char pauseBreak; cout << endl << "\t\tThis Program Shows implimentation of Matrix functio ns."<< endl; cout << " ___________________________________________________________ ___________" <<endl << endl; Matrix obj1; //First Matrix Matrix obj2; //Second Matrix Matrix obj3(2,4); //Third Matrix /* obj1.input(); cout << "First Matrix:" << endl; obj1.output(); //Multiply c2 from *this

obj2.input(); cout << "Second Matrix:" << endl; obj2.output(); */ obj3.input(); cout << "Third Matrix:" << endl; obj3.output(); if (obj3.isDiagonal()) cout << "diagonal"; else cout << "not diagonal"; obj3.Transpose(); cout << "\nAfter transposing \n"; obj3.output(); Matrix obj4(obj1); //Forth Matrix cout << "Fourth Matrix that is equal to first matrix:" << endl; obj4.output(); cout << endl <<"Press Enter to continue..."; cin.get(pauseBreak); // to pause screen output cin.get(pauseBreak); cout << endl << "Following functions are calculated by First and Second Matrix" << endl; cout << "First Matrix:" << endl; obj1.output(); cout << "Second Matrix:" << endl; obj2.output(); Matrix multiplyResult(obj1.multiply(obj2)); uses copy contructor cout << "Product of Matrices :" << endl; rst and second matrix //MultiplyResult Matrix //to store product of fi

multiplyResult.output(); Matrix sumResult(obj1.add(obj2)); //sumResult Matrix uses copy contruct or //to store Sum of first and second matrix Matrix minusResult(obj1.minus(obj2)); ontructor //to store Difference of first and second matrix cout << endl <<"Press Enter to continue..."; cin.get(pauseBreak); // to pause screen output cout << "Addition Result : " <<endl; sumResult.output(); //Sum Output cout << "Substraction Result : " <<endl; minusResult.output(); //Difference Result if(obj1.isEqual(obj2)) //Checks if matrices are equal //minusResult Matrix uses copy c

cout << "Both are equal"<<endl; else cout << "Both Matrices are not equal"<<endl; cout << endl <<"Press Enter to continue..."; cin.get(pauseBreak); // to pause screen output cout << "Executing Matrix functions for First Matrix" << endl; obj1.output(); if(obj1.isRectangular()) //Checks if Order of matrix is same cout << "It is rectangular matrix"<<endl; else cout << "It is not rectangular matrix"<<endl; if(obj1.isIdentity()) //checks if matrix contains all 0's in non-diago nal and 1's in main diagonal cout << "It is Identity matrix"<<endl; else cout << "It is not Identity matrix"<<endl; if(obj1.isNullMatrix()) //checks if matrix contains all 0's cout << "It is Null matrix"<<endl; else cout << "It is not Null matrix"<<endl; if(obj1.isDiagonal())//checks if matrix contains all 0's in non-diagonal and at least one non-zero in main diagonal cout << "It is Diagonal matrix"<<endl; else cout << "It is not Diagonal matrix"<<endl; if(obj1.isTriangular())//checks if matrix is upper triangular or lower t riangular or both cout << "It is Triangular matrix"<<endl; else cout << "It is not Triangular matrix"<<endl; if(obj1.isSymmetric())//checks if matrix and its transpose are equal cout << "It is Symmetric matrix"<<endl; else cout << "It is not Symmetric matrix"<<endl; cout << "Taking Transpose" << endl; obj1.Transpose(); //converts rows into columns cout << "After Transpose" << endl; obj1.output(); cout << endl << "Press Enter to Exit..."; cin.get(pauseBreak); } //--------------------------------------------------------------------------------------------------------------//-------------------Class functions body //-------------Parameterized/Default constructor ----------------

//create row*col matrix ,create 3*3 if not specified //and initialize it with zero. Matrix::Matrix(int row,int col) { if(row <= 0 || col <= 0) { cout << "Number of row or columns cannot be negative, so 3x3 mat rix is created" << endl; row = col = 3; } this->row=row; this->col=col; data = new int* [row]; //Memory Allocation of int pointers for(int i=0;i<row;i++) data[i] = new int[col]; //Memory Alloction of 2D array for(int i=0;i<row;i++) //Data initializing for(int j=0;j<col;j++) data[i][j] = 0; } /*-------------------------------------------------------------*/ //----------Output Function------------void Matrix::output() const { cout << "Matrix[" << row << "][" << col <<"] = "; for(int i=0;i<row;i++) { for(int j=0;j<col;j++) cout << data[i][j] << " "; cout << endl << "\t "; } cout << endl; } /*-------------------------------------------------------------*/ //----------Copy constructor------------Matrix::Matrix (Matrix & obj) { this->row=obj.row; this->col=obj.col; data = new int* [row]; //Memory Allocation of int pointers for(int i=0;i<row;i++) data[i] = new int[col]; //Memory Allocation of 2D array for(int i=0;i<this->row;i++) //Data initializing for(int j=0;j<this->col;j++) data[i][j] = obj.data[i][j]; } /*-------------------------------------------------------------*/ //----------Input Function-------------void Matrix::input()

{ char temp[10]; //character array cout << "Enter values for Matrix " << row << "X" << col << endl; cout << "Enter Integer value at matrix index: "; for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { cout << "[" << i+1 << "]" << "[" << j+1 << "] = "; cin >> temp; data[i][j]=atoi(temp); // cout << endl <<"\t\t\t\t } } cout << endl; } /*-------------------------------------------------------------*/ //----------Set function-------------//For setting some value (val) at a particular location of matrix void Matrix::setAt (int val, int row, int col) { if(row > this ->row || col > this->col) cout << "Index does not exist" << endl; else data[row-1][col-1] = val; } /*--------------------------------------------------------------*/ //----------get function-------------//For getting some value at a particular location of matrix int Matrix::getAt(int row, int col) const { if(row>this->row || col >this->col) cout << "Index does not exist" << endl; else return this->data[row-1][col-1]; } /*-------------------------------------------------------------*/ //----------Rectangular Check Function------------//In which number of rows are not equal to number of columns. bool Matrix::isRectangular () const { if(row!=col) return true; return false; } /*-------------------------------------------------------------*/ //----------Identity Check Function----"; //To avoid invalid entry of data

--------//return true if the matrix is an identity matrix; false otherwise //If aij = 0 for all i != j and aij = 1 for all i = j is identity matrix bool Matrix::isIdentity () const { if(isRectangular()) return false; for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { if(i!=j && data[i][j] != 0) return false; if(i==j && data[i][j] != 1) return false; } } return true; } /*-------------------------------------------------------------*/ //----------Diagonal Check Function------------//Return true if the matrix is a diagonal matrix; false otherwise //If aij = 0 for all i != j and at least one aij != 0 for i = j; is //diagonal matrix bool Matrix::isDiagonal () const { if(isRectangular())//to check if it is square matrix return false; bool nonZero=false; for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { if(i!=j && data[i][j]!=0) al elements are zero return false; if(i==j && data[i][j]!=0) e diagonal elements non-zero nonZero=true; }//end for(int j=0;j<col;j++) }//end for(int i=0;i<row;i++) return nonZero; } /*-------------------------------------------------------------*/ //----------NULL Check Function------------//Return true if the matrix is a null matrix; false otherwise //A matrix whose each element is zero is null matrix

//to check if non diagon

//to check if atleast on

bool Matrix::isNullMatrix () const { for(int i=0;i<row;i++) //Nested loop to check if any elements i s not zero for(int j=0;j<col;j++) if(data[i][j]!=0) return false; return true; } /*-------------------------------------------------------------*/ //----------Equality Check Function------------//Return true if *this and c2 are equal otherwise false. bool Matrix::isEqual(const Matrix& c2 ) const { if(this->row != c2.row || this->col != c2.col) //checks if rows and col umns of both matrices are equal return false; for(int i=0;i<row;i++) //Nested loop to checks if data is equal for(int j=0;j<col;j++) if(this->data[i][j] != c2.data[i][j]) return false; return true; } /*-------------------------------------------------------------*/ //----------Triangular Check Function------------//Return true if the matrix is a Triangular matrix; false otherwise //A matrix that is either Lower-Triangular or Upper-Triangular bool Matrix::isTriangular () const { if(row == 1 && col == 1) // 1x1 matrix can not be triangular return false; if(this->isRectangular() || this->isNullMatrix()) //to checks if i t is rectangualar or null matrix return false; if(this->isIdentity() || this->isDiagonal()) //to checks if it is Ide ntity or Diagonal matrix return true; int DiagonalZeroTrue=0,DiagonalZeroFalse=0; for(int i=0;i<row;i++) if all diagonal elements are zero { if(data[i][i] == 0) DiagonalZeroTrue++; else DiagonalZeroFalse++; } if(DiagonalZeroFalse==0) return false;

//checks

bool diagonalCheck=false; int countUpper,countLower; countUpper=countLower=0; for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { if(i==j) lumns are equal { diagonalCheck=true; if(j<col-1) j++; umns, because current position is on diagonal }

//if rows and co

//increments col

if(diagonalCheck==true && data[i][j] == 0 && i!=j) countUpper++; //counts zeroes on upper side of diagonal else if(diagonalCheck==false && data[i][j] == 0 && i!=j) countLower++; //counts zeroes on Lower side of diagonal } diagonalCheck=false; } int noOfZero=((row*col) - row )/2; if(countUpper == noOfZero || countLower == noOfZero) return true; } /*-------------------------------------------------------------*/ //----------Sum Function-------------//Add *this (calling object) and c2 and return the result in a //new Matrix object. Matrix Matrix::add (const Matrix& c2) const { Matrix sum(this->row,this->col); if(this->row != c2.row || this->col != c2.col) { cout << "Cannot add because Order of matrices is not same" << en dl; return sum; } for(int i=0;i<this->row;i++) { for(int j=0;j<this->col;j++) { sum.data[i][j] = this->data[i][j] + c2.data[i][j]; } }

return sum; } /*-------------------------------------------------------------*/ //----------Difference Function------------//Subtract c2 from *this (calling object) and return the result in a //new Matrix object. Matrix Matrix::minus (const Matrix& c2) const { Matrix subs(this->row,this->col); if(this->row != c2.row || this->col != c2.col) { cout << "Cannot Substract because Order of matrices is not same" << endl; return subs; } for(int i=0;i<this->row;i++) { for(int j=0;j<this->col;j++) { subs.data[i][j] = this->data[i][j] - c2.data[i][j]; } } return subs; } /*-------------------------------------------------------------*/ //----------Multiplication Function------------//Multiply c2 from *this (calling object) and return the result in a //new Matrix object. Matrix Matrix::multiply (const Matrix& c2) const { Matrix mul(this->row,c2.col); if(this->col != c2.row) { cout << "Multiplication is not possible because columns of first Matrix is not equal to rows of second Matrix"; return mul; } mul.output(); for(int i=0;i<this->row;i++) // 'i' is for no of rows of pres ents and resultant matrix { for(int j=0;j<c2.col;j++) // 'j' is for no of colu mns of second and resultant matrix { for(int k=0;k<this->col;k++) // 'k' is for no of colu mns of second matrix { mul.data[i][j] += this->data[i][k] * c2.data[k][ j]; } }

} return mul; } /*-------------------------------------------------------------*/ //----------Transpose Function------------//Take the transpose of the matrix. void Matrix::Transpose () { Matrix temp(this->col,this->row); //obj to store data for(int i=0;i<this->row;i++) temp { for(int j=0;j<this->col;j++) { temp.data[j][i] = this->data[i][j]; } } for(int i=0;i<row;i++) delete [] data[i]; delete data; this->row = temp.row; this->col = temp.col; data = new int* [this->row]; //re-Allocating memory for matrix data w ith rows and columns interchanged for(int i=0;i<this->row;i++) data[i] = new int[this->col]; for(int i=0;i<this->row;i++) for(int j=0;j<this->col;j++) this->data[i][j] = temp.data[i][j]; } /*-------------------------------------------------------------*/ //----------Symetric Check Function------------//Return true if the matrix is a Symmetric matrix; false otherwise //A matrix is a symmetric matrix if At = A bool Matrix::isSymmetric () { if(this->isRectangular()) return false; for(int i=0;i<row;i++) for(int j=0;j<col;j++) if(data[i][j] != data[j][i]) return false; return true; } //deleting memory of current data array //Nested loop to copy data into

Das könnte Ihnen auch gefallen