# Matrix multiplication

This is a discussion on Matrix multiplication within the C Programming forums, part of the General Programming Boards category; So I have my program up and running but it us outputting the wrong values. 2 and of the four ...

1. ## Matrix multiplication

So I have my program up and running but it us outputting the wrong values. 2 and of the four values are wrong. The first and last values are wrong for some reason while the second and third values are correct. Thanks everyone.

Code:
```#include <stdio.h>
#include "conio.h"
int main()
{ // 4x1
int  product, entries, current, input;
char buffer[1028];
int a,b,i,j;
int x[4] [4] = {{ 0,1,1,0}, {1,0,0,0}, {0,1,0,0}, {0,1,0,1}}; //i has 4 numbers j has 4 numbers
int matrix [4][1];

for (a=0; a <4; a++)
{
for(b=0; b< 1; b++)
{
printf("Please enter in four entries for a 4*1 matrix.");
scanf("%d", &matrix[a][b]);
}
}

for (i = 0; i < b; i++){

for ( j =0; j < a ; j++)
{
product =x[i][j] * matrix[i][j];
printf("The product of the two matricies is %d\n", product);
_getch();	}
}

getchar();
return 0;

}```

2. I don't think matrix multiplication is commutative. Given matrices A and B the product AB will have as many rows as A and as many columns as B. You multiplied x by matrix, so there is no right matrix.

3. Thanks for what? You didn't ask us anything.

You have a buffer overrun, and no that's not how you perform matrix multiplication. 'x' and 'matrix' aren't the same size, so what makes you think you can use the same values to index into both of them?
Go and look up how to perform matrix multiplication. You have gotten the algorithm wrong.

4. Your second set of loops is not performing matrix multiplication at all. It is simply printing out the product of elements with the same index in x and matrix.

There is also the problem that the loops reading matrix work over a and b, and then you use the last values of a and b to control number of iterations in the loops doing your "product". Such tricks increase chances of getting things wrong. Buffer overruns when reading matrix won't help much.

5. Ok, this is what I have now. I'm not sure where I am going wrong here Can someone please point me in the right direction?

Code:
```#include <stdio.h>
#include "conio.h"
int main()
{ // 4x1
int entries, current, input;
int product[4];
int a,b,row,col;
int x[4] [4] = {{ 0,1,1,0}, {1,0,0,0}, {0,1,0,0}, {0,1,0,1}}; //i has 4 numbers j has 4 numbers
int matrix [4];

for (a=0; a <4; a++)
{
for(b=0; b< 1; b++)
{
printf("Please enter in four entries for a 4*1 matrix.");
scanf("%d", &matrix[b]);
}
}

for (row = 0; row < 4; row++){
product[row] = 0;

for ( col =0; col < 4 ; col++)
{
product[row] =  product[row] + x[row][col]*matrix[col];

}
printf("Row %d of The product of the two matricies is %d\n", row, product[row]);
_getch();
}

getchar();
return 0;

}```

6. Code:
```		for(b=0; b< 1; b++)
{
printf("Please enter in four entries for a 4*1 matrix.");
scanf("%d", &matrix[b]);
}```
Look closely... how often will that loop execute? How many elements are in your matrix?

7. Thank you Thank you. The one part of the program I wasn't looking at. You guys are awesome! CommonTater you are in fact an ExtroidinaryTater!

8. Just wait till you get my bill....