Friday, October 16, 2009

Two – Dimensional Arrays

A two - dimensional M x N array A[M][N] is a collection or list of M x N data elements such that each element is specified by a pair of integers (such as J, K), called subscripts, with the property that

LB of Row Index £ J £ UB of Row Index

and LB of Column Index £ K £ UB of Column Index

In C/C++, lower bounds, LB, are zero. The element of A with first subscript J and second subscript K will be denoted by A [J][K].

Two–dimensional arrays are analogous to matrices in mathematics and tables in business. In it, data can be logically viewed as a table with columns & rows.

2–dimensional array can be declared as A[ 4] [5] in C/C++.

Like 1–dimensional array, 2–dimensional array is a linear and structured data type made up of a finite, fixed–size collection of ordered homogenous elements. Its accessing mechanism is direct access.

All characteristics of 1–dimensional array, like upper bound (UB), lower bound (LB), base address, and size (W) applies as well to 2–dimensional (or multidimensional) array.

Number of elements of 2–dim array =

(UB of row index – LB of row index + 1) x (UB of column index LB of column index + 1)

In case of an array A[4] 5]

no. of elements = 4 x 5 = 20 (rows) x (column)

no. of bytes = no. of elements x bytes per element

= 20 x 2 bytes per data element = 40, if each element uses two memory bytes.

Computer memory is one long sequence of bytes accessed by addresses beginning at 0. Therefore, a 2–dimensional array is stored with all the elements
of a row in sequence, followed by all the elements of next row in sequence and

so on. Such array storage is said to be stored in row-major order

OR

Alternatively, the array can be stored with all the elements of a column in sequence, followed by all the elements of next column in sequence and so on. This kind of storage is said to be column–major order. Figure shows these two ways when A is a 2–dim 3 x 4 array.

3.1 ADDRESS CALCULATION OF ARRAY ELEMENT A [J][K] :

The computer keeps track of Base (A), the address of the first element

A[0][0] of A[M][N], and computes address Loc (A[J][K]) of A[M][N] using the formula

Column–Major Order:

Loc (A[J][K] ) = Base (A) + W [M x K + J ]

OR

ROW MAJOR ORDER:

Loc (A [J][K]) = Base (A) + W [N x J + K ]

W denotes the size, i.e; number of bytes per data element of the array A, M is total numbers of rows, and N is total number of columns in the array.


No comments:

Post a Comment