
==== Step 2:
Step Title     : Arrays
File Name      : MP_2.txt
Author         : Michael Preslar / E_Maus
Author Note    : heres some arrays..
Example Source : none
Date           : September 27th, 1998
Length         : 8 pages
Layout         : - intro to arrays
                 - syntax of a simple array
                 - usage of a simple array
                 - intro to multideminsional arrays
                 - syntax of multideminsional arrays
                 - usage of multideminsional arrays
                 - A word of warning about the 64k limit
                 - Intro to advanced arrays
                 - The string type is actually an array!
                 - retrospect
====
Please refer back to "intro.txt" for complete documentation of copyrights,
and author information.

Step 2 Foreword
---------------
In step 1, i said the next lesson would be completely devoted to arrays. So
here it is. Im going to go over every aspect of arrays that I know of and then
some. This might be a bit tough, so you might have to re-read it a few times.

  =========================== Intro to arrays ==============================

Take a look at these.. See if you can basically understand them..

var
  TFarray   : array[1.15] of boolean;
  chararray : array[1..255] of char;
  Sarray    : string;

What do they all have in common? True, they are all arrays, even the string.
As a matter of fact, that second variable type (array[1..255] of char) is the
exact same thing as a string. But whats the overall likeness among the three?
Theyre all collections of like things. TFarray is 15 different boolean
variables, and chararray and string are 255 different char variables.

  ====================== Syntax of an simple array =========================

Like the other variable types, an array can only be used when either
declaring a variable or declaring a typed constant. An array's syntax?
Quite simple.. Lets disect the first example from above..

Tell the compiler, "this a variable (var) called TFarray (tfarray) of
type (:) array (array) with a value of ( [1 ) all the to (..) 15 ( 15] ) of
type boolean ( of boolean );

In other words: var TFarray : array[1..15] of boolean;

get it?

  ========================= Usage of a Simple array ========================

Alright.. so you know how to declare an array, but how to use one? Lets use
that first example again. its an array of 15 different boolean variables..
Heres a few examples..

  "tfarray[1]" would get you the first one..
  "tfarray[12]" would get the 12th one..
  "tfarray[4]" would get the fourth one..
  etc etc etc

To get the value, youd use the variable's name (tfarray), then whichever one
you wanted to access in brackets. What kind of value are you going to get?
Whatever the array is of is what. If its an array of boolean, the value will
be a boolean, if its an array of char, the value will be a char.

  =================== intro to multideminsional arrays =====================

Woah now.. Thats a $5 word if Ive ever seen one.. But dont let it scare you.
Multideminsional arrays are simple. Heres some examples.. If you follow
simple arrays, then you can easy follow these..

var
   BigTFarray  : array[1..15,1..15] of boolean;
   ScreenArray : array[1..80,1..25] of char;
   CubeArray   : array[1..4,1..4] of word;

See? That wasnt that hard.. Again, theyre just a collection of like things,
however, you have more than one collection per array. If youll notice the
second array, ScreenArray, and notice its limits, youll see that it is
actually the limits of the monitor screen in 80x25 textmode.

Now let me add a bit more to the confusion.. you can have as many different
collections as memory allows. For example..

var VeryBigTFArray : array[1..2,1..2,1..2,1..2,1..2,1..2,1..2] of boolean;

This is completely valid. What youd use such a variable for, i have no idea.

  ================== syntax of multideminsional arrays =====================

The syntax of a multideminsional array is a lot like simple arrays.. Again,
lets disect the first example..

"var" then the variables name, BigTFArray, of type, ":", array.. the first
deminsion will have a minimum value of 1, and a maximum value of 15. The
second deminsion of the array will have a minimum value of 1, and a maximum
value of 15. and the array will be of boolean type. In other words..

var BigTFArray : array[1..15,1..15] of boolean;

Note the comma between the different deminsions. This is just a delimeter
that says, "next deminsion please".

But what if you wanted to make the first deminsion 15 different booleans,
and the second deminsion 15 different bytes? Sorry. That cant be done
directly. There is a way to have mixed types in arrays, but we havent gotten
there yet. Just keep in mind that an array is a collection of like things.

  =================== Usage of multideminsional arrays =====================
