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.
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;//** begin bSeen:=0;//Put zero in each of the subtotals //** bSeen:=0;//** bSeen:=0;//** bSeen:=0;//** clrscr; repeat writeln('Seen so far: Motorbikes:',bSeen, ' Two doors:',bSeen, ' Four doors:',bSeen, ' Other:',bSeen);//** writeln('Enter 1,2,3,4 or 99'); readln(sTmp); if sTmp='1' then bSeen:=bSeen+1;//** if sTmp='2' then bSeen:=bSeen+1; if sTmp='3' then bSeen:=bSeen+1; if sTmp='4' then bSeen:=bSeen+1; until sTmp='99'; writeln; writeln('Bye. Make a note of your subtotals! THEN press the "Enter" key.'); readln; end.
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, 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....
bSeen:=0; bSeen:=0; bSeen:=0; bSeen:=0;
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? :-)
Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.
....... P a g e . . . E n d s .....