Hello everyone!

I am writing a function to multiply two matricies together. I have written a simple algorithm for a matrix 3x3 -

```
void multiply()
{
int a[3][3], b[3][3], c[3][3], i, j , p, val = 0;
a[0][0] = -1;
a[0][1] = 0;
a[0][2] = 1;
a[1][0] = 5;
a[1][1] = 3;
a[1][2] = 0;
a[2][0] = 2;
a[2][1] = 4;
a[2][2] = 6;
b[0][0] = 3;
b[0][1] = 6;
b[0][2] = 0;
b[1][0] = 1;
b[1][1] = 3;
b[1][2] = 4;
b[2][0] = -1;
b[2][1] = 2;
b[2][2] = 5;
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
//val = 0;
for (p=0; p<3; p++)
{
val += (a[i][p]*b[p][i]);
cout << a[i][p] << " x " << b[p][j] << " + ";
}
cout << " = " << val << endl;
c[i][j] = val;
}
}
}
```

When run -

```
-1 x 3 + 0 x 1 + 1 x -1 + = -4
-1 x 6 + 0 x 3 + 1 x 2 + = -8
-1 x 0 + 0 x 4 + 1 x 5 + = -12
5 x 3 + 3 x 1 + 0 x -1 + = 27
5 x 6 + 3 x 3 + 0 x 2 + = 66
5 x 0 + 3 x 4 + 0 x 5 + = 105
2 x 3 + 4 x 1 + 6 x -1 + = 151
2 x 6 + 4 x 3 + 6 x 2 + = 197
2 x 0 + 4 x 4 + 6 x 5 + = 243
```

At least the 1st answer is correct! I don't know exactly what it is doing, but it seems to add the new value to the previous result of 'val'. Now, a good idea would be to make 'val' zero before the start of the 'p' loop, so I uncomment 'val = 0'. The result -

```
-1 x 3 + 0 x 1 + 1 x -1 + = -4
-1 x 6 + 0 x 3 + 1 x 2 + = -4
-1 x 0 + 0 x 4 + 1 x 5 + = -4
5 x 3 + 3 x 1 + 0 x -1 + = 39
5 x 6 + 3 x 3 + 0 x 2 + = 39
5 x 0 + 3 x 4 + 0 x 5 + = 39
2 x 3 + 4 x 1 + 6 x -1 + = 46
2 x 6 + 4 x 3 + 6 x 2 + = 46
2 x 0 + 4 x 4 + 6 x 5 + = 46
```

The right answers are -

-4

-4

5

18

39

12

4

36

46

Now it takes a random anwser out of the three and uses it for all 3 rows! Why does it ignore the 'val=0'? The numbers are different in each row, so why is the answer the same!!? It prints the elemets of the matricies correct, but for some reason its struggling to correctly add those together! Even If I set val to 0 or'NULL' in each loop, the result is the same. What is wrong? PLEASE HELP!