435,501 Members | 2,768 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

Classes Matrix question

 P: 87 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 "Constructor", 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: Expand|Select|Wrap|Line Numbers #include  #include  using namespace std; int innerprod(int s[],int t[],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;     int size; }; Matrix::Matrix() {     cout<<"Constructor"<>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,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<>choice;     choice>>tolower(choice);     cout<<"   Enter a square matrix from 2x2 to 10x10 in row order:"<
18 Replies

 Expert 10K+ P: 11,448 Your loops all have an off by one error. When the maximum dimension of an array equals 10, valid index values are 0, 1, 2, 3, 4, 5, 6 , 7, 8, and 9 (count them: there are 10 different index values). Therefore your loops should all have a terminating condition such as: Expand|Select|Wrap|Line Numbers for (int i= 0; i < 10; i++) ...   kind regards, Jos Jun 19 '07 #2

 P: 87 Back to my rescue again hun JosAH? I understand what you're saying, and that is right...I wasn't thinking when I made them less than or equal to 10. The program still crashes, so the functions aren't going out of scope (or whatever it's called). J Jun 19 '07 #3

 Expert 10K+ P: 11,448 Back to my rescue again hun JosAH? I understand what you're saying, and that is right...I wasn't thinking when I made them less than or equal to 10. The program still crashes, so the functions aren't going out of scope (or whatever it's called). J I took a bit closer look at your code: have a look at the inner loop in your ctor: it increments 'i' instead of 'j' and it will shoot you in your foot for it ;-) kind regards, Jos Jun 19 '07 #4

 P: 87 Awesome. I never used 'i' and 'j' before because they look so much alike! But seems that my instructor uses them so I've started. I also removed the destructor function...which stopped the program from crashing. Do you know why that would be? J Jun 19 '07 #5

 P: 87 I know need to set the size of my matrix to the input from the user from the default size of the matrix (the constructor). Do I need to create an entirely differnent function for this? something like: void SetMatrix() ? Jun 19 '07 #6

 P: 87 I know need to set the size of my matrix to the input from the user from the default size of the matrix (the constructor). Do I need to create an entirely differnent function for this? something like: void SetMatrix() ? Is passing the size of the array to the functions just like passing any other info to a function? Thanks, J Jun 20 '07 #7

 P: 87 Well I have found my problem, however there is a problem with my multiplication. J Jun 20 '07 #8

 Expert 10K+ P: 11,448 Well I have found my problem, however there is a problem with my multiplication. J What's the problem then? And please don't say "it produces the wrong results" ;-) kind regards, Jos Jun 20 '07 #9

 P: 87 Hah. Well it does give the wrong answer. Anyway, I think the problem is in the innerprod function. When I do addition the program works great. When I do the multiplication it returns a HUGE number for a matrix with all 1's as the input. I pass all types of information to the innnerprod function including both matrices and size. The size is correct, however the numbers within the matrix are incorrect. I'm going to check my code and make sure the one posted in the thread is up to date. Thanks, J Jun 20 '07 #10

 P: 87 Current code: Expand|Select|Wrap|Line Numbers   #include  #include  using namespace std; int innerprod(int s[],int t[],int r,int c,int size) {     int temp=0,i;     for (i=0;i>a[i][j];         } } void Matrix::addm(Matrix B,int r, int t) {     int i=0,j=0;     for (i=0;i>r>>t;     }     while (r>10||t>10||r<2||t<2||r!=t);     Matrix(r,t);     cout<>choice;     choice>>tolower(choice);     cout<<"   Enter a square matrix from 2x2 to 10x10 in row order:"<

 P: 87 Hey everyone. I'm still having a problem when multiplying the 2 matrices. When I multiply them I receive an outrageously large "garbage" number. I have a feeling the problem is in my innerprod function, but I'm just not seeing it. The current code is posted above. Thanks, J Jun 21 '07 #12

 P: 18 Depending on the input, having int a underlying datatype can easily overflow. May be you can try with double. Double don't overflow easily but may lose precision over time. But int is almost sure to overflow, specialy for a 10 by 10 matrix. Expand|Select|Wrap|Line Numbers class Matrix { public:     ~Matrix(){};     Matrix();     void readm(int r, int t);     void addm(Matrix,int r,int t);     void multm(Matrix,int r, int t);     void print(int r,int t);     Matrix(int r,int t); private:     double a;     int size; }; Also don't forget than floating point number if not exact regarding the binary representation. This is an issues mostly for the == operator when double(0.3333333) != double(1.0/3.0); In that case you have to use something like this to test equality: epsilon is an arbitrary value. For my part I always took the sqrt root of the defaut epsilon of a floating point type. Expand|Select|Wrap|Line Numbers float const FloatEspsilon = 0.00034526698; // == sqrtf( std::numeric_limits::epsilon() );   inline bool FloatEqual( float _a, float _b ) {   float const a = _a;   float const b = _b;     return fabsf( a - b ) < FloatEspsilon; } I tell you that before you ask. And also maybe you already know that. But if you are going to work with double without knowing that, you may have some trouble that are hard to find. (it happens to me once working with a my own matrix class) Jun 21 '07 #13

 P: 87 Thanks for the reply. I'm not sure I understand the whole overflow thing. I made the change you suggested and I still receive an incorrect answer. I assume by overflow you mean out of scope? I wrote a matrix multiplication problem not to long ago and didn't have half the problems I'm having with this program...seems like classes just complicate things, but I'm sure they'll help out down the road. Thanks again, J Jun 21 '07 #14

 P: 18 By overflow I mean maybe your intermediate value is greater than the maximum value supported by a int (~2 billion). Did you convert intermediate variable of int by double too? innerproduct must return a double too. Expand|Select|Wrap|Line Numbers void Matrix::multm(Matrix B,int r, int t) {     int i=0,j=0,n=r;       double temp;       for(i=0;i

 Expert 10K+ P: 11,448 The parameters of your inner product function call are incorrect: you should pass i and j instead of r and t. kind regards, Jos Jun 21 '07 #16

 P: 87 Because by passing r & t I'm sending the size of the matrix to the innerprod function as opposed to sending the actual input? This is just for my understanding? Thanks, J Jun 21 '07 #17

 Expert 10K+ P: 11,448 Because by passing r & t I'm sending the size of the matrix to the innerprod function as opposed to sending the actual input? This is just for my understanding? Thanks, J You want the dot product of row i and column j so you should pass those to your innerproduct function, not the maximum allowed values of i and j. Since your matrices are supposed to be square you can remove some redundancy later. kind regards, Jos Jun 21 '07 #18

 P: 87 Thanks a TON. Now on to TicTacToe. Thanks again, J Jun 21 '07 #19 