By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,768 Online
Bytes IT Community
+ 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
  1. #include <iostream>
  2. #include <ctype.h>
  3. using namespace std;
  4. int innerprod(int s[][10],int t[][10],int r,int c,int size)
  5. {
  6.     int temp=0,i;
  7.     for (i=0;i<=size;++i)
  8.     {
  9.         temp=temp+s[r][i]*t[i][c];
  10.     }
  11.     return temp;
  12. }
  13. class Matrix
  14. {
  15. public:
  16.     ~Matrix(){};
  17.     Matrix();
  18.     void readm();
  19.     void addm(Matrix);
  20.     void multm(Matrix);
  21.     void print();
  22. private:
  23.     int a[10][10];
  24.     int size;
  25. };
  26. Matrix::Matrix()
  27. {
  28.     cout<<"Constructor"<<endl;
  29.     int i=0, j=0;
  30.     for (i=0;i<=10;++i)
  31.         for (j=0;j<=10;++i)
  32.         {
  33.             a[i][j]=0;
  34.         }
  35. }
  36. void Matrix::readm()
  37. {
  38.     int i=0,j=0;
  39.     for (i=0;i<=10;++i)
  40.         for (j=0;j<=10;++j)
  41.         {
  42.             cin>>a[i][j];
  43.         }
  44. }
  45. void Matrix::addm(Matrix B)
  46. {
  47.     int i=0,j=0;
  48.     for (i=0;i<=10;++i)
  49.         for (j=0;j<=10;++j)
  50.         {
  51.             a[i][j]=a[i][j]+B.a[i][j];
  52.         }
  53. }
  54. void Matrix::multm(Matrix B)
  55. {
  56.     int i=0,j=0,temp[10][10],n=2;
  57.     for(i=0;i<=10;++i)
  58.         for (j=0;j<=10;++j)
  59.         {
  60.             temp[i][j]=innerprod(a,B.a,i,j,n);
  61.         }
  62.     for (i=0;i<=10;++i)
  63.         for (j=0;j<=10;++j)
  64.         {
  65.             a[i][j]=temp[i][j];
  66.         }
  67. }
  68. void Matrix::print()
  69. {
  70.     int i=0,j=0;
  71.     for (i=0;i<=10;++i)
  72.     {
  73.         cout<<endl<<endl;
  74.         for (j=0;j<=10;++j)
  75.         {
  76.             cout<<"    "<<a[i][j];
  77.         }
  78.     }
  79.     cout<<endl;
  80. }
  81. void main()
  82. {
  83.     char choice;
  84.     Matrix x,y;
  85.     cout<<"   Welcome to Matrix Manipulations."<<endl;
  86.     x.print();
  87.     cout<<"   You may add or multiply a matrix."<<endl;
  88.     cout<<"   Enter choice: (a)dd or (m)multiply.  "<<endl;
  89.     cin>>choice;
  90.     choice>>tolower(choice);
  91.     cout<<"   Enter a square matrix from 2x2 to 10x10 in row order:"<<endl;
  92.     x.readm();
  93.     switch(choice)
  94.     {
  95.     case 'a':cout<<"Enter a square matrix from size 2x2 to 10x10 in row order:"<<endl;
  96.         y.readm();
  97.         x.print();
  98.         cout<<endl<<"    +";
  99.         y.print();
  100.         cout<<endl<<"    =";
  101.         x.addm(y);
  102.         x.print();
  103.         break;
  104.     case 'm':cout<<"Enter a square matrix from size 2xe to 10x10 in row order:"<<endl;
  105.         y.readm();
  106.         x.print();
  107.         x.multm(y);
  108.         cout<<endl<<"    *";
  109.         y.print();
  110.         cout<<endl<<"    =";
  111.         x.print();
  112.         break;
  113.     default: cout<<"    INVALID CHOICE!";
  114.         break;
  115.     };
  116. }
  117.  
  118.  
Thanks,

