# Multiplying matrices

• 05-21-2003
Star Lancer
Multiplying matrices
There's this ridiculous program I have to do as a school assignment, and I have but just one question: how can you declare a two-dimensional matrix with parameters that are variables? Everytime I try to compile, it tells me I want a constant expression. Unfortunately, the teacher offered little help on the matter, and his advice didn't make sense at all. "Put const in," he told me. Very descriptive. I tried putting in const in various places, but it does nothing but create more errors. Anyway, it seems as if all my problems revolve around that bit. If there's any other bugs or errors, I can work them out, but this problem I just can't seem to fix. Oh, and of course, the best thing of all is the fact that I can only use standard two-dimensional arrays, and no matrix class! Oh JOY! Sorry for the sarcasm. Anyway...

if NumType == 1, then the matrices use int
if NumType == 2, then the matrices use float
Code:

```// creates matrices based on user specifications switch(NumType)  {     case 1:       int Matrix1[UserRows1][UserCols1];       int Matrix2[UserRows2][UserCols2];       long AnswerMatrix[UserCols1][UserRows2];       break;     case 2:       float Matrix1[UserRows1][UserCols1];       float Matrix2[UserRows2][UserCols2];       float AnswerMatrix[UserCols1][UserRows2];       break;     default:       cout << "You did not choose a valid data type.\n"; }```
If anyone could help me with this, I'd greatly appreciate it.
• 05-21-2003
Star Lancer
Here's the source code, if anyone needs it?
• 05-21-2003
Magos
It's true you can't define an array with a variable size.
Use pointers and dynamic allocation:
Code:

```int Matrix* = NULL; //Allocate memory, return if the allocation failed Matrix = new int[NrOfRows * NrOfColumns]; if(Matrix == NULL) {   return; } //Set the element at Row Y and column X to 45 Matrix[Y * NrOfColumns + X] = 45; //Deallocate the data when done delete[] Matrix;```
• 05-22-2003
Star Lancer
Thanks so much for your help ^_^
• 05-22-2003
Star Lancer
Erm, I decided to just go with a 10 x 10 matrix thing. The problem is though, after the second call to the Get_values function (which is supposed to fill up the second matrix with values), I always get an error after I enter in the last value, and it said I reached the breakpoint (when I used a matrix class), and that my index was 1 over, yet I don't really see how because I had ... kept it in bounds. It never happened for the first matrix though, oddly enough. I don't know if I'll be able to finish this by tonight, but if I could get someone to help me debug this, that'd be really great. ^^' I'm horrible at debugging.

Code:

```template <class DataType> void Get_values(DataType *Matrix1, int NumRows1, int NumCols1) {   for(int RowNum1 = 0; RowNum1 <= (NumRows1 - 1); RowNum1++)   {     for(int ColNum1 = 0; ColNum1 <= (NumCols1 - 1); ColNum1++)         {           cout << "\t(" << (RowNum1 + 1) << ", " << (ColNum1 + 1) << "): ";           cin >> Matrix1[RowNum1][ColNum1];         }   } }// end Get_values```
• 05-22-2003
Star Lancer
HUZZAH! (bows) Wow, thanks. It's always the stupid things that I never catch >_>'... ^_^ I can't express my gratitude in words ;o;...
• 05-22-2003
Star Lancer
... Funny, it still doesn't work, I still get that error about memory not being read and stuff -_-'. Oh well, thank you anyway ^^
• 05-22-2003
Star Lancer
O_o' Something's strange. I changed it to [1][10] since [][10] didn't work, and then it worked. Then I changed it back to just a pointer to see what'd happen, and it still works. ... -_- I don't know why.