The document describes a C++ program that implements a matrix class template. The matrix class template defines operations for matrices including addition, subtraction, multiplication, increment/decrement. It also defines input/output operators and functions for accessing matrix elements, getting dimensions and mean. The main function demonstrates using the class template for integer and float matrices, performing various operations on them and calculating their standard deviations.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
77 views24 pages
Mini Project (Hard Copy)
The document describes a C++ program that implements a matrix class template. The matrix class template defines operations for matrices including addition, subtraction, multiplication, increment/decrement. It also defines input/output operators and functions for accessing matrix elements, getting dimensions and mean. The main function demonstrates using the class template for integer and float matrices, performing various operations on them and calculating their standard deviations.
} bool Matrix::operator!=(Matrix & c) { if(this==&c) return false; if((i!=c.i)&&(j!=c.j)) { return true; } int count=0; for(int a=0;a<i;a++) { for(int b=0;b<j;b++) { P a g e | 6 if(this->arr[a][b]==c.arr[a][b]) count++; } } if(count==i*j) return false; return true; } int & Matrix::operator()(int m, int n) { return (arr[m][n]); } const int & Matrix::operator()(int m, int n)const { return (arr[m][n]); } void Matrix::seti(int c) { i=c; }
void Matrix::setj(int d) { j=d; } void Matrix::setelement(int **a,int c,int d) { for(int h=0;h<i;h++) delete [] arr[h]; delete [] arr; i=c; j=d; arr=new int * [i]; for(int k=0;k<i;k++) { arr[k]=new int [j]; for(int h=0;h<j;h++) arr[k][h]=a[k][h]; } } void Matrix::setindel(int val,int c,int d) { arr[c][d]=val; } int Matrix::geti() const { return i; } P a g e | 7 int Matrix::getj() const { return j; } int Matrix::getindel(int c,int d) const { return arr[c][d]; } double Matrix::getmean() const { double mean=0; for(int k=0;k<i;k++) { for(int h=0;h<j;h++) mean+=(double)arr[k][h]; } return mean/(i*j); } void Matrix::print() { for(int k=0;k<i;k++) {cout<<'\n'; for(int h=0;h<j;h++) cout<<arr[k][h]<<' '; } } Matrix::~Matrix() { for(int k=0;k<i;k++) delete [] arr[k]; delete [] arr; } istream & operator>>(istream & in, Matrix & A) { for(int a=0;a<A.i;a++) { for(int b=0;b<A.j;b++) { in>>A.arr[a][b]; } } return (in); } ostream & operator<<(ostream & out, Matrix & A) { for(int a=0;a<A.i;a++) { P a g e | 8 cout<<endl; for(int b=0;b<A.j;b++) { out<<A.arr[a][b]<<' '; } } return (out); } Main: #include "Matrix.h" double stdev (const Matrix & ); void main() { int i1,i2,j1,j2; double stda,stdb; cout<<"Enter the row and coloums of Matrix 1:\n"; cin>>i1; cin>>j1; Matrix A(i1,j1); cout<<"\nEnter the elements of Integer Matrix 1:\n"; cin>>A; cout<<"\nMatrix 1:"<<A<<'\n'; cout<<"\nEnter the row and coloums of Matrix 2:\n"; cin>>i2>>j2; Matrix B(i2,j2); cout<<"\nEnter the elements of Integer Matrix 2:\n"; cin>>B; cout<<"\nMatrix 2:"<<B<<'\n'; if(A==B) cout<<"\nEnter Matrices are same\n"; cout<<"\nMatrix 2["<<i2-1<<','<<j2-1<<"] = Matrix 1 ["<<i1-1<<','<<j1- 1<<"]\n"; B(i2-1,j2-1)=A(i1-1,j1-1); cout<<"\n\nNew Matrix 2: "<<B<<endl; cout<<"\nMatrix 2 pre-incremented:\n"; cout<<++B; cout<<endl; cout<<"\nMatrix 1 post-decremented"; cout<<A--; cout<<"\nNew Matrix 1:"<<A; cout<<endl; cout<<"\nMatrix 1 += Matrix 2:"; cout<<(A+=B)<<endl; cout<<"\nMatrix 1 * Matrix 2:"; cout<<A*B<<endl; P a g e | 9 stda=stdev(A); stdb=stdev(B); cout<<"\nStandard Deviation of Matrix 1: "<<stda; cout<<"\nStandard Deviation of Matrix 2: "<<stdb<<endl; cout<<"\nMatrix 1 = Matrix 2:\n"<<(A=B); if(!(A!=B)) cout<<"\n\nMatrices are same\n"; cout<<endl;