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

Single Dimension Arrays and Two-Dimensional Arrays

 

Single Dimension Arrays.

*These arrays are basically a list of information of the same type that are stored in contiguous memory locations in index order. This kind of array is referred as a vector.

* They have to be explicitly declared so the compiler can allocate space for them in the memory.

*Int x [98]; in this statement we have an array called x of type integer and it holds 98 elements, from 0-97. Another example is int x[5]={1,5,8,7,9};

this means that the array contains these numbers.

*A very important thing is that all arrays have 0 as the index of their first element. What this means is that always there is only going to be one row , and the number tells us how many columns there are.

char a [7];   

  0 1 2 3 4 5 6
0 0 1 2 3 4 5 6

 

*The amount of storage needed to hold and array is closely related to its type and size. The formula is the following:

total bytes= size of (base type)*size of the array

Some Examples:

       a) float x [98]; here we are asking for 392 bytes. This is because a float has 4 bytes and the size of the array is 98.

       b) char x [7]; here we are asking for 7 bytes. This is because a char has only 1 byte and the size of the array is 7.

       c) double x [14]; here we are asking for 112 bytes. This is because a double has 8 bytes and the size of the array is 14.

*In memory they look like this:

        a) char a [7];

                Element                a[0]    a[1]    a[2]    a[3]    a[4]    a[5]    a[6]

                Address                1000   1001  1002  1003   1004  1005  1006

  This is assuming that the base location is 1000.

        b) short  a [3];

                Element                a[0]    a[0]    a[1]    a[1]    a[2]    a[2]

                Address               1500   1501  1502   1503  1504  1505

  This is assuming that the base location is 1500.

Two-Dimensional Arrays.

*We can say that a two-dimensional array is an array of one-dimensional arrays. In other words, it is a group of one-dimension arrays.  It is the simplest of multidimensional arrays. This kind of array is referred as a table or a matrix.

*As we say before, all arrays have to be declared.

*Int a [5][4]; in this statement we have an array called a of type integer and it hold 20 elements.

*These arrays are stored in a row-column matrix. The first index represents the row and the second one the column. Examples to visualize this:

        a) int  num [5][4];       

  0 1 2 3
0 1 2 3 4
1 5 6 7 8
2 9 10 11 12
3 13 14 15 16
4 17 18 19 20

        b) int  num [2][3];

  0 1 2
0 1 2 3
1 4 5 6

*The formula that yields the number of bytes needed is:

total bytes: size of 1st index*size of 2nd index*size of (base type)

Some examples:

       a) char a [5][4]; here we are asking for 20 bytes. This is because a char needs only one byte, first index is 5, and second index is 4

       b) float b [7][9]; here we are asking for 252 bytes. Just do the math. 7*9*4=252 (a float has 4 bytes).

       c) long  c [15][32]; here we are asking for 3840 bytes. 15*32*8= 3840 (a long int has 8 bytes).

*In memory they look like this:

       a) char a [4][3];

                a [0][0]    a[0][1]    a[0][2]

                a [1][0]    a[1][1]    a[1][2]

                a [2][0]    a[2][1]    a[2][2]

                a [3][0]    a[3][1]   a[3][2]

       b) float b [5][2];

                b [0][0]    b [0][1]

                b [1][0]    b [1][1]

                b [2][0]    b[2][1]

                b [3][0]    b[3][1]

                b  [4][0]   b[4][1]

       c) Given the following table, where is stored the value 10 in RAM if the base location is 1400? (Assume float)

  0 1 2 3
0 1 2 3 4
1 5 6 7 8
2 9 10 11 12
3 13 14 15 16
4 17 18 19 20

        The first thing we have to do is to locate the row and the column where the value is stored, in this case is [2][1]. Then we just do the mathematical operation: 1400+2*20+1*4= 1444. The 1444 is where 10 is stored in RAM. The 20 is the number of bytes that each  row requires and it has to be multiplied by the row, the 4 is the number of bytes that a float has.

 IN CONCLUSION: The difference between single dimension arrays and two-dimensional arrays are:

*What they mean

*How they are stored

*How you have to declare them

*The formula to get the number of bytes needed to hold the array

 If you need more help, here you can get some references and here you can get some exercises.