Alright, so we understand how to declare multideminsional arrays, but again,
how to use them? The same basic way a simple array is.. Watch..

First, lets declare it..

var screenarray:array[1..80,1..25] of char;

Then say you saved every character on the screen to this array (yes, it is
possible!), but only wanted to access a few of the chars..

screenarray[1][1] would give you the character in the top left corner..
screenarray[80][1] would give you the 80th character across, on the first
  line.. Also known as the top right corner.
screenarray[1][25] goes across 1, but down 25. The bottom left corner..
screenarray[80][25] goes across 80, and down 25. the bottom right corner.

Simple, eh? First, the variable name.. screenarray.. then the first
deminsion in brackets.. [1].. then the second deminsion in brackets.. [25]..
and that would give you a char value.

  ============== A word of Warning about the 64k limit =====================
There is a limit to arrays that Ive not discussed yet, but it has been
mentioned in mp_1.txt.. The 64k limit, or as Kateness put it, "the mem thing"
is easy to understand.

A majority of BBS's today run on 16 bit operating systems. It is for this
reason that you have to limit the amount of memory an array uses to 64k.
Keep in mind that 64k is the same thing as 65,535. If your array is larger
than that, youll get the error, "structure too large".

As a matter of fact, if the source file youre currently working on has either
"program <whatever>" or "unit <whatever>", you have to limit yourself to
64k of memory usage there as well. Whats a unit? We will get to that in time.

  ====================== Intro to Advanced Arrays ==========================
Heres something I want you to look at. If you dont understand it, its okay.
This isnt something you have to learn, but it does give an example of the
power of arrays in Pascal.

Program arrayexample;

const
  OffOn : array [Boolean] of String[5] =
    ('False','True');
var
  v : Boolean;

begin
v:=true; {set v to true}
writeln('The variable V is currently ',offon[v]);
v:=false;
writeln('The variable V is currently ',offon[v]);
end.

This is completely legal. How? Remember that an array has to be of a certain
type. "boolean" is a type, either 0 or 1. "offon" is a typed constant.
offon[0] or offon[false] both correspond with "false", while offon[1] or
offon[true] both correspond with "true". How can you make these type arrays?
Well, you need to learn how to make your own types, which is something we'll
cover in time.

  ================ The string type is actually an array! ===================
What was that? the string type is actually an array? Sure is. "string" is
declared as:

  string:array[0..255] of char;

Thats 256 different characters. Yes, 0 counts as well, almost. Read this:

Question: Since I can work with a single element of an array, can I work with
          a single element (a character) in a string?

Answer  : Yes! If you had a string variable called "s", then s[1] would be
          the first letter of the string. You could do the same thing all
          the way out to the last letter of the string.

However, s[0] is not one of the characters from the string. s[0] would be the
"length" byte of the string. Element 0 (s[0]) holds how many characters are
in the string.

Not only can you access each element in a string variable, but you can limit
how long a string variable will be. Try this on for size:

program stringarray;

var
  str1:string;
  str2:string[10];

begin
str1:='Hello there, world!';
str2:='Hello there, world!';
writeln(str1);
writeln(str2);
end.

When you run this program, youll notice the difference. str1 holds the entire
line "hello there, world!", but uses a full 256 (255 characters + element 0)
bytes of memory. str2, however, only holds "Hello ther" since it can only
hold 10 characters. And yes, the space does count as a character. But str2
only uses 11 (10 characters + element 0) bytes of memory. If you dont limit
the string, it defaults to 255.

  =========================== Retrospect ===================================
In this lesson, we covered arrays..

- Single and multideminsional.. the syntax and usage of both.

- the 64k limit.

- I hit "advanced arrays". Dont worry if you dont understand advanced arrays.
  With BBS programming, its not something that would be used a whole lot. If
  you want to learn more about advanced arrays, however, get in touch with me.
  Ill give you some example code to study.

- then strings are arrays..