1. ## multiply 2 matrixes

this code should multiply 2 matrixes but it seems it doesn't work how it should.

output:
Code:
```Matrix A :
0  4  7
1  5  5

Matrix B :
0  5  3  6
0  7  1  2
0  3  2  1

Matrix C :
3  103  50  53
0  77  32  35```
the first number of C[1][1] should be 0!!!

C[1][1] = 0*0+4*0+7*0 = 0 not 3 !! can soneone help me?
C[1][2] = 0*5+4*7+5*3 = 43 not 103..
and so on..

it works sometimes, other times it does not

source:

Code:
```#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define RIGAA 2
#define COLA  3
#define RIGAB 3
#define COLB  4

int A [RIGAA][COLA];
int B [RIGAB][COLB];
int C [RIGAA][COLB];

void GenerateMatrixes();
void GenerateMatrix(int* matrix,int riga,int col);
void MultiplyaMatrix();
void PrintMatrix(char* name,int* matrix,int row,int col);
void CleanScreen();

int main(int argc, char* argv[])
{

int ch=0;  //scelta dal menu iterattivo

printf("************************************\n");
printf("**     Matrix Multiplication      **\n");
printf("************************************\n\n");

GenerateMatrixes();
printf("\n\n");

while(ch!=5) //loop cycle, repeat untill user choose to exit with value 5
ch=0;
printf("Regenerate matrixes    : 1 \n");
printf("Multiplicate           : 2 \n");
printf("Re-print matrixes      : 3 \n");
printf("Clean Screen           : 4 \n");
printf("Exit                   : 5 \n");
printf("Select command: ");
scanf("%d",&ch);

switch(ch)
{
case 1:
GenerateMatrixes();
break;
case 2:
{
MultiplyaMatrix();
break;
}
case 3:
{
PrintMatrix("A",(int*)A,RIGAA,COLA);
PrintMatrix("B",(int*)B,RIGAB,COLB);
break;
}
case 4:
{
//clear the screen
CleanScreen();
break;
}
default:
CleanScreen();
break;

};

}

return 0;
}

//random generation of matrix values
void GenerateMatrixes()
{

//generate matrixes A B
GenerateMatrix((int*)A,RIGAA,COLA);
GenerateMatrix((int*)B,RIGAB,COLB);

//automatically print of generated matrix
printf("\n\n\n");
PrintMatrix("A",(int*)A,RIGAA,COLA);
PrintMatrix("B",(int*)B,RIGAB,COLB);

printf("\n\n");

}

//Print output of the specified matrix
void PrintMatrix(char* name,int* matrix,int row,int col)
{
int i,j;
char str[20];
printf("\n\n");

sprintf(str,"Matrix %s : ",name);
printf(str);
printf("\n\n");

for(i=0;i<row;i++)//ciclo e genero casualmente i valori
{
for(j=0;j<col;j++)
{
sprintf(str,"%d  ",matrix[(i*col)+j]);
printf(str);
}
printf("\n");
}

printf("\n");

}

//Matrix multiplication
void MultiplyaMatrix()
{
int i; int j;
int m;
for (i=0; i<RIGAA; i++)
for (j=0; j<COLB; j++)
{
for(m=0;m<COLA;m++)
C[i][j]+= (A[i][m] * B[m][j]);
}

PrintMatrix("C",(int*)C,RIGAA,COLB);

}

//Randomly generate velues of a matrix given by pointer
void GenerateMatrix(int* matrix,int riga,int col)
{
int i,j=0;

for(i=0;i<riga;i++)//random generation of values
for(j=0;j<col;j++)
matrix[(i*col)+j]=rand()%8;
}

void CleanScreen()
{
system("clear"); // unix system
}```

2. In your MultiplyaMatrix() function you are not initialising C before summing the individual elements:
Code:
```void MultiplyaMatrix()
{
int i; int j;
int m;

for (i=0; i<RIGAA; i++) {
for (j=0; j<COLB; j++) {
C[i][j] = 0;   /* <-- initialise before using a loop to sum a number of elements */
for(m=0;m<COLA;m++) {
C[i][j]+= (A[i][m] * B[m][j]);
}
}
}

PrintMatrix("C",(int*)C,RIGAA,COLB);
}```

3. you are soooo right
thanx

Jacopo