Page 57 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 57

Fundamentals of Data Structures




                    Notes          structure, i.e. a two-dimensional array, to store the positions of the chess pieces. Two-dimensional
                                   arrays use two indices to pinpoint an individual element of the array. This is very similar to
                                   what is called “algebraic notation”, commonly used in chess circles to record games and chess
                                   problems.
                                   In principle, there is no limit to the number of subscripts (or dimensions) an array can have.
                                   Arrays with more than one dimension are called multi-dimensional arrays.
                                   While humans cannot easily visualize objects with more than three dimensions, representing
                                   multi-dimensional arrays presents no problem to computers.
                                   In practice, however, the amount of memory in a computer tends to place limits on the size of an
                                   array. A simple four-dimensional array of double-precision numbers, merely twenty elements
                                   wide in each dimension, takes up 20^4 * 8, or 1,280,000 bytes of memory – about a megabyte.


                                          Example: you have ten rows and ten columns, for a total of 100 elements. It’s really no
                                   big deal. The first number in brackets is the number of rows, the second number in brackets is
                                   the number of columns. So, the upper left corner of any grid would be element [0][0]. The
                                   element to its right would be [0][1], and so on. Here is a little illustration to help.

                                                           Figure 4.4: Multi-dimensional array

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

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


                                   Three-dimensional arrays (and higher) are stored in the same way as the two-dimensional ones.





                                     Notes  They are kept in computer memory as a linear sequence of variables, and the last
                                     index is always the one that varies fastest (then the next-to-last, and so on).

                                   4.2.1 Two-Dimensional Arrays

                                   A two-dimensional m x n array is a collection of m .n data elements such that each element is
                                   specified by a pair of integers (such as J, K), called subscripts, with the following property that,
                                                                I ≤ J ≤ m and I ≤ K ≤ n
                                   The element of A with first subscripts j and second subscript k will be denoted by

                                                                   A J,K or A[J, K]
                                   Two-dimensional arrays are called matrices in mathematics and tables in business applications;
                                   hence two-dimensional arrays are called matrix arrays.
                                   You can declare an array of two dimensions as follows:
                                   datatype array_name[size1][size2];

                                   In the above example, variable_type is the name of some type of variable, such as int. Also, size1
                                   and size2 are the sizes of the array’s first and second dimensions, respectively.





          50                                LOVELY PROFESSIONAL UNIVERSITY
   52   53   54   55   56   57   58   59   60   61   62