Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Multiplying 2 matrices using link lists

Multiplying 2 matrices using link lists
Thread Tools
Moderator
Join Date: Sep 2000
Location: Irvine, CA
Status: Offline
Reply With Quote
May 28, 2001, 05:37 PM
 
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
Reply With Quote
May 31, 2001, 09:04 AM
 
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
Reply With Quote
Jun 1, 2001, 02:27 AM
 
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&#40;<font color = green>int</font> m, <font color = green>int</font> n&#41;
{
<font color = green>int</font> i;
<font color = green>double</font> **a;

a = calloc&#40;m, <font color = green>sizeof</font>&#40;<font color = green>double</font> *&#41;&#41;;
<font color = green>for</font>&#40;i = <font color = blue>0</font>; i &lt; m; ++i&#41;
a[i] = calloc&#40;n, <font color = green>sizeof</font>&#40;<font color = green>double</font>&#41;&#41;;
<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
Reply With Quote
Jun 1, 2001, 05:10 AM
 
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
Reply With Quote
Jul 27, 2001, 03:30 AM
 
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 ]
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:04 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2