 |
 |
Multiplying 2 matrices using link lists
|
 |
|
 |
|
Moderator 
Join Date: Sep 2000
Location: Irvine, CA
Status:
Offline
|
|
Hi,
Here is my problem. I have to read in two files using link lists. The files are 2 dimensional matrices. What I have to do is to multiply these two matrices together and return the result. The problem is that I am not quite sure how to read in the file. Here is my methodolgy:
1) read in the file somehow storing them into structures
2) create 2 dynamic arrays using the size of the link lists as the dimensions
3) scan in the linked lists into the arrays
4) multiply the 2 arrays together and return the result
Any ideas?
[This message has been edited by mindwaves (edited 05-28-2001).]
|
|
{{{ mindwaves }}}
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: Sep 2000
Location: Irvine, CA
Status:
Offline
|
|
Hmmm, now that I think about it, this program relates heavily to my other one in that I have to read in each number as a char testing whether it is a newline or not. Well, here is what I am trying to use in my structure:
edit: deleted most of what I said earlier.
New stuff: Ok, I have been working on this and getting some new ideas. I wrote a while loop which its only pupose was to determine the size of the matrices so now I know the number of rows and columns per matrix. I now want to dynamically create an array of that size so I can then store the data in. Here are my two structures:
<pre><font size = 1 face = courier><font color = green>struct</font> MatrixList
{
<font color = green>double</font> value;
<font color = green>struct</font> MatrixList *next;
};
<font color = green>struct</font> MatrixInfo
{
<font color = green>int</font> rows;
<font color = green>int</font> columns;
<font color = green>struct</font> MatrixList *head, *tail;
} mMatrix1, mMatrix2, mResult;
</font></pre>
MatrixList will be the structure to read in all of the values of the matrix and MatrixInfo will have the relevant information pertaining to the matrix (like the dimensions) and it will also have the resulttant matrix. What I need help on now is to dynamically create that array using calloc() (not too sure how to use it) and then to read in the linked list into the array. Any ideas?
[This message has been edited by mindwaves (edited 06-01-2001).]
|
|
{{{ mindwaves }}}
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: Sep 2000
Location: Irvine, CA
Status:
Offline
|
|
Ok, I wrote a function that is supposed to dynamically create an array of size m * n, but it does not seem to work. Any ideas?
<pre><font size = 1 face = courier>
<font color = green>double</font> **get_matrix_space(<font color = green>int</font> m, <font color = green>int</font> n)
{
<font color = green>int</font> i;
<font color = green>double</font> **a;
a = calloc(m, <font color = green>sizeof</font>(<font color = green>double</font> *));
<font color = green>for</font>(i = <font color = blue>0</font>; i < m; ++i)
a[i] = calloc(n, <font color = green>sizeof</font>(<font color = green>double</font>));
<font color = green>return</font> a;
}
</font></pre>
Here are the errors:
Error : illegal implicit conversion from 'void *' to
'double **'
test.cpp line 79 a = calloc(m, sizeof(double *));
Error : illegal implicit conversion from 'void *' to
'double *'
test.cpp line 81 a[i] = calloc(n, sizeof(double));
[This message has been edited by mindwaves (edited 06-01-2001).]
|
|
{{{ mindwaves }}}
|
| |
|
|
|
 |
|
 |
|
Moderator 
Join Date: Sep 2000
Location: Irvine, CA
Status:
Offline
|
|
edit: deleted message until later...doing new method
[This message has been edited by mindwaves (edited 06-01-2001).]
|
|
{{{ mindwaves }}}
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 1999
Location: Georgetown, Demerara, Guyana
Status:
Offline
|
|
Hi mindwaves,
As promised, I took a look at this thread (that was linked from your other "Advice on how to start this program" thread). Again, my apologies that I've been away from this Forum for the last couple of months... I guess that by now you've already resolved the issues with the memory-allocation routine. ;-)
Just a few general notes for anyone else who might have been following this thread:-
[1] The C memory-allocation routines ('calloc', 'malloc', etc.) return a "generic" pointer of type 'void *'. Depending on IDE/compiler settings, many C compilers (including MW CodeWarrior) could insist that we apply an ugly cast (for the desired type) to the pointer returned by these routines, e.g.:-
'a = (double **)( calloc( ... ) );'
[2] It's possible to allocate all the required memory for an array in one step, i.e., creating a single monolithic block to hold the 'm' x 'n' elements each of size 'double'. [Note that an array is typically stored as a contiguous block of memory anyway; it's the subscript notation that provides the illusion that the block is "segmented" into rows, columns and elements.] In this case, the returned pointer 'a' would now be declared as 'double *' (instead of 'double **'), the element-size would be 'double' (instead of 'double *'), and the allocation would look like:-
'a = (double *)( calloc( (m * n), sizeof(double) ) );'
Regards,
--Paul
[ 07-27-2001: Message edited by: Paul Crawford ]
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|