Free Pascal with Terry

Array Help Document

What is an Array?



One book I have states that: The array is probably the most widely used data structure, in some languages it is even the only one available.. An array consists of components which are all the same type, called the base type; it is therefore called homogeneous structure... What does this mean and how is it implemented? Lets say we want to define 100 integers, we can declare them in the Var section like this MyIntArray: Array[1..100] of LongInt; And then to access each of them, we need a whole number to use as an index. So to add them all together we could first define a sum integer and set it to zero: Sum: LongInt; … Sum :=0; And then add all the array integers together. Sum := Sum + MyIntArray[1]; Sum := Sum + MyIntArray[2]; Sum := Sum + MyIntArray[3]; Sum := Sum + MyIntArray[4]; . . . Sum := Sum + MyIntArray[99]; Sum := Sum + MyIntArray[100]; WriteLn(Sum:6); Now that is a real pain in the read-end and probably worse than defining each one individually. But there is a better way, we can use another integer as the Index value instead of a whole number and stick it all in a loop: Sum := 0; For I := 1 To 100 Do Sum := Sum + MyIntArray[i]; WriteLn(Sum:6); In fact that is what the For loop was really designed for. Most computer languages have for loops of some form just to manipulate arrays. It is also possible to manipulate arrays with While and Repeat loops also, but you will have to increment your index values somewhere in your loop. Arrays can also be defined as Strings or doubles and even a complex structure of your own devising.