-1

I'm working on a program to multiply two 3X3 matrices together. I have hit a few problems and I can't figure out the problems. Any help would be appreciated :D

#include <iostream>

using namespace std;

int main(){

int matrix1[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int matrix2[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int results[3][3];
int product = 0;
int i;
int j;

for (i = 1; i <= 3; i++){
    for (j = 1; j <= 3; j++){
        product += matrix1[i][j] * matrix2[j][i];
        cout << product << endl;
    }
    results[i][j] = product;
    product = 0;
}

cout << endl << "Output Matrix: " << endl;

for (int i = 1; i < 4; i++){
    for (int j = 1; j < 4; j++){
        cout << results[i][j];
    }
    cout << endl;
}


system("pause");
return 0;
}

And this is the result I get out of it:

25
73
-1717986851
48
129
-858993331
-1867771963
1566576709
1595991863

Output Matrix:
-858993460-858993460-858993460
-1717986851-858993460-858993460
-85899333112
Press any key to continue . . .

Thanks again in advance! :D

The Gibbinold
  • 259
  • 3
  • 12
  • Why are you using `delete[] matrix1;` aso, if you never created these using `new`? – πάντα ῥεῖ Dec 07 '16 at 12:56
  • `results[i][j] = product;` is supposed to be inside the inner loops body. – πάντα ῥεῖ Dec 07 '16 at 12:57
  • 3
    To begin with you seem to be forgetting that array indexes goes from zero to size minus one. – Some programmer dude Dec 07 '16 at 12:59
  • Try using the debugging tool in your favorite IDE with "watches" window open, it's really good in dealing with this sort of problems. If you're struck, return to stackoverflow and create a question which describes the problem, the desired behavior and what you tried to achieve it, as well as providing the MCVE. – alexeykuzmin0 Dec 07 '16 at 12:59
  • Your algorithm for multiplying matrices seems wrong! See [this](https://en.wikipedia.org/wiki/Matrix_multiplication#General_definition_of_the_matrix_product) – kiner_shah Dec 07 '16 at 13:00
  • @Someprogrammerdude Suggestion is right - thats the reason for you code to fail, because there is some random value - though I'm curious why this doesnt throw an exception anyway. – TripleEEE Dec 07 '16 at 13:08
  • attempting to `delete` automatically allocated memory is undefined behaviour in C++. And as has been pointed out `matrix1[i][j]` is accessed out of bounds which also invokes UB. – George Dec 07 '16 at 13:11
  • Please have a look at [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329). – IInspectable Dec 07 '16 at 13:24
  • 1
    @TripleEEE Because C++ have no bounds checking for arrays. Going out of bounds leads to *undefined behavior*, which *could* lead to crashes, but it's not caught by the compiler or the runtime environment. – Some programmer dude Dec 07 '16 at 13:39
  • @Someprogrammerdude ah, thanks for this "re-"update - been quite a while - well, this means in the address there will be some random bits which cause random numbers - right? :) – TripleEEE Dec 07 '16 at 13:48
  • Thanks, I shouldn't have been using `delete[]` I have been using pointers a lot recently so I just did it out of habit haha – The Gibbinold Dec 07 '16 at 16:22
  • I dislike the manual, i.e. the STL-unaware scalar product eval, but that's deft'ly not a reason to downvote this question; +1 from me for a well put question. – decltype_auto Dec 07 '16 at 19:13

3 Answers3

5

So to begin with you don't need the int i, j; lines at the beginning. If you didn't have them there the compiler would correctly told you that results[i][j] = product; is in the wrong place. Also arrays' first value isn't at A[1] but at A[0]. And for the matrix multiplication I suggest you to read this.
Therefore the solution should look like this:

int matrix1[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int matrix2[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int results[3][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}};

for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) {
        for (int u = 0; u < 3; u++)
            results[i][j] += matrix1[i][u] * matrix2[u][j];
    }

cout << endl << "Output Matrix: " << endl;

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        cout << results[i][j] << ".";
    }
    cout << endl;
}
mindriot
  • 5,413
  • 1
  • 25
  • 34
Maroš Beťko
  • 2,181
  • 2
  • 16
  • 42
0

Your code for matrix multiplication is wrong. Look at this literal implementation:

for (i = 0; i < 3; i++){
    for (j = 0; j < 3; j++){
         product = 0; 
         for (k = 0; k < 3; k++){
            product += matrix1[i][k] * matrix2[k][j];
         }
         matrix3[i][j] = product;

    }
}  
MBo
  • 77,366
  • 5
  • 53
  • 86
0

Matrix multiplication is implemented the following way (for 2 N x N matrices):

for i = 1..N
   for j = 1..N
     result[i][j] = 0.
     for k = 1..N
       result[i][j] += matrix1[i][k] * matrix2[j][k] // "row times column"
     end for
   end for
end for 

This will return you the product result = matrix1 * matrix2. In C++ you have to use e.g.

for (int i = 0; i < N; i++)

for a loop. You have to start with 0 and end with N-1 (that's why you use < and not <=). In your example set int N = 3.

You only use a delete for every new. So you do not need to delete arrays if you do not allocate the matrix dynamically.

Kapa11
  • 311
  • 2
  • 18