int main()
{
int n = 0;
int i = 0;
int j = 0;
int k = 0;
int **x;
int **y;
int **z;
printf("Enter size of matrix - (N x N):");
scanf("%d", &n);
x = malloc(n * sizeof(int *));
y = malloc(n * sizeof(int *));
z = malloc(n * sizeof(int *));
for(i = 0; i < n; i++)
{
x[i] = malloc(n * sizeof(int));
y[i] = malloc(n * sizeof(int));
z[i] = malloc(n * sizeof(int));
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
x[i][j] = (rand()% 10) + 1;
y[i][j] = (rand()% 10) + 1;
}
}
for(i = 0; i < n; i++)
{
printf("\n");
for(j = 0; j < n; j++)
{
printf("%2d ", x[i][j]);
printf("%2d ", y[i][j]);
}
}
for(i = 0; i < n; i++)
{
printf("\n\n");
for(j = 0; j < n; j++)
{
for(k = 0; k < n; k++)
{
z[i][j] += x[i][k] * y[k][j];
}
printf("%2d ", z[i][j]);
}
}
}
So I'm trying to do a matrix multiply where the 2 matrices are always 2x2 or 3x3 etc. I want to fill the 2 matrices with random values and do a multiply. My issue is that my third matrix which is storing the answer is producing wrong values. For example, my first 2 matricies (for 2x2) hold values [4, 7, 4, 6] and [8, 6, 7, 3] and my output is [76, 48, 70, 45] when really it should be [81, 45, 74, 42]. Dont know what is going wrong?