0

i start with:

 typedef struct
{
    int rows, cols;
    int **element;
}Matrix;

and i create two matrices:

void matrixInit (Matrix *m, int r, int c )
{
    m->element = malloc(sizeof(int*)*r);
    for (int j=0;j<3;++j)
    {
        m->element[j] = malloc(sizeof(int)*c);
    }
    m->rows=r;
    m->cols=c;
 }
Matrix m1,m2,m3;
matrixInit(&m1,3,3);
    for (k=0; k<m1.cols*m1.rows; k++)
        {
            m1.element[k]=q;
            q++;
        }

then i do matrix 2 using copy function created

void matrixCopy (Matrix *m, Matrix *n )
{
    int r=n->rows,c=n->cols,k;
    for (k=0; k<r*c; k++)
    {
        m->element[k]=n->element[k];
    }
}

matrixInit(&m2, 3, 3);
matrixCopy(&m2, &m1);

and then i create a third to be the result of the addition

matrixInit(&m3, 3, 3);

then i do the addition. this is where my problem lies. I cannot seem to get this to work.

my code for the function is below: (the prototype must stay the same)

Matrix* matrixAdd (Matrix *m, Matrix *n )
{
    int q;
    for (q=0; q<m->rows*m->cols; q++)
    {
        m->element[q]=*m->element[q]+*n->element[q];
    }
    return m;
}
m3=*matrixAdd(&m1, &m2);
4
  • Add the programming language, where you perform this implementation. Commented Dec 5, 2013 at 4:10
  • 1
    Is the addition function supposed to allocate a new matrix? Because your's currently modifies the first-parameter in-place, then returns its address. The result in your code usage is leaked memory from m3, and two-matrices (m3 and m1) that are pointing to the same buffer. In summary, that isn't good. Commented Dec 5, 2013 at 4:41
  • 1
    (a) The for (int j=0; j<3; ++j) loop should be for (int j=0; j<r; ++j) (r instead of 3); (b) the function creates but does not initialize the matrix; the values in the cells is indeterminate. For pity's sake, please use spaces: the loop for (q=0; q<m->rows*m->cols; q++) is damned hard to read, compared with: for (q = 0; q < m->rows * m->cols; q++). Commented Dec 5, 2013 at 4:56
  • Also, since m3 == &m1 after the call, returning the matrix as a pointer is a little pointless. Either you need to allocate a result matrix in the function or you don't need to return a pointer to the result. Since you can't change the prototype, you need to allocate the matrix in the addition function. Commented Dec 5, 2013 at 5:55

1 Answer 1

1

matrixAdd function should be as follows: * on element will refer to first value only.

Matrix* matrixAdd (Matrix *m, Matrix *n )
{
    int q;
    int r;
    for(r = 0; r<m->rows; ++r)
       for (q=0; q<m->cols; q++)
       {
           m->element[r][q]=m->element[r][q]+n->element[r][q];
       }

    return m;
}

This will be more readable can be understood.

In your case,

   *m->element[q] = *m->element[q]+*n->element[q];

will lead you into problem as you have allocated r pointers to int * (therefore r continuous element) and at each pointer you have allocated c integers.

Therefore, if q is beyond r, behaviour is not defined.

Sign up to request clarification or add additional context in comments.

4 Comments

*m->element[q] = *m->element[q]+*n->element[q]; gets me "Thread 1: EXC_BAD_ACCESS (code=1,address=0x1)"
sorry for inconvenience. I edited the answer by stating the reason.
Is q<m->rows*m->cols correct? Looks like it should be q<m->cols
This is one of the times when m->element[r][q] += n->element[r][q]; really helps; you don't have to look hard to see if two of the three expressions are the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.