Hello everyone. I'm writing a program which uses a class called matrix. I have written all of the different functions, constructor, etc.
When I run the program I receive "Constructo r", which I placed in the constructor, and then the program crashes. I have no clue where my problem is.
The matrix is for size 2x2 up to 10x10, and it must be square.
Below is the code:
Thanks,
J
When I run the program I receive "Constructo r", which I placed in the constructor, and then the program crashes. I have no clue where my problem is.
The matrix is for size 2x2 up to 10x10, and it must be square.
Below is the code:
Code:
#include <iostream> #include <ctype.h> using namespace std; int innerprod(int s[][10],int t[][10],int r,int c,int size) { int temp=0,i; for (i=0;i<=size;++i) { temp=temp+s[r][i]*t[i][c]; } return temp; } class Matrix { public: ~Matrix(){}; Matrix(); void readm(); void addm(Matrix); void multm(Matrix); void print(); private: int a[10][10]; int size; }; Matrix::Matrix() { cout<<"Constructor"<<endl; int i=0, j=0; for (i=0;i<=10;++i) for (j=0;j<=10;++i) { a[i][j]=0; } } void Matrix::readm() { int i=0,j=0; for (i=0;i<=10;++i) for (j=0;j<=10;++j) { cin>>a[i][j]; } } void Matrix::addm(Matrix B) { int i=0,j=0; for (i=0;i<=10;++i) for (j=0;j<=10;++j) { a[i][j]=a[i][j]+B.a[i][j]; } } void Matrix::multm(Matrix B) { int i=0,j=0,temp[10][10],n=2; for(i=0;i<=10;++i) for (j=0;j<=10;++j) { temp[i][j]=innerprod(a,B.a,i,j,n); } for (i=0;i<=10;++i) for (j=0;j<=10;++j) { a[i][j]=temp[i][j]; } } void Matrix::print() { int i=0,j=0; for (i=0;i<=10;++i) { cout<<endl<<endl; for (j=0;j<=10;++j) { cout<<" "<<a[i][j]; } } cout<<endl; } void main() { char choice; Matrix x,y; cout<<" Welcome to Matrix Manipulations."<<endl; x.print(); cout<<" You may add or multiply a matrix."<<endl; cout<<" Enter choice: (a)dd or (m)multiply. "<<endl; cin>>choice; choice>>tolower(choice); cout<<" Enter a square matrix from 2x2 to 10x10 in row order:"<<endl; x.readm(); switch(choice) { case 'a':cout<<"Enter a square matrix from size 2x2 to 10x10 in row order:"<<endl; y.readm(); x.print(); cout<<endl<<" +"; y.print(); cout<<endl<<" ="; x.addm(y); x.print(); break; case 'm':cout<<"Enter a square matrix from size 2xe to 10x10 in row order:"<<endl; y.readm(); x.print(); x.multm(y); cout<<endl<<" *"; y.print(); cout<<endl<<" ="; x.print(); break; default: cout<<" INVALID CHOICE!"; break; }; }
J
Comment