wpe41.gif (23084 bytes)CIS3355: Business Data Structures
Fall, 2008
 

Multidimensional Arrays and how they work!

 

●An array by definition is a regular order of arrangement.  Arrays are one-dimensional and can be though of similar to this arrangement:

 

[][][][][][]

 

●Any array that surpasses the one-dimensional boundary becomes a Multidimensional Array and can be pictures as:

 

[][][][][]

[][][][][]

[][][][][]

[][][][][]

[][][][][]

 

●Despite the fact that Multidimensional Arrays appear to be extremely confusing and beyond the concepts of one-dimensional arrays, they are in fact very similar.  One important aspect to bring from one-dimensional arrays is that the first index number is zero and the last is the number of elements in the array minus one.  The two are also stored in the Random Access Memory similarly, and initializing them is closely related.  Remember initializing an array when declaring a statement such as:

 

int numvector[10] = {0, 1, 3, 5, 7, 9, 11, 13, 15, 17};

 

Well initializing a multidimensional array isn’t much different for example:

 

Int numtable[4][2] = {(2, 4), (6, 8), (10, 12), (14, 16)};

 

Similarly to uni-dimensional arrays multidimensional arrays also rely upon contiguous storage.  In the example above 16 bytes of contiguous storage is reserved in the RAM.  Sixteen bytes due to the fact that the above example will contain 4 rows with 2 columns that have 2 byte integers in each so, 4*2*2 = 16 bytes.  Upon initializing Ram provides 16 bytes of contiguous storage at address we’ll say 2000.  Since each value is two bytes, two storages will be used by each value such as location 2000, and 2001 for the value 2 and 2002, and 2003 for 4 and so on.  This leads us to the multidimensional array offsets.  Are you still with me?

      

 

If you are seeing stars just always remember to follow the path of light and righteousness to deter this material from turning you to the dark side. 

 

●Array offsets such as [0][0] are also references to the base address of the array which one in our example can be (2000).  There is a formula for calculating each address:

 

Element address = base address of the array

+(Row offset*bytes per row)

             +(Column offset*bytes per element)

 

●The array offsets increase like binary code except when a one is carried to the left it increases the left most number by one and keeps repeating.  The pattern would be similar to this example:

[0][0]             

[0][1]

[1][0]

[1][1]

[2][0]

[2][1]

[3][0]

[3][1]

                       [4][0]   And so on….

 

●Lets interpret the element address formula by applying it to our previous example:

Int numtable[4][2] = {(2, 4), (6, 8), (10, 12), (14, 16)};

 

Array Offsets

       Calculated Addresses

Row/column

 

      [0][0]

2000 + (0 * 4 + 0 * 2) = 2000 + 0 + 0 = 2000

      [0][1]

2000 + (0 * 4 + 1 * 2) = 2000 + 0 + 2 = 2002

      [1][0]

2000 + (1 * 4 + 0 * 2) = 2000 + 4 + 0 = 2004

      [1][1]

2000 + (1 * 4 + 1 * 2) = 2000 + 4 + 2 = 2006

      [2][0]

2000 + (2 * 4 + 0 * 2) = 2000 + 8 + 0 = 2008

      [2][1]

2000 + (2 * 4 + 1 * 2) = 2000 + 8 + 2 = 2010

      [3][0]

2000 + (3 * 4 + 0 * 2) = 2000 + 12 + 0 = 2012

      [3][1]

2000 + (3 * 4 + 1 * 2) = 2000 + 12 + 2 = 2014

 

Can you see the pattern yet?

 

●The outline for any dimension beyond 2 is the same besides little differences for example take the 3-dimensional array:

 

double cubetable[5][2][2];

You have to keep in mind that you need 8 bytes of storage so now you’ll have (5*2*2)*8 = 160 contiguous bytes of storage.

“Even the calculation for the element address remains the same with the addition of an extra number ([][][]) instead of two ([][])

 

 

●Multidimensional arrays are the successors to one-dimensional arrays due to the fact that they are more useful.  They can be used to store just about any kind of information such as a favorite sports teams seasonal results to business investments.

 

●Multidimensional Array Quiz

 

1.     What is the index number of the first element of an array with 80 elements?

A.   10

B.   80

C.   79

D.   1

E.   0                                              Ans: E

2.     What is the last in index number from the array question above?

A.   0

B.   80

C.   79

D.   1

E.   10                                            Ans: C   

 

3.     What statement would declare a Multidimensional Array with five rows and two columns?

           A. int vectornum = {1,2,3,4,5, (2)};

           B. int numvector = {1,2,3,4,5};

           C. int tablenum = {(5.2)}

           D. int numtable[5][2];

           E. int numtable[2][5];                   Ans: D

 

4.     Multidimensional Arrays do not require contiguous storage in the Random Access Memory?  True or False                        Ans:False

 

    5.  A Multidimensional Array statement int numtable[10][2]; requires          20 bytes of contiguous storage?  True or False                        Ans:False