# Thread: Matrix Multiplication - Weird things happening

1. ## Matrix Multiplication - Weird things happening

Well, of course, I'm trying to multiply two matrices together.

In short, my calculuations are correct (as you can see in the big text block describing the calculations) but they are not peiced together in the solution matrix for some WEIRD reason.

Here is my sample output for 1 run...
Code:
```Dimension of the matrices: 2
Constructing two 2x2 matrices...
3         3
3         3
*
0         0
0         3
=
[0][0] = (3 * 0)[0] + (3 * 0)[0]  = 0
[0][1] = (3 * 0)[0] + (3 * 3)[9]  = 9  <-- see this 9?
[1][0] = (3 * 0)[0] + (3 * 0)[0]  = 0
[1][1] = (3 * 0)[0] + (3 * 3)[9]  = 9
0         0 <-- it's supposed to be there too!
0         9
freeing memory for matrix1
freeing memory for matrix2```

Here is my code:
Code:
```#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>

void gen_matrix(const int , int **, int **);
void multiply(const int, int *, int *);
void print_matrix(int, int *);

int main(int argc, char *argv[])
{
int dim;
char user_input[16];
int *matrix1 = NULL, *matrix2 = NULL;

memset(user_input, '\0', sizeof(user_input));

printf("Dimension of the matrices: ");
fgets(user_input, 16, stdin);

dim = atoi(user_input);

printf("Constructing two %dx%d matrices...\n", dim, dim);

gen_matrix(dim, &matrix1, &matrix2);

print_matrix(dim, matrix1);
printf("*\n");
print_matrix(dim, matrix2);
printf("=\n");
multiply(dim, matrix1, matrix2);

if(matrix1 != NULL)
{
printf("freeing memory for matrix1\n");
free(matrix1);
}

if(matrix2 != NULL)
{
printf("freeing memory for matrix2\n");
free(matrix2);
}

}
void gen_matrix(const int dim, int **matrix1, int **matrix2)
{
int i, j;

*matrix1 = malloc((dim * dim) * sizeof(int));

if(*matrix1 == NULL)
{
fprintf(stderr, "matrix1 allocation failed.");
return;
}

*matrix2 = malloc((dim * dim) * sizeof(int));

if(*matrix2 == NULL)
{
fprintf(stderr, "matrix2 allocation failed.");
return;
}

srand((unsigned int)time(NULL));

for(i = 0; i < dim; i++)
{
for(j = 0; j < dim; j++)
{
*((*matrix1 + i) + j) = rand() % 4;
}
}

for(i = 0; i < dim; i++)
{
for(j = 0; j < dim; j++)
{
*((*matrix2 + i) + j) = rand() % 4;
}
}
}

void multiply(const int dim, int *m1, int *m2)
{
int i, j, k;

int *s = malloc((dim * dim) * sizeof(int));

for(i = 0; i < dim; i++)
{
for(j = 0; j < dim; j++)
{
int a, b;
*((s + i) + j) = 0;

printf("[%d][%d] = ", i, j);

for(k = 0; k < dim; k++)
{
printf("(%d * %d)[%d] ", (*((m1 + i) + k)), (*((m2 + k) + j)), (*((m1 + i) + k)) * (*((m2 + k) + j)));

if(k == 0)
a = (*((m1 + i) + k)) * (*((m2 + k) + j));
if(k == 1)
b = (*((m1 + i) + k)) * (*((m2 + k) + j));
if(k != dim - 1)
printf("+ ");

*((s + i) + j) += (*((m1 + i) + k)) * (*((m2 + k) + j)); <-- no workie?
}
printf(" = %d\n", a+b);
}
}

print_matrix(dim, s);

free(s);
}

void print_matrix(int dim, int *matrix)
{
int i, j;
for(i = 0; i < dim; i++)
{
for(j = 0; j < dim; j++)
{
printf("%10d", *((matrix + i) + j));
}
printf("\n");
}
}```

2. Maybe this code will give you a hint:
Code:
```	for(i = 0; i < dim; i++)
{
printf("%p ",((s + i) + j));
printf("\n");
}```
I could be wrong, but I think you want something more like this:
Code:
`*(s + j*dim + i)`
Or maybe this:
Code:
`*(s + i*dim + j)`
I'm not sure which one.

3. Hmm, if I run this code:
Code:
```	for(i = 0; i < dim; i++)
{
for(j = 0; j < dim; j++)
{
printf("%p\t", (s + i + j));
}
printf("\n");
}```
The [0, 1] and [1, 0] items have the same address.

Weird.

4. Yep.

Thanks all.