PDA

View Full Version : Arrays!

Santa_Clause
04-17-2008, 02:39 PM
Arrays

An array can be defined as a variable that holds a specified number of the same variable. What does this mean?

Basically, an array of variables holds a certain number of a common variable. The amount of values in the array ‘Length’ can either be defined when declaring the variable, or can be defined inside a procedure or function.

The position of a value in an array ‘Index’ is important when dealing with array. The first value in an array usually starts with an index of zero, with each successive value in the array having an index one more than the index of the previous value.

Observe this piece of code:

Var
NumberArray : Array [0..10] of Integer;

Begin
NumberArray := [12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132];
End.

In the above example, the length of the array is defined in the variable declaration ‘Array [0..10] of Integer’. This means that the first value in the array will have an index of zero, while the last value in the array will have an index of ten. The values of the array are then declared.

Splitting the array:

NumberArray[0] := 12
NumberArray[1] := 24
NumberArray[2] := 36
NumberArray[3] := 48
NumberArray[4] := 60
NumberArray[5] := 72
NumberArray[6] := 84
NumberArray[7] := 96
NumberArray[8] := 108
NumberArray[9] := 120
NumberArray[10] := 132;

It is much easier and professional to declare an array in one line if possible, but the splitting of the array can also be used to declare it:

Var
NumberArray : Array [0..10] of Integer;

Begin
NumberArray[0] := 12
NumberArray[1] := 24
NumberArray[2] := 36
NumberArray[3] := 48
NumberArray[4] := 60
NumberArray[5] := 72
NumberArray[6] := 84
NumberArray[7] := 96
NumberArray[8] := 108
NumberArray[9] := 120
NumberArray[10] := 132
End.

Length(S) / High(X)

When looping through arrays, knowing its length is just as important as knowing what an array is. To find the length of an array, we use the function ‘Length’. The length of the array means the number of values that are contained in it. Note that since the array index usually starts from zero, an array length of one means that there will only be an array index of zero. We can then deduce that if the first index of an array is zero, the last index of the array is equal to the length of the array minus one.

Observe this piece of code:

Var
NumberArray : Array [0..10] of Integer;

Begin
NumberArray := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
End.

The first index of the array is zero, and its value is one. The second index of the array is one, and its value is two, and so on.

Simply put:

NumberArray[0] := 1
NumberArray[1] := 2
NumberArray[2] := 3
NumberArray[3] := 4
NumberArray[4] := 5
NumberArray[5] := 6
NumberArray[6] := 7
NumberArray[7] := 8
NumberArray[8] := 9
NumberArray[9] := 10
NumberArray[10] := 11

Notice that the number in between the parentheses ‘[ ]’ is always one less than the value contained in the array index.

The length of the array is usually used for looping through the array. A for/to/do loop is used in order to check every value in the array.

Observe this piece of code:

Program New;

Var
NumberArray : Array [0..10] Of Integer;
I : Integer;

Begin
NumberArray := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

For I := 0 To Length(NumberArray) - 1 Do
WriteLn(IntToStr(NumberArray[I]));
End.

In this example, the value of our integer ‘I’ changes, going through all the numbers from the first array index ‘zero’ to the last array index ‘Length(NumberArray) - 1’. It then does a WriteLn of the number that is contained in each specific index of the array.

The ‘High’ function ultimately returns the length of an array minus one, but it does have a certain formula that is unimportant.

Note : Arrays can hold any type of value in SCAR, ranging from strings to TPoints. The ‘Length’ and ‘High’ functions are compatible with any type of arrays.

______________________________________

Well, there goes another tutorial. I really do hope that some of you are learning something. If you have any questions, feel free to ask me. Till the next tutorial, ciao!

Naum
04-17-2008, 02:41 PM
Hah Very good tut, just like mine, everyone's writing tuts now :).

Explains it well rep++

W00t first post.

Santa_Clause
04-17-2008, 02:59 PM
Thanks for the reputation. And yes, it's more interesting writing a tutorial for everyone to see rather than explaining one thing over and over to different people on different occasions.

Nava2
05-11-2008, 01:34 AM
Small but concise tut. Repped. You should go more into Array of Strings etc though. Not just mention them.

Nava2

bullzeye95
05-11-2008, 02:15 AM
Nice tutorial. If you even look at this again, you should add that if you want speed while looping through an array, you should create an integer and do YourInt:= High(TheArray);, and then, for the loop, do for SomeVar:= 0 to YourInt do ...
It's quite a bit faster.

Zigon
06-12-2008, 05:58 AM
nice tut! I quickly wanted to jump into array's but had no idea how to declare or anything, very helpful.