We already covered the RECORD data type in Pascal. This time we'll look at one more structured type in Pascal, the ARRAY, or larik as it's known in Indonesian.
What an ARRAY Is
What is an ARRAY, anyway?
An ARRAY is a data structure that holds a set of data with the same data type. Picture a row of lockers lined up neatly: each box has its number, and you can reach it directly through its index.
๐ท Illustration: a row of consecutive boxes representing array elements together with their index numbers.
To see the difference clearly, look at the following program:
program BuatArray;
uses crt;
var
nama1, nama2, nama3, nama4, nama5: string;
begin
clrscr;
nama1 := 'adi';
nama2 := 'budi';
nama3 := 'putri';
nama4 := 'putra';
nama5 := 'ida';
writeln('nama1: ', nama1);
writeln('nama2: ', nama2);
writeln('nama3: ', nama3);
writeln('nama4: ', nama4);
writeln('nama5: ', nama5);
readln;
end.
We have 5 variables, from nama1 to nama5, all with the string data type. Each variable holds one person's name. The code looks perfectly normal, doesn't it?
But what if you had variables nama1 through nama100?
Writing the variable names out one by one stops being effective. This is where the ARRAY comes in. With an ARRAY you declare the name variable just 1 time.
Writing an ARRAY in Pascal
An ARRAY declaration has 3 components: the length of the ARRAY, the data type of the ARRAY, and the name of the ARRAY variable. The format looks like this.
var
variable-name: array [lower-bound..upper-bound] of data-type;
The lower and upper bounds in an ARRAY declaration are the first and last index you can access. The data type is what will be stored in the ARRAY.
Say we want a variable nama with the string type that holds 5 elements. We can declare it like this.
var
nama: array [1..5] of string;
To access an element inside the ARRAY we use what's called an index.
The index is the number written after the ARRAY variable name. That number can't be lower than the ARRAY's lower bound, and it can't be higher than the ARRAY's upper bound. For example:
var
nama: array [1..5] of string; // the ARRAY nama has 5 elements
begin
nama[1] := 'Budi'; // fill in the value at index 1
nama[4] := 'Adi';
nama[6] := 'Ida'; // error because the ARRAY nama has no element 12
nama[0] := 'Rani';
end.
Because an ARRAY is also a data type, we can make an ARRAY whose data type is another ARRAY. This is called a multidimensional ARRAY. For example:
var
matrix2D: array [1..2] of array [1..2] of integer; // an array whose data type is an integer array
So how do you access an element of a multidimensional ARRAY?
Much the same way as before, still with an index. The difference is that this array is nested, so the first index still gives you an element that is itself an array. To take the value inside, you access the index twice, much like row and column coordinates.
๐ท Illustration: a two-dimensional table showing row and column indexes in an array.
var
M: array [1..2] of array [1..2] of integer; // an array whose data type is an integer array
begin
M[1][1] := 1; // access the first value of the first array in M
M[1][2] := 2;
M[2][1] := 3;
M[2][2] := 4;
end.
That one is called a 2-dimensional ARRAY, because you access the index 2 times. The concept is the same as reaching an entry in a matrix (using rows and columns).
A multidimensional ARRAY isn't limited to 2 dimensions either. It can go on to 3, 4, or even n dimensions, depending on what you need.
Using an ARRAY in a Loop
The advantage of an ARRAY is that it works with loops. Let's rebuild the code from the start of this tutorial.
program BuatArray;
uses crt;
var
nama: array [1..5] of string; // using an array
i: integer; // the loop variable
begin
clrscr;
nama[1] := 'adi';
nama[2] := 'budi';
nama[3] := 'putri';
nama[4] := 'putra';
nama[5] := 'ida';
// print nama[1] through nama[5] with a for loop
for i:=1 to length(nama) do writeln('nama[',i,']: ', nama[i]);
readln;
end.
Try running that program. The output will look roughly like this:
nama[1]: adi
nama[2]: budi
nama[3]: putri
nama[4]: putra
nama[5]: ida
No more calling nama1 through nama5 one at a time. By using the index feature of the ARRAY, we just use the variable i as that index.
There is one command that may look unfamiliar, namely length().
What does length() do?
It returns the length of an ARRAY or a STRING. So if you have an ARRAY with a lower bound of 1 and an upper bound of 10, the length of that ARRAY is 10.
It returns the length of a STRING too?
Yes, a STRING is actually an ARRAY as well. More precisely, an ARRAY of CHAR.
STRING is a standard data type in Pascal whose value is a word or a sentence. That word or sentence is made up of a few letter characters, and letter characters are also a standard Pascal data type, namely CHAR.
Everything you can do with an ARRAY also works on a STRING.
var
kata: string;
i: integer;
begin
kata := 'Belajar pascal di invasikode';
for i := 1 to length(kata) do writeln('Huruf ke-',i,': ',kata[i]);
end.
If you run it, that code prints the output one letter at a time.
Wrapping Up
That's enough for this tutorial, hope it helps :D
Next time we'll learn about pointers in Pascal.