By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,106 Members | 2,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,106 IT Pros & Developers. It's quick & easy.

C function multiplying matrices using dynamic allocation

P: 1
I have the following definition
Expand|Select|Wrap|Line Numbers
  1. typedef struct matrix_t
  2. {
  3.     int rows, columns;
  4.     Complex** nextElement;// TODO: Add fields
  5. }*Matrix;
and I have to implement
Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * Performs matrix multiplication. Computes matrix1 * matrix2 and stores the
  3.  * result in a newly-created matrix.
  4.  * Usage example:
  5.  * 
  6.  * int main()
  7.  * {
  8.  *      Matrix matrix1 = ...;
  9.  *      Matrix matrix2 = ...;
  10.  *      Matrix result = NULL;
  11.  *
  12.  *      matrixMultiply( matrix1, matrix2, &result );
  13.  *      // result now points to a newly
  14.  *      // created matrix that contains
  15.  *      // the product of the matrices.
  16.  *
  17.  *      ...
  18.  *      matrixDestroy( result );
  19.  *      ...
  20.  * }
  21.  * 
  22.  * Generally, matrix1 * matrix2 does not equal to matrix2 * matrix1.
  23.  *  A new matrix is created, and the caller is responsible for freeing it.
  24.  * @param matrix1 - First matrix to multiply; should NOT be modified.
  25.  * @param matrix2 - Second matrix to multiply; should NOT be modified.
  26.  * @param result - A newly-created matrix that contains the result of the
  27.  * multiplication will be created, and result will point to it. (You can look at
  28.  * it as if this is how this function returns the resulting matrix)
  29.  * @return One of the following, in decreasing priority:
  30.  *      MATRIX_SUCCESS - Success.
  31.  *     MATRIX_NULL_ARGUMENT - Either matrix (or both), or result, is NULL.
  32.  *     MATRIX_BAD_DIMENSIONS - Matrices do not have compatible
  33.  *             dimensions; that is, number of columns of \p matrix1 is different
  34.  *             than the number of rows of \p matrix2.
  35.  */
  36.  
  37. MatrixResult matrixMultiply( Matrix matrix1, Matrix matrix2, Matrix* result );
and that's my buggy code so far:
Expand|Select|Wrap|Line Numbers
  1. MatrixResult matrixMultiply(Matrix matrix1, Matrix matrix2, Matrix* result) {
  2.     if (matrix1 == NULL || matrix2 == NULL) {
  3.         return MATRIX_NULL_ARGUMENT;
  4.     }
  5.  
  6.     if ((matrix1->columns != matrix2->rows)) {
  7.         return MATRIX_BAD_DIMENSIONS;
  8.     }
  9.     result = malloc(sizeof(struct matrix_t));
  10.     if (result == NULL) {
  11.         return MATRIX_NULL_ARGUMENT;
  12.     }
  13.  
  14.     result->columns = matrix1->columns;
  15.     result->rows = matrix1->rows;
  16.  
  17.     result->nextElement = (Complex**) malloc(matrix1->rows * sizeof(Complex*));
  18.     if (result->nextElement == NULL) {
  19.         free(result);
  20.         return MATRIX_NULL_ARGUMENT;
  21.     }
  22.  
  23.     for (int i = 0; i < matrix1->rows; ++i) {
  24.         int acc = 0;
  25.         for (int j = 0; j < matrix2->columns; j++) {
  26.             acc = acc + (complexMultiply(matrix1->nextElement[i][j],
  27.                     matrix2->nextElement[j][i]));
  28.             while (j == matrix2->columns) {
  29.                 result->nextElement[i][j] = acc;
  30.             }
  31.         }
  32.     }
  33.  
  34.     return MATRIX_SUCCESS;
  35. }
I can't seem to get it to compile.
Probably the whole memory allocation is amiss.

What should I do?
Mar 27 '11 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
Post a full and proper description of you problem which would include the actual compiler errors copied and pasted in full with line numbers correctly translated to the line numbers on the posted code or for running code expected behaviour and observed behaviour.

Do not just post you code and a "it doesn't work" message.
Mar 28 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.