Thread: Matrix Multiplication - Weird things happening

  1. #1
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182

    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");
    	}
    }
    {RTFM, KISS}

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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. #3
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182
    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.
    {RTFM, KISS}

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182
    Yep.

    Thanks all.
    {RTFM, KISS}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-15-2006, 05:17 AM
  2. Weird things happening!!!!!!
    By korbitz in forum Windows Programming
    Replies: 4
    Last Post: 03-22-2004, 06:31 AM
  3. Matrix Reloaded Questions (SPOILERS_
    By Xei in forum A Brief History of Cprogramming.com
    Replies: 73
    Last Post: 10-19-2003, 02:21 PM
  4. Funny things happening to values outside of function
    By drb2k2 in forum Windows Programming
    Replies: 2
    Last Post: 04-10-2003, 02:39 PM
  5. Funny things happening outside of function
    By drb2k2 in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 02:26 PM