CHAPTER 5

ARRAY:  ARRANGEMENT OF DATA

You may have heard the following expressions: array of flowers, array of thoughts, or an array of soldiers standing by. What do all these expressions have in common? Each of the above has several alike elements that are arranged in order (one next to the other). In the world of programming, an array has similar properties that are arranged in the memory cells (locations). One cell follows another with each holding information of the same kind.

DEFINITION OF AN ARRAY 

An array is a contiguous and homogeneous memory location identified by a name.  The access to each location is done through an index or subscript. Contiguous means the locations (memories) that are continuously next to each other. Homogeneous meaning the cells can only hold data of the same kind. All the data is of the same type: integer, float, etc.  The index is a number that represents each room, beginning with 0 and ending with a defined maximum value.  The index uniquely identifies each cell allowing access to a corresponding location.  

HOW DO YOU DECLARE A VARIABLE ARRAY? 

The way to declare an array is the same as declaring other single variables, such as int x, except that the maximum size of the array must be specified, for example int x[10].  To declare an array, start with the data type, give the array a name, and specify the maximum size of the array by enclosing the number of elements in brackets. The following line of code illustrates the above process:                                               

int  item[10];  

The variable item is declared as an array of 10 locations that each holds an integer value. 

ARRAY SIZE AND ITS RANGE  

When declaring an array, the size determines the number of memory locations that are reserved for the array. Note that the index of the array starts at zero rather than one.  For example, and array of size ten, the index ranges from zero to nine.  Why zero instead of one? One answer is that the internal memory addresses start from zero. Therefore, the compiler doesn't need to adjust the conversion of an array to its address. In the following array declaration, note the array’s range runs from zero (0) to nine (9). 

int item[ 10 ];  // Array range of 0 to 9 will access each element of the array.