1

I'm trying to write a code that gets a matrix A and its dimensions, a matrix B and its dimensions, and returns a matrix C such that C=AB.

It's safe to assume that the number of columns of A is equal to the number of rows of B and so C=AB is defined

This is my code:

int *matrix_multiplication(int *A,int row_A,int column_A,int *B,int row_B,int column_B)
{
    int row_C,column_C,*C,i,j,k,sum=0;
    row_C=row_A;
    column_C=column_B;
    C=(int*)malloc(row_C*column_C*sizeof(int));
    for(i=0;i<row_C;i++)
    {
        for(j=0;j<column_C;j++)
        {
            for(k=0;k<column_A;k++)
                sum+=(*(A+column_A*i+k))*(*(B+column_B*k+j));//A[i][k]B[k][j]
            *(C+row_C*i+j)=sum;
            sum=0;
        }
    }
    return C;
}

A little explanation: I view a matrix as a single dimensional array, of size columns*rows*sizeof(int) and the given formula A[i][j]=*(A+column_A*i+j) where A is pointer to the first element of the array, and column_A is the amount of columns in "matrix" A.

My problem is that my code does not work for some inputs when row_C != column_C

For example, if A=[28,8,12;14,5,45] and B=[31;27;11] it returns C=[1216;-842150451]

Why does this happen? I can't seem to find the bug.

Oria Gruber
  • 1,513
  • 2
  • 22
  • 44
  • 4
    Have you tried stepping through (in a debugger) a simple multiplication, while also doing it on paper in parallel? This has worked for me in the past... – roelofs Aug 13 '14 at 10:35
  • 1
    First of all, [in C you shouldn't cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). Secondly, I recommend you run in a debugger, and step through the code line by line. – Some programmer dude Aug 13 '14 at 10:35
  • I'm not familiar at all with the debugger and visual studio in general. I usually work things out with a piece of paper. But i added a line to print A[i][k] and B[k][j] (in the k for loop) to make sure im never out of bounds, and i wasnt. it always printed correct values. – Oria Gruber Aug 13 '14 at 10:39
  • Yup, agree with all comments so far. You should find the simplest possible matrix that this fails for (small, ideally something involving say an identity matrix), and then step through it line by line (or adding print statements). – Oliver Charlesworth Aug 13 '14 at 10:43
  • Your logic suggests that you should replace `column_B` with `row_B` in the most inner loop. – barak manos Aug 13 '14 at 10:43
  • column_A and row_B are the same, otherwise, matrix multiplication is undefined. – Oria Gruber Aug 13 '14 at 10:48
  • 3
    Should `*(C+row_C*i+j)` be `*(C+column_C*i+j)`? – Salix alba Aug 13 '14 at 10:56
  • oh my god...i would have never found that. thanks Salix, works as intended now. You have a very keen eye :) – Oria Gruber Aug 13 '14 at 10:57
  • Make it an answer, I will accept it, bonus rep for you Salix! – Oria Gruber Aug 13 '14 at 10:58

1 Answers1

1

Try

*(C+column_C*i+j)=sum;

it might be an idea to make a function or macro for accessing matrix elements. That way similar problems in the future can be avoided. Better than that make a matrix class with method.

Salix alba
  • 7,536
  • 2
  • 32
  • 38