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

Pascalite Programming: Arrays

This version of this tutorial is fine tuned for a Pascalite, or the free software Pascalite emulation. Please send me an email with "complaints" if something doesn't work!

If you have the open source FPC, aka Free Pascal or Borland's Turbo Pascal (TP hereafter), then there's a separate tutorial for you, covering much of the ground as presented here for the Pascalite crowd.

A special place to keep things: Arrays

Be sure to look at the previous tutorial before attempting this one. 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.

In this version of the hamster monitor, we're going to extend it to monitor four hamsters!

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

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. You'll need to be careful to distinguish zeros and ohs... boB0Was is bee oh (for Boolean) bee zero (for the first element of port B).

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 FourHamsters;

var boB0Was,boB1Was,boB2Was,boB3Was: boolean;
      bTurns:array [0..3]of byte;


bTurns[0]:=0;{Put zero in each of the counters}

boB0Was:=b0; {Find out what the switches are set to so far}


if not(b0) then boB0Was:=false;
if not(boB0Was) and b0 then begin

if not(b1) then boB1Was:=false;
if not(boB1Was) and b1 then begin

if not(b2) then boB2Was:=false;
if not(boB2Was) and b2 then begin

if not(b3) then boB3Was:=false;
if not(boB3Was) and b3 then begin

until 4=5;

The program so far will watch switches b0, b1, b2, and b3. It will count how many times each one is turned on. It won't yet display how many times each one is turned on, though.

On an ordinary PC, it would be no problem to show four numbers on the screen. (one for the number of times each switch was turned on). However, without using the LCD screen, showing four numbers on a Pascalite would be hard. To get around this, I've decided to design the program as follows: d0, d1, d2... d7 will show a number in binary, as in the first way of showing answers in the previous tutorial. WHICH number it shows will depend on the setting of switches a0, a1, a2 and a3. It will be up to the user to remember that only one of these four switches may be turned on at any time. (In a commercial product a better solution would have to be implemented, but the extra code wouldn't have any value to the purpose of this tutorial). The program will "work" even if you do have more than one of the counter selection switches on... you should be able to see what it will do when, say, switches a2 and a3 are on. It that instance, the count of b3 "ons" will be displayed, won't it... which shouldn't be allowed to be possible, given that the user might just have most recently turned a2 on, and thus might be thinking they're looking at the count of a2 "ons".

To provide for the output of the count, and selection thereof, just add the following. Again: make use of copy/ paste if you know how to.. but it isn't essential if you don't.

if a0 then write(portd,bTurns[0]);
if a1 then write(portd,bTurns[1]);
if a2 then write(portd,bTurns[2]);
if a3 then write(portd,bTurns[3]);

Run the program again. Warning: While the program would run just as you would expect it to on a real Pascalite, the free virtual Pascalite and the simulator that comes with it runs more slowly than you can switch the virtual switches on and off. Because the program is getting relatively big, it will appear to fail to count some "on"s unless you switch on and off quite slowly. Be sure to set the simulator to run at its top speed, and wait at least 2 seconds between switchings, if you want to be fully safe.

So. Now we have a program using the array bTurns. bTurns provides four places to store a byte. A particular one of them, say bTurns[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 bTurns0, bTurns1, bTurns2 and bTurns3. For a very modest example of the power of arrays, let's go back to the part of the program that starts each element of bTurns off at zero. We had....


After we add c1 to the variables declaration, we can replace those lines with:

for c1:=0 to 3 do bTurns[c1]:=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 bTurns 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 .....