can anybody give me the cocept of 2d n 3d arrays in c++ i will be very thankful.i am using this type of site 4 da first time but i m sure it will be good.

9 Years
Discussion Span
Last Post by WaltP


Even if you are familiar with a language a good reference is invaluable. C++ is so popular that if you google anything alongside "c++" you can usually get good hits also.

Hope this helps.


>can anybody give me the cocept of 2d n 3d arrays in c++
The simplest concept I can think of is a 2D array is like a table, and a 3D array is like a stack of tables.


Multidimensional arrays are concepts designed to make the job/life of the programmer easier as the compiler may well convert the multidimensional array into a standard, single dimensional array. Multidimensional arrays are useful when data is thought of as being in tabular form such as seen in a spread sheet or a game board like checkers/chess or a 3D game like Rubiks' cube. Multiple dimensions are also concepts used by physicists and mathematicians when discussing the basics building blocks of time/space, etc, though converting that type of dimensionality into a program isn't something us neophytes are likely to be doing. Another common use of a multidimenional array is an array of strings, which can be thought of as a 2D array of type char.


Every dimension you add to an array increases its space requirements exponentially. In anything above a two-dimensional array the need for all that space becomes rarer. In such cases, programmers use what are called "sparse arrays", which really aren't arrays at all, but are treated as if they were. In C++ and other object-oriented languages, this is the perfect use of a class type abstraction.

An array of strings is really a trick. So far (except for "sparse arrays" --which are also a trick) you have thought of an array as a sort of table or grid or matrix. This is called a static array, as it has a fixed domain and range. An unconstrained array is one where one or more dimensions of the array do not share the same range of indices --like the array of strings: one string may have a different length than another. A specific type of unconstrained array is a dynamic array, where the domain of one or more dimensions of the array may be changed at will.

Static arrays are simple, because all the computer needs to know is known at compile-time: how many dimensions and what is the size of each dimension respectively? The array can then be stored as a simple, contiguous list of elements which the computer indexes according to the information it has about that array.

Unconstrained arrays cannot be that simple, so to implement them usually involves pointers. Hence, an array of strings is something like an array of (pointer to char), or in C++, an array of std::string objects, which abstract away pointers to the actual char string. (In other words, pointers are involved either way.)

Enjoy. :)


And in simpler terms...

1D array: The list of TARGET stores in one city.
-- Store numbers 1,3,5,7,9 in Detroit

2D array: The list of Target stores in each state by city.
-- in Michigan:
---- 1D array for Detroit
---- 1D array for Pontiac
---- 1D array for Lansing
---- etc

3D Array: The list of Target stores in each country by state by city.
-- for USA
---- a 2D array for Michigan
---- a 2D array for New York
---- a 2D array for Oklahoma
---- etc.

4D Array: The list of Target stores on each or each planet by country by state by city.
---- 3D array for USA
---- 3D array for Australia
---- etc.

and so on.



n what abt the declaration of 2d n 3d array n how to call them


The syntax is slightly different for each situation. For example you can use static memory or dynamic memory to declare the array.

static memory to declare a 3D array of cubies to represent a Rubiks cube:

struct cubie
int face[6];
int num;

cubie cube[3][3][3];

dynamic memory to declare the same cube:

int x = 3;
int y = 3;
int z = 3
cubie *** cube;
cube = new cubie**[x];
for(int i = 0; i < 3; ++i)
  cube[i] = new cubie*[y];
  for(int j = 0; j < 3; ++j)
     cubie[i][j] = new cubie[z];

To access the cubie with coordinate x, y, z in cube, irrespective of type of memory used to allocate space for cube:

//conventions to interpret x, y, z---you may change this convention at your discretion
//x = row top to bottom->0-2
//y = column left to right->0-2
//z = layer front to back->0-2

//assign color value of 2 to face 1 of cubie in center row center column front layer
cube[1][1][0].face[1] = 2;

//pass cube to function to twist front layer to counterclockwise by 90 degrees:


//function declaration for above:

void frontCC90DegreeTwist(cubie *** cube);


void frontCC90DegreeTwist(cubie cube[][][3]);

and there are other possible variations for the declaration as well, depending on how many * vs [] you wish to use, though these are the two most often used.

Here's a 2d array of char to represent up to 10 words each of which may have up to 20 letters each

char words[10][21];

to assign a word to the second element of words:

char word[] = "hello";
strcpy(words[1], word);

to display the second word in words to the screen you would do this:

cout << words[1];


n what abt the declaration of 2d n 3d array n how to call them

Plz rd ths abt prpr pstng so ppl cn rd wat u wnt hlp wth...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.