J
Jun 19 '07 #1
Share this Question
Share on Google+
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
  1. for (int i= 0; i < 10; i++) ...
  2.  
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
  1.  
  2. #include <iostream>
  3. #include <ctype.h>
  4. using namespace std;
  5. int innerprod(int s[][10],int t[][10],int r,int c,int size)
  6. {
  7.     int temp=0,i;
  8.     for (i=0;i<size;++i)
  9.     {
  10.         temp=temp+s[r][i]*t[i][c];
  11.     }
  12.     return temp;
  13. }
  14. class Matrix
  15. {
  16. public:
  17.     ~Matrix(){};
  18.     Matrix();
  19.     void readm(int r, int t);
  20.     void addm(Matrix,int r,int t);
  21.     void multm(Matrix,int r, int t);
  22.     void print(int r,int t);
  23.     Matrix(int r,int t);
  24. private:
  25.     int a[10][10];
  26.     int size;
  27. };
  28. Matrix::Matrix()
  29. {
  30.     cout<<"In the Constructor function....."<<endl<<endl;
  31.     int i=0, j=0;
  32.     for (i=0;i<2;++i)
  33.         for (j=0;j<2;++j)
  34.         {
  35.             a[i][j]=0;
  36.         }
  37. }
  38. Matrix::Matrix(int r,int t)
  39. {
  40.     int i=0,j=0;
  41.     cout<<"   Initializing new Matrix....."<<endl;
  42.     for (i=0;i<r;++i)
  43.         for (j=0;j<t;++j)
  44.         {
  45.             a[r][t]=0;
  46.         }
  47.     for (i=0;i<r;++i)
  48.     {
  49.         cout<<endl<<endl;
  50.         for (j=0;j<t;++j)
  51.             cout<<"   "<<a[r][t];
  52.     }
  53.     cout<<endl;
  54.  
  55. }
  56. void Matrix::readm(int r, int t)
  57. {
  58.     int i=0,j=0;
  59.     for (i=0;i<r;++i)
  60.         for (j=0;j<t;++j)
  61.         {
  62.             cin>>a[i][j];
  63.         }
  64. }
  65. void Matrix::addm(Matrix B,int r, int t)
  66. {
  67.     int i=0,j=0;
  68.     for (i=0;i<r;++i)
  69.         for (j=0;j<t;++j)
  70.         {
  71.             a[i][j]=a[i][j]+B.a[i][j];
  72.         }
  73. }
  74. void Matrix::multm(Matrix B,int r, int t)
  75. {
  76.     int i=0,j=0,temp[10][10],n=r;
  77.     for(i=0;i<r;++i)
  78.         for (j=0;j<t;++j)
  79.         {
  80.             temp[i][j]=innerprod(a,B.a,r,t,n);
  81.         }
  82.     for (i=0;i<r;++i)
  83.         for (j=0;j<t;++j)
  84.         {
  85.             a[i][j]=temp[i][j];
  86.         }
  87. }
  88. void Matrix::print(int r,int t)
  89. {
  90.     int i=0,j=0;
  91.     for (i=0;i<r;++i)
  92.     {
  93.         cout<<endl<<endl;
  94.         for (j=0;j<t;++j)
  95.         {
  96.             cout<<"    "<<a[i][j];
  97.         }
  98.     }
  99.     cout<<endl;
  100. }
  101. void main()
  102. {
  103.     int r=0, t=0;
  104.     char choice,s;
  105.     Matrix x,y;
  106.     s=6;
  107.     cout<<"   Welcome to Matrix Manipulations for Square Matrices."<<endl;
  108.     x.print(r,t);
  109.     do
  110.     {
  111.         cout<<"   What size matrix do you have??  Enter a matrix size 2x2 to 10x10."<<endl;
  112.         cin>>r>>t;
  113.     }
  114.     while (r>10||t>10||r<2||t<2||r!=t);
  115.     Matrix(r,t);
  116.     cout<<endl<<"   You may add or multiply a matrix."<<endl;
  117.     cout<<"   Enter choice: (a)dd or (m)multiply.  "<<endl;
  118.     cin>>choice;
  119.     choice>>tolower(choice);
  120.     cout<<"   Enter a square matrix from 2x2 to 10x10 in row order:"<<endl;
  121.     x.readm(r,t);
  122.     switch(choice)
  123.     {
  124.     case 'a':cout<<"Enter a square matrix from size 2x2 to 10x10 in row order:"<<endl;
  125.         y.readm(r,t);
  126.         x.print(r,t);
  127.         cout<<endl<<"    +";
  128.         y.print(r,t);
  129.         cout<<endl<<"    =";
  130.         x.addm(y,r,t);
  131.         x.print(r,t);
  132.         break;
  133.     case 'm':cout<<"Enter a square matrix from size 2x2 to 10x10 in row order:"<<endl;
  134.         y.readm(r,t);
  135.         x.print(r,t);
  136.         x.multm(y,r,t);
  137.         cout<<endl<<"    *";
  138.         y.print(r,t);
  139.         cout<<endl<<"    =";
  140.         x.print(r,t);
  141.         break;
  142.     default :cout<<"    INVALID CHOICE!";
  143.         break;
  144.     };
  145. }
  146.  
  147.  
j
Jun 20 '07 #11

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
  1. class Matrix
  2. {
  3. public:
  4.     ~Matrix(){};
  5.     Matrix();
  6.     void readm(int r, int t);
  7.     void addm(Matrix,int r,int t);
  8.     void multm(Matrix,int r, int t);
  9.     void print(int r,int t);
  10.     Matrix(int r,int t);
  11. private:
  12.     double a[10][10];
  13.     int size;
  14. };
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
  1. float const FloatEspsilon = 0.00034526698; // == sqrtf( std::numeric_limits<float>::epsilon() );
  2.  
  3. inline bool FloatEqual( float _a, float _b )
  4. {
  5.   float const a = _a;
  6.   float const b = _b;
  7.  
  8.   return fabsf( a - b ) < FloatEspsilon;
  9. }
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
  1. void Matrix::multm(Matrix B,int r, int t)
  2. {
  3.     int i=0,j=0,n=r;
  4.  
  5.     double temp[10][10];
  6.  
  7.     for(i=0;i<r;++i)
  8.         for (j=0;j<t;++j)
  9.         {
  10.             temp[i][j]=innerprod(a,B.a,r,t,n);
  11.         }
  12.     for (i=0;i<r;++i)
  13.         for (j=0;j<t;++j)
  14.         {
  15.             a[i][j]=temp[i][j];
  16.         }
  17. }
Jun 21 '07 #15

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

Post your reply

Sign in to post your reply or Sign up for a free account.