?
Solved

2D Dynamic Arrays

Posted on 2004-10-03
11
Medium Priority
?
235 Views
Last Modified: 2010-04-01
Hi,

How do I make a two-dimensional dynamic array? And is it possible to make one from a class?
0
Comment
Question by:SonicX_BlueBlur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12214035
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 12214211
EE's Axter has this implementation, which he posted a while back, which is nice:

http://axter.com/code/dynamic_2d_array.h
0
 

Author Comment

by:SonicX_BlueBlur
ID: 12214375
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12214419
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
 

Author Comment

by:SonicX_BlueBlur
ID: 12214494
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
 
LVL 30

Accepted Solution

by:
Axter earned 580 total points
ID: 12214683
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
 
LVL 30

Expert Comment

by:Axter
ID: 12214695
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
 
LVL 30

Expert Comment

by:Axter
ID: 12214711
>>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
 

Author Comment

by:SonicX_BlueBlur
ID: 12216257
...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
 
LVL 17

Expert Comment

by:rstaveley
ID: 12218882
I'm glad you accepted that from Axter rather than me. It would have been embarrassing 8-)
0
 
LVL 30

Expert Comment

by:Axter
ID: 12219336
>>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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question