Mastering Array

 

Hello ! Nice to meet you ! It seems you are cleverer day by day. You are even be able to write simple games ! I have a little problem on my health recently, so I have to get some good rest. Topics that will be discussed this time are :

  1. What is an array.
  2. Declarations.
  3. Using arrays.
  4. Two-dimensional array.
  5. Multi-dimensional array.
  6. Special case of array.

Let's begin !

What is an array exactly ? Well it is like a variable that is able to store a bunch of data. Think about drawers. Drawers have several latches, don't they ? Each latch could keep things safe. Eventhough they have so many latches and store things, they are called drawers. Confused ? Let's look at the declaration :

var
  arrayname : array [x..y] of vartype;

Example :

var
  ourdata : array[1..100] of byte;
  myarray : array[5..25] of char;

Yes, yes, name it whatever you want. Array declarations are located inside the var block. You can declare arrays as global variable or local variable, just whenever you want it. How to use it ? Suppose, I have an array :

var
  x : array[1..30] of byte;

It means that x has 30 elements ranging from 1 to 30. Each element can be accessed individually, not affecting others. Suppose I want to access the first element, it would be :

  x[1]:=10;  { Suppose I want to fill the first element with 10 }

Then, I would like to access the second element :

  x[2]:=15;

If you check the first element, it is still 10. Even I have assigned the second element with 15, the first one still retain its originality. Check out this simple program to gain better understanding :


var
  a : array[1..10] of byte;

begin
  a[1]:=10;
  a[2]:=15;
  a[3]:=a[1]+a[2];
  writeln(a[1]);
  writeln(a[2]);
  writeln(a[3]);
end.

As you can see that the variable a could store multiple values. Each element could be accessed without interfering others.

In what case the array contribute to the applications in real-life ? Suppose you have a list of names. You would probably prefer to have an array of names instead of declaring each variable names. Suppose : It would be nicer to store 10 names with an array like this :

var
  names : array[1..10] of string;

rather than having each variables like this :

var
  name1, name2, name3, name4, name5,
  name6, name7, name8, name9, name10 : string;

Array makes us easier in writing the contents. Writing all the names with an array names above, would be :


  for i:=1 to 10 do
    writeln(names[i]);

You would love doing that rather than writing writeln statements with 10 variables intact. It is much easier and simpler.

What about if I have a table of x and y, could I access it with array too ? Yes ! But, you will need a two-dimensional array. It is the same, but the declaration is like this :

var
  table : array[1..5, 1..3] of byte;

That table is 5x3 in size. How to access it ? Well, like this :

  table[5,3]:=5;
  table[1,2]:=4;
  table[4,1]:=table[1,2]*table[5,3];

Yes, it is pretty much the same. Well, array helps you make spreadsheets, just like Lotus 1-2-3 or Excel.

How about the three-dimensional table or more ? It also the same. Look at the declaration :

var
  table3d : array [1..5, 1..4, 1..6] of byte;

Accessing it is all the same :

  table3d[3,4,5]:=6;

And so on. You may extend it to 4-D array, like :

var
  table4d : array[1..2, 1..3, 1..4, 1..5] of byte;

Accessing it is analogue to the previous examples. 5-D array, or even 100-D array is just the same in concept.

Here are the special case of array in Pascal : You can declare array like this :

  x   : array[3..40] of integer;
  idx : array['A'..'Z'] of string;
  a   : array['a'..'z'] of byte;
  n   : array[byte] of integer; { The same as array[0..255] of integer; }

Or even like this :

  schedule : array[Monday..Saturday] of string;
  carprice : array[Honda..Suzuki] of longint;

These types of array will be discussed in depth in chapter 10.

Accessing them is just the same :

  x[3]:=4;
  x[1]:=5; {Illegal}
  idx['D']:='Dave';
  idx[1]:='XX'; {Illegal}
  a['a']:=65;
  n[0]:=1;
  n[255]:=10;
  n[5]:=5;
  schedule[Monday]:='Go meet clients at 10am';
  carprice[Honda]:=15000; {US$}

Well, well ! Got to practise a lot. Accessing array idx and a with loop is similar. Suppose c is a char variable :

  for c:='A' to 'Z' do
    idx[c]:='';
  for c:='a' to 'z' do
    a[c]:=random(100);

This kind of array, for me, is seldom used. The more usual ones are the array of schedule and carprice above. They use enumeration types. This will be discussed in chapter 10.

OK, that's all for this time !


Where to go ?

Back to main page
Back to Pascal Tutorial Lesson 1 contents
To the quiz
Back to Chapter 6 about procedures and functions
To Chapter 8 about processing strings
My page of programming link
Contact me here


By : Roby Joehanes, © 1996, 2000