2D Dynamic Arrays

Hi,

How do I make a two-dimensional dynamic array? And is it possible to make one from a class?
SonicX_BlueBlurAsked:
Who is Participating?
 
AxterConnect With a Mentor Commented:
http://axter.com/code/dynamic_2d_array.h

If you use my code, you can easily create a 2 Dimensional array using the following method:

dynamic_2d_array<int> MyArrOfInt(33, 44);

Where 33 is one dimension, and 44 is the second.

The above example created and interger array.

Following is a char array:

dynamic_2d_array<char> myarray(row, col);
0
 
Jaime OlivaresSoftware ArchitectCommented:
Hi SonicX_BlueBlur,

You can design your own class to manage a bidimensional dynamic class.
Have a look to this similar question I've answered for a 3-dimensional dynamic class:
http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_21021623.html

Good luck,
Jaime.
0
 
rstaveleyCommented:
EE's Axter has this implementation, which he posted a while back, which is nice:

http://axter.com/code/dynamic_2d_array.h
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
SonicX_BlueBlurAuthor Commented:
rstaveley, Axter's code is illegible to me. So far, Jaime's code looks the best, but I don't know how I could make it work with a class.
0
 
Jaime OlivaresSoftware ArchitectCommented:
Here is a 2d implementation with a 'double' data member, hope you can understand. I will continue tomorrow because I am going to sleep. Good luck, Jaime.

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

class Double2dArray
{
      double *data;   // example with 'double' datatype, could be anything
      int maxx, maxy;

      Double3dArray (int x, int y) {
             maxx = x;
             maxy = y;
             data = new double[maxx*maxy];
      }
      ~Double3dArray () {
           delete [] data;
      }
      inline double GetAt(int x, int y) {
           return data[x*maxy*y];
      }
      inline SetAt(int x, int y, int z) {
           data[x*maxy*y] = val;
      }
};

How to use:

Double2dArray arr(200,200);
arr.SetAt(100,30);    // Set element 100,30
double value = arr.GetAt(100,30);  // Get value of element 100,30

0
 
SonicX_BlueBlurAuthor Commented:
Yes, I know. I read that same code on that other thread, but how would I call member functions using that 2D array code to make a class array?
0
 
AxterCommented:
To access the data in the array, you just use the same syntax as if you were using a C-Style array.

MyArrOfInt[22][11] = 1234;

cout << MyArrOfInt[22][11] << endl;

The dynamic_2d_array class is very generic, simplistic, and efficient.

Since it uses the same interface as a C-Style array, it makes it easier to use and maintain.
0
 
AxterCommented:
>>but I don't know how I could make it work with a class.

To use Jaime's code, you would have to change his original, since it's not a template class.

If you use the dynamic_2d_array, you don't have to change the class.
You just declare it with your class name in the template ID.

dynamic_2d_array<MyClass> my_var(x, y);
0
 
SonicX_BlueBlurAuthor Commented:
...Geez, it's that easy? And I thought this was all way above my head :P
Thanks for clearing that up, Axter. 145 points to you.
0
 
rstaveleyCommented:
I'm glad you accepted that from Axter rather than me. It would have been embarrassing 8-)
0
 
AxterCommented:
>>I'm glad you accepted that from Axter rather than me. It would have been embarrassing 8-)

IMHO, I think the points should have been split.

I steal jkr's answers all the time. :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.