I have the following definition - typedef struct matrix_t
-
{
-
int rows, columns;
-
Complex** nextElement;// TODO: Add fields
-
}*Matrix;
and I have to implement - /**
-
* Performs matrix multiplication. Computes matrix1 * matrix2 and stores the
-
* result in a newly-created matrix.
-
* Usage example:
-
*
-
* int main()
-
* {
-
* Matrix matrix1 = ...;
-
* Matrix matrix2 = ...;
-
* Matrix result = NULL;
-
*
-
* matrixMultiply( matrix1, matrix2, &result );
-
* // result now points to a newly
-
* // created matrix that contains
-
* // the product of the matrices.
-
*
-
* ...
-
* matrixDestroy( result );
-
* ...
-
* }
-
*
-
* Generally, matrix1 * matrix2 does not equal to matrix2 * matrix1.
-
* A new matrix is created, and the caller is responsible for freeing it.
-
* @param matrix1 - First matrix to multiply; should NOT be modified.
-
* @param matrix2 - Second matrix to multiply; should NOT be modified.
-
* @param result - A newly-created matrix that contains the result of the
-
* multiplication will be created, and result will point to it. (You can look at
-
* it as if this is how this function returns the resulting matrix)
-
* @return One of the following, in decreasing priority:
-
* MATRIX_SUCCESS - Success.
-
* MATRIX_NULL_ARGUMENT - Either matrix (or both), or result, is NULL.
-
* MATRIX_BAD_DIMENSIONS - Matrices do not have compatible
-
* dimensions; that is, number of columns of \p matrix1 is different
-
* than the number of rows of \p matrix2.
-
*/
-
-
MatrixResult matrixMultiply( Matrix matrix1, Matrix matrix2, Matrix* result );
and that's my buggy code so far: -
MatrixResult matrixMultiply(Matrix matrix1, Matrix matrix2, Matrix* result) {
-
if (matrix1 == NULL || matrix2 == NULL) {
-
return MATRIX_NULL_ARGUMENT;
-
}
-
-
if ((matrix1->columns != matrix2->rows)) {
-
return MATRIX_BAD_DIMENSIONS;
-
}
-
result = malloc(sizeof(struct matrix_t));
-
if (result == NULL) {
-
return MATRIX_NULL_ARGUMENT;
-
}
-
-
result->columns = matrix1->columns;
-
result->rows = matrix1->rows;
-
-
result->nextElement = (Complex**) malloc(matrix1->rows * sizeof(Complex*));
-
if (result->nextElement == NULL) {
-
free(result);
-
return MATRIX_NULL_ARGUMENT;
-
}
-
-
for (int i = 0; i < matrix1->rows; ++i) {
-
int acc = 0;
-
for (int j = 0; j < matrix2->columns; j++) {
-
acc = acc + (complexMultiply(matrix1->nextElement[i][j],
-
matrix2->nextElement[j][i]));
-
while (j == matrix2->columns) {
-
result->nextElement[i][j] = acc;
-
}
-
}
-
}
-
-
return MATRIX_SUCCESS;
-
}
I can't seem to get it to compile.
Probably the whole memory allocation is amiss.
What should I do?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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;...
|
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...
|
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;
}
|
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...
|
by: vinay3744 |
last post by:
Hello
I have this code
string **a; //pointer to pointer to a string
int x=300;
int y=500;
|
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |