Title: 3by3 matrix multiplication
Tags: c++;class;matrix;multiplication
Question: I have a 3by3 matrix class that isn't working properly. When I multiply using a third instance to store the answer and multiply two others instances it works properly. But when I try to do *= it gives me weird numbers.

Here's the regular * and *= functions:

``` threeby3matrix operator*(threeby3matrix&amp; multiplier)
  {
    threeby3matrix m1;
    m1[0] = matrix[0] * multiplier[0] + matrix[1] * multiplier[3] + matrix[2] * multiplier[6];
    m1[1] = matrix[3] * multiplier[1] + matrix[4] * multiplier[4] + matrix[5] * multiplier[7];
    m1[2] = matrix[6] * multiplier[2] + matrix[7] * multiplier[5] + matrix[8] * multiplier[8];
    m1[3] = matrix[0] * multiplier[0] + matrix[1] * multiplier[3] + matrix[2] * multiplier[6];
    m1[4] = matrix[3] * multiplier[1] + matrix[4] * multiplier[4] + matrix[5] * multiplier[7];
    m1[5] = matrix[6] * multiplier[2] + matrix[7] * multiplier[5] + matrix[8] * multiplier[8];
    m1[6] = matrix[0] * multiplier[0] + matrix[1] * multiplier[3] + matrix[2] * multiplier[6];
    m1[7] = matrix[3] * multiplier[1] + matrix[4] * multiplier[4] + matrix[5] * multiplier[7];
    m1[8] = matrix[6] * multiplier[2] + matrix[7] * multiplier[5] + matrix[8] * multiplier[8];
    return m1;
  }
  threeby3matrix&amp; operator*=(threeby3matrix&amp; multiplier)
  {
    matrix[0] = matrix[0] * multiplier[0] + matrix[1] * multiplier[3] + matrix[2] * multiplier[6];
    matrix[1] = matrix[3] * multiplier[1] + matrix[4] * multiplier[4] + matrix[5] * multiplier[7];
    matrix[2] = matrix[6] * multiplier[2] + matrix[7] * multiplier[5] + matrix[8] * multiplier[8];
    matrix[3] = matrix[0] * multiplier[0] + matrix[1] * multiplier[3] + matrix[2] * multiplier[6];
    matrix[4] = matrix[3] * multiplier[1] + matrix[4] * multiplier[4] + matrix[5] * multiplier[7];
    matrix[5] = matrix[6] * multiplier[2] + matrix[7] * multiplier[5] + matrix[8] * multiplier[8];
    matrix[6] = matrix[0] * multiplier[0] + matrix[1] * multiplier[3] + matrix[2] * multiplier[6];
    matrix[7] = matrix[3] * multiplier[1] + matrix[4] * multiplier[4] + matrix[5] * multiplier[7];
    matrix[8] = matrix[6] * multiplier[2] + matrix[7] * multiplier[5] + matrix[8] * multiplier[8];
    return *this;
  }
```

For some reason I get [18][18][18][108][228][18][108][708][1638]

when they all should be 18. I tried messing around with brackets, but nothing seems to work.

Comment: @SamVarshavchik - I guess they want to get the typing up to scratch

Comment: Did you yet learn anything about "for" loops, yet?

Comment: also I forgot to mention, I used a single dimensional dynamic array to store the 2 dimensional array


Here is another answer: you are modifying matrix as you use it for calculation.

try something like this:

```threeby3matrix&amp; operator*=(threeby3matrix&amp; multiplier)
{
    st181a:6c1c:918f:f3a4:2e8c:61d6:1a08:6b16swap(*this, operator*(multiplier));
    return *this;
}
```

Comment for this answer: oh wow, seems like a stupid mistake now that I see it, thanks. And we learned loops, it just didn't seem very practical since it would take multiple loops and variables, so I just typed it out

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:04/02/16', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<c++><class><matrix><multiplication>', 'Title': '3by3 matrix multiplication', 'ViewCount': 45, 'id': 36367856, 'user_AboutMe': None, 'user_AccountId': 7163279.0, 'user_DisplayName': 'student', 'user_Reputation': 47.0, 'user_Views': 16.0, 'user_id': 5473307.0}}