473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Classes Matrix question

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
18 2439
JosAH
11,448 Expert 8TB
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
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
JosAH
11,448 Expert 8TB
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
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
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
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
Well I have found my problem, however there is a problem with my multiplication.

J
Jun 20 '07 #8
JosAH
11,448 Expert 8TB
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
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
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
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
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
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
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
JosAH
11,448 Expert 8TB
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
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
JosAH
11,448 Expert 8TB
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
Thanks a TON.

Now on to TicTacToe.



Thanks again,
J
Jun 21 '07 #19

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

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
5
by: Srini nandiraju | last post by:
Hi folks, I am trying to implement a friend class and the following is what I did. Please scroll down. /***************** CODE **********************************/ class BKP { private: int...
5
by: Steven T. Hatton | last post by:
This is a question about coding styles. I've seen some cases where the programmer has redeclared the pure virtual functions from an interface class in the implementation derived from it. For...
4
by: Desperate | last post by:
I want to create class Matrix which contains array of class Rows and some additions Class Rows contains array of Cells Here is the declaration public class Cell public int value = 0...
10
by: Duncan M Gunn | last post by:
Hi, I need to store the following matrix of values: T U V A 2 1 1 B 2 - - C - - 2 Where (-) is an empty cell.
7
by: toton | last post by:
Hi, I have a STL vector of of characters and the character class has a Boost array of points. The things are vector<Characterchars; and class Character{ private: array<Point,Npoints; }; Now...
9
by: MathStuf | last post by:
I am working on a class that will be a matrix based on a vector of vectors. I am having trouble with the following code causing errors on compilation: template<class Tclass MatrixBase {...
1
by: almurph | last post by:
Hi everyone, Concerning the Needleman-Wunsch algorithm (cf. http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm) I have noticed a possible loop. Inside the algorithm there is an...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.