Link to home
Start Free TrialLog in
Avatar of tpe91
tpe91

asked on

Multidimensional Array

Hallo,
I need help. How Can I create a multidimensional array with CArray or CSortedArray?
Is it possible to create an nxm array where n,m are variables?

Thanl you

Peter
Avatar of mikeblas
mikeblas

Yes. Just create an array of your arrays.

Do you want a code sample? I don't usually write samples for only 50 points.

..B ekiM
I'll write you the sample code for free, but I will NOT do it while "mikeblas" has the question locked.
Avatar of tpe91

ASKER

I need code!
Since I already have tried what he mentioned I need a working example.
Anyway, thanks.
ASKER CERTIFIED SOLUTION
Avatar of Surfer
Surfer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The glaring problem with that example is its lack of support for serialization.

..B ekiM
Avatar of tpe91

ASKER

You can use the >> or << to serialise the arrays....
Not with the code provided in that article, no.  You need a SerializeElements() specialization, at least.

..B ekiM
Whenever I need to create multi-dimensional arrays, I always follow this path, deviating only with some variations.  Of course there is always the alternative way of just using an ordinary C/C++ array, but why duplicate a lot of the work when MFC has already created ways for making this a breeze (viz. a CArray of CArrays).

This example is for a 2-Dimensional array, even though you can extend the idea for more dimensions.

The code (below) creates a type for an array of integers (viz. CIntArray) and another for an array of "CIntArrays".  The array of arrays is called, "CIntArray2D".  Then it derives a class from CIntArray2D named, "CIntMatrix".  This derived class has a constructor that sets the sizes of each array to hold the specified number of rows and columns.


#include <afxtempl.h>
typedef  CArray<int, int> CIntArray;
typrdef  CArray<CIntArray, CIntArray> CIntArray2D;

class CIntMatrix  :  public CIntArray2D
{
public:
    CIntMatrix(int r, int c)
    {
       SetSize(r);
       for(int i=0; i < r; i++)
           ElementAt(i).SetSize(c);
    }
};


Notice, I was still able to use the functions that CArray has for performing some of the operations in setting up my arrays.  Personally, I prefer this way because it gives me a lot of flexibility with what I might want to do with my arrays (be them one-dimensional, or multi-dimensional).


There is another way I could have used, because collection classes created with CArray (also CList and CMap), use helper functions to perform many of the operation involving the objects in the collection.  Some of these helper functions are, ConstructElements(), DestructElements(), CopyElements(), SerializeElements(), etc., which perform similar functions to what you might do if you were to choose the other method I chose earlier.

Note that these helper functions are global functions that do NOT belong to any specific class.  This also means that you only need to implement them once for each data type you would be using.

For example, using the SerialElements() function, the collection templates will call this function to serialize the objects in the collection.  The default implementation simply does a bitwise copy, and (looking at the sample below) you see parameters for the first element, a count of elements, and a reference to CArchive used for serialization.  (NOTE:  If your object is derived from CObject, you can use the Serialize() function of your element class.)

void  SerialElements(CArchive& ar, CIntArray* pElement, int nCount)
{
   int  nIndex;
   for(nIndex = 0; nIndex < nCount; nIndex++, pElement++)
       pElement->Serialize(ar);
}

------------------------------------

For the ConstructElements() function, it would be:

void  ConstructElements(CIntArray* pElement, int nCount)
{
   int  nIndex;
   for(nIndex = 0; nIndex < nCount; nIndex++, pElement++)
       pElement->CIntArray::CIntArray();
}


Notice, because I defined another class (viz. CMatrix) and created my own constructor in that class, I didn't need to use the ConstructElements() helper function.

Two approaches I have oultined above.  They are separate, and follow separate implementations.  In the first approach, I created my arrays (by way of CArray), but I also derived a class from my multi-dimensional array, because I wanted the flexibility of (perhaps) implementing a sort routine.

The second approach uses the global helper functions which sort of cut directly to the chase, and is more cut and dry.  They do not use the member functions of the collection classes to initialize, add, remove, insert, etc., elements.


If you have any questions, don't hesitate.
Avatar of tpe91

ASKER

To Try:
I think that you should taked the points :)
To everyone:

Anyway, I think that I will see it soon, if I can serialise it, or I will use Try's code. BTW, I had answered a few question in HTML area. Do you know why I did not took any points? I need them for the C++ section :-)

Cheers,
Peter
Don't worry about the points, I didn't do it for the points.  Just happy to help.

In my example regarding SerializeElements(), I misspelt the name of the function.  Please note it is NOT "SerialElements"; the correct spelling is "SerializeElements".

If I wasn't too clear with the things I've stated, I'll be happy to explain further.

------------------------------------

NOTE:  Points that you earn when you answer someone's question, cannot be used (in return) to offer to someone else for answering your questions.
Avatar of tpe91

ASKER

Try: Thanks a lot,
Really, I did not said that for yourseff. Actually, I have a BIG project for my PhD and although a know C++ pretty well, I need help for the MFC. So, I am needing the points:)
Anyway, I think that the idea is to help each other. If not, then what's the point? I mean, we coud do that job in a Helpdesk, or not?

By the way, I will try your code in a few days (till Thursday).


"If I wasn't too clear with the things I've stated, I'll be happy to
explain further." eg by mail???:):):):):):):):):):):)
Cheers,
Peter
> Note that these helper functions are global
 > functions that do NOT belong to any specific class.  

Well, kind of. They're strongly associated with a particular class because they're friends to the class, and they're specializations based on the template arguments of the class.

But SerializeElements() is what was missing from that codeguru.com example. You have to do it, and do it right, if you want the code to work.

..B ekiM
Hairsplitting!

Nonetheless, I don't believe any of us can fully express the magnitude of our thanks to you for bringing that to our attention.  I'm sure our lives are much better off now, that you have done so.
> Hairsplitting!

Hardly. There's a vast difference between what works and what doesn't work. The collection helper functions aren't easy to get right, are crucial to a working implementation, and lots of people end up asking about 'em.

I don't know why that fact offends you so, but there it is.

..B ekiM
I don't believe anybody is more qualified than you to talk about what works and what doesn't.  You have certainly contributed more than your fair share to the pile.
It's hard for me to understand why you're so hostile and sarcastic.

..B ekiM