Site hosted by Angelfire.com: Build your free website today!

Menu bar
Data Structures And Number Systems
© Copyright Brian Brown, 1984-2000. All rights reserved.

Part 2
home prev next


ARRAYS
An array is a group of elements which are all of the same type, size and name. It can be thought of as a box with multiple compartments, each compartment capable of storing one data item.

To reference any information stored in a particular compartment of the box, we could use the notation,

 

	box_name[ compartment_number ] 
 

As we could have a number of boxes, it is appropriate to name them, and number each compartment within each box also. Numbering can begin at 0 or 1. It therefore implies that there are limits (called bounds) on a box size, an upper and lower limit. Numbered compartments outside this range indicate they are out of bounds.

So, we can rename the box as an array, and the compartments as elements. Within each element can be stored a single data item. An array of integers would store an integer value in each successive element of the array.

In Pascal, arrays are defined and used as follows,

 

	var 
		box1 : ARRAY [1..100] of integer; 

	begin 
		box1[ 1 ] := 1267; 
 

The example declares an integer array called box1 of 100 elements (numbered 1 to 100). This allocates 16 bits of memory storage per element, so the array consumes 200 bytes of memory storage.

Inside the main body of the program, after the keyword begin, the statement shown assigns the numeric value 1267 to the first element of the integer array box1. This is equivalent to storing the bit combination 0000010011110011 in the sixteen bits of memory allocated to element 1 of the array variable box1.

Pictorially, the memory storage of an integer array would look like,

 

	+---------+<---+ 
	+---------+  box[1] 
	+---------+<---+ 
	+---------+  box[2] 
	+---------+<---+ 
	+---------+  box[3] 
	+---------+<---+ 
	.         . 
	.         . 
	+---------+<---+ 
	+---------+  box[100] 
	+---------+<---+ 
	+---------+ 
	+---------+ = 8 bits of memory storage 
 

Obviously, the amount of storage bits per element depends upon the data type of the array. Generally, the sizes are as follows,

 

	 8 bits per element for each character 
	16 bits per element for each integer 
	32 bits per element for each floating point number 
 

Note that text strings are essentially an array of characters!

Using arrays in programs offers the following advantages,


home prev next

Home | Other Courses | Feedback | Notes | Tests

© Copyright Brian Brown, 1984-2000. All rights reserved.