473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

C function multiplying matrices using dynamic allocation

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
1 4281
Banfa
9,065 Expert Mod 8TB
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

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

Similar topics

5
by: kupiko | last post by:
Hello i'm working on problem of dynamic allocation of memory. I would like to have variable char name set in the way to be able to change the memory it uses while program is running for ex. I will...
13
by: jimjim | last post by:
Hello, I am coming from a C background and the below dynamic allocation of an array of pointers makes sense to me: #define SIZE 2 int **p; p = malloc ( SIZE * sizeof ( int * )); for(int j=0;...
1
by: john townsley | last post by:
OK so with c++ when using pointers for dynamic allocation at runtime, like a database type program. I am talking about a user adding records of an unkown amount at runtime, so pointers would be the...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
2
by: Prashanth | last post by:
Hi All, My slef Prashanth just learning c++, and VC++, i need all you help. We use New operator to dynamicly allocate the memory for a variable. Dynamic allocation is used to optimize the memory...
1
by: vinay3744 | last post by:
Hello I have this code string **a; //pointer to pointer to a string int x=300; int y=500;
16
by: Steven D'Aprano | last post by:
On Tue, 09 Sep 2008 14:59:19 -0700, castironpi wrote: You've created a solution to a problem which (probably) only affects a very small number of people, at least judging by your use-cases....
0
by: Kelly Bert Manning | last post by:
I didn't get any hits when I searched deja for DSN1COPY dynamic allocation so either everyone else had no trouble realizing that this happens or nobody else has ever come across it. I've used...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.