HOME - - - - - - - - - Other material for programmers - - - - - - - - - Pascalite Tutorial Table of Contents

Pascal Programming: Arrays

This version of this tutorial is fine tuned for the open source FPC, aka Free Pascal. Most of what you read here should also directly apply if you are using Borland's Turbo Pascal (TP hereafter). It is also available for free. Please send me an email with "complaints" if something doesn't work!

If you have a Pascalite, or the software emulation, then there's a separate tutorial for you, covering much of the ground as presented here for the FPC/ TP crowd.

A special place to keep things: Arrays

Before I say what I'm about to, I will say: This tutorial will develop a program which will work with FPC or Turbo Pascal. Now that's out of the way, I come to: Be sure to look at the previous tutorial before attempting this one. (In the previous tutorial, you could only read about something the Pascalite people could do.) In it we looked at using a computer to record the activity of a hamster. The "For... To... Do..." statement was introduced. In this tutorial we are also going to track some events, and will meet arrays for the first time. They are a special type of data, or a special sort of variable, if you prefer to think of them that way.

This might have left for "Level 2" of the tutorials, but it introduces a fundamental programming tool: arrays.

The program we will make could be used for a traffic survey. By "traffic survey", I mean the exercise in which someone sits at a roadside for an hour and counts how many motorbikes, how many 2-door sedans, how many 4-door sedans, and how many none-of-the-above vehicles pass by. Obviously, such a program could be adapted to many other uses.

As developed here, the program will have a lot of user unfriendliness. There are lots and lots of things which you would do to it if it were really going to be used for traffic surveys... but for the purposes of the tutorial, the following is uncluttered.

The program will be used as follows: Users will start it, at which time they will see "Enter 1, 2, 3, 4, or 99, and press enter." They will enter 0 if they see a motorbike, 2 or 4 for a 2 or 4 door sedan, 3 for an "other" vehicle, and 99 to quit the program. They will see running subtotals of the vehicles seen.

It always pays, by the way, to write out a description like the above before you start writing a program.

We've already looked at simple variables. They allow us to keep things so that we can refer to them. Now we're moving on to arrays. Like a simple variable, they are a place for keeping something. In addition to the characteristics of a simple variable, they allow us extra ways, clever ways, to specify which piece of information we want. Type in the following. Notice there is lots of scope for using copy/ paste to decrease typing and increase accuracy.

Don't let the blank lines confuse you... You can always put them in where ever you want to in order to help make logical blocks of the program show up. The indenting is there for the same reason.

program Traffic;
uses crt;

var sTmp:string;
      bSeen:array [1..4] of byte;//**


bSeen[1]:=0;//Put zero in each of the subtotals //**

   writeln('Seen so far: Motorbikes:',bSeen[1],
             '  Two doors:',bSeen[2],
             '  Four doors:',bSeen[4],
             '  Other:',bSeen[3]);//**

   writeln('Enter 1,2,3,4 or 99');
   if sTmp='1' then bSeen[1]:=bSeen[1]+1;//**
   if sTmp='2' then bSeen[2]:=bSeen[2]+1;
   if sTmp='3' then bSeen[3]:=bSeen[3]+1;
   if sTmp='4' then bSeen[4]:=bSeen[4]+1;

until sTmp='99';

writeln('Bye. Make a note of your subtotals! THEN press the "Enter" key.');

That's all there is to it! I've marked lines you should note especially carefully with //**s. As I said, it could be done fancier, it could be done better... but what we have so far WORKS, and it shows you an array (bSeen) in action. Now we'll talk about it a bit.

Our program uses the array bSeen. bSeen provides four places to store a byte. A particular one of them, say bSeen[2], is called "an element of the array". In the program in the tutorial we haven't taken advantage of the power of arrays... we could have done what we've done so far with four variables named bSeen0, bSeen1, bSeen2 and bSeen3. For a very modest example of the power of arrays, let's go back to the start of the program, back to the part that initializes each element of bSeen to zero. We had....


Remember the important "For... To... Do..." statement we met in the previous tutorial? If we merely add "bTmp:byte" to the variables declaration, we can replace the four lines above with:

for bTmp:=1 to 4 do bSeen[bTmp]:=0;

Look at that closely; make sure you see what it's doing, why it achieves the same thing that the previous answer achieved. It may not see very impressive. However, consider what an improvement it would be if 150 bSeen elements had to be initialized! And there are more subtle things that you can do which can only be done through the power of arrays.

And so to bed! Unless you want to go back to the table of contents for another one? :-)

Please also note that I have two other sites, and that the following search will not include them. They have their own search buttons.

My Sheepdog Guides site.
My Arunet site.

Go to the sites above, and use their search buttons if you want to search them.
To search this site....

Search this site or the web powered by FreeFind

Site search Web search
The search engine merely looks for the words you type, so....
*    Spell them properly.
*    Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....

You can also search this site without using forms.
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.

Want a site hosted, or email? You can also help me if you sign up via this link to 1&1's services. (I wouldn't recommend them unless I was happy after several years as one of their customers. The Google advertisers pay me. I know nothing about them or their services, of course.)

Valid HTML 4.01 Transitional Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.

AND passes... Valid CSS!

Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Also, I have some of my pages' traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Why do I mention the script? Be sure you know all you need to about spyware.
Editor's Main Homepage
How to email or write this page's editor, Tom Boyd

....... P a g e . . . E n d s .....