# C Matrix Multiplication

I’m attempting to solve a matrix multiplication problem with C. Lattice sizes given in issue (2x2) I composed this code yet it doesn’t print result as I anticipate. I believe I’m feeling the loss of a point about the rules of C.

What is my misstep in this code?

#include <stdio.h>
int main() {
int matA[2][2]={0,1,2,3};
int matB[2][2]={0,1,2,3};
int matC[2][2];
int i, j, k;
for (i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
for(k = 0; k < 2; k++) {
matC[i][j] += matA[i][k] * matB[k][j];
}
printf("%d\n",matC[i][j]);
}
}
}

Output:

2
3
4195350
11

You did not initialize matC

``````int matA[2][2]={{0,1},{2,3}};
int matB[2][2]={{0,1},{2,3}};
int matC[2][2]={{0,0},{0,0}};
``````

Result :

``````2
3
6
11
``````
3 Likes

Here is the code I use:

``````for(i=0;i<M;i++){
for(j=0;j<K;j++){
matC[i][j]=0;
for(k=0;k<N;k++){
matC[i][j]+=matA[i][k]*matB[k][j];
}
}
}

``````

Read this resource https://www.scaler.com/topics/matrix-multiplication-in-c/ to clear your doubt.

Here is the C program for matrix multiplication in C which is working in my IDE. We have to initialize the product matrix with 0 values also.

``````for(i = 0; i < rows1; i++){
for(j = 0; j < rows2; j++){
for(k = 0; k < cols2; k++){
product[i][j] += firstM[i][k] * secondM[k][j];
}
}
}
``````

Check this program C Program for Matrix Multiplication