Multiplying matrices

This is a discussion on Multiplying matrices within the C++ Programming forums, part of the General Programming Boards category; There's this ridiculous program I have to do as a school assignment, and I have but just one question: how ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    9

    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.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    9
    Here's the source code, if anyone needs it?
    Attached Files Attached Files

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    9
    Thanks so much for your help ^_^

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    9
    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
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    9
    HUZZAH! (bows) Wow, thanks. It's always the stupid things that I never catch >_>'... ^_^ I can't express my gratitude in words ;o;...

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    9
    ... Funny, it still doesn't work, I still get that error about memory not being read and stuff -_-'. Oh well, thank you anyway ^^

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    9
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overflow multiplying matrices
    By Logicbynumbers in forum C Programming
    Replies: 11
    Last Post: 11-17-2008, 05:53 PM
  2. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  3. multiplying matrices
    By RenderedAwake in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2005, 10:36 PM
  4. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-13-2004, 11:19 PM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 08:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21