Beginning Programming 3 - Section Two

by Brad Moore

Building on Arrays

Adding to Our Understanding of Arrays

So that was the program we say in section one. Not a lot of code, but a fun program to play anyway. Can you imagine what the IF-THEN-END IF statements would have looked like if there were 24 possible answers? That would have meant 23 block IF statements. That is a lot of code, but there is a simpler way.

It is the way of the array! Arrays give us tremendous power when processing things that are similar in nature (of course they are useful in many areas besides this as well). In our case, the text printed out in response to the question has a uniform property. That property is the value associated with each text string. Notice that every time we generate a 1 for the value of number we always print the text string "MAYBE..." and when we generate a 2 we print the text string "NO! - Never - Don't ask again!", and so on.

If we chose to we could load each one of these responses into a string array and we would be able to greatly simplify the programming to accomplish the same result. But first, lets build on our understanding of arrays in general.

You will remember from part two of the series that variable arrays allow the programmer to multiple instances of the same variable. Yet every instance of the variable is unique. It has a unique (yet common) name and they each can have unique values associated with them.

We used the analogy of the row of mailboxes in the last article when we discussed arrays. Each mailbox on the fence post was identical. Each was black and had the same dimensions and the same red flag and a white number of the same size and font written on the front. Yet each mailbox was uniquely its own, a member of a collection of mailboxes, identified by its mailbox number. Each mailbox had unique contents for a specific addressee. Mailbox seven may have had hunting magazines and mailbox two might have had ballet tickets. The mailboxes are identical, but the names and the contents are unique.

What I have painted a picture of is an array of black mailboxes. In the same way an array variables have many of the same characteristics. They all look the same (they might be stings or they might be numeric - those are the choices), but they are all identified uniquely and hold unique data. For instance if our array were one called mailbox$() - an array of string variables - then mailbox$(1) might contain my name, where as mailbox$(2) might contain your name. They are all mailbox$() array!

Is it making sense yet?

Lets try a little experimentation. We will use a string array called name$(). It will have six elements: Dave, Tom, Fred, Jack, Steve and Mark. Lets assign a name to each of the six elements of the array (type this in on your computer in your copy of Liberty Basic):

name$(1) = "Dave"
name$(2) = "Tom"
name$(3) = "Fred"
name$(4) = "Jack"
name$(5) = "Steve"
name$(6) = "Mark"

Using our mailbox analogy, lets look at what we have done. We have created six mailboxes on the mailbox rail. They all look the same. They are all called name$(). Each of the six mailboxes has a unique number painted on the front. The numbers start at one and run up to six. These are the array subscripts. Next inside each mailbox is a unique bit of data - a name in our case. If we look inside mailbox 1 (called name$(1)) we will see the name "Dave" that we stuck in there. We look into an array by referencing it by its name and subscript. To print the value assigned to the first element in the array (we have been calling it mailbox 1 in our analogy here) we would execute the following print statement:

Print name$(1)

Lets go ahead and print all six values stored in the array out to the screen:

Print name$(1)
Print name$(2)
Print name$(3)
Print name$(4)
Print name$(5)
Print name$(6)

So far there is nothing we have done that an ordinary variable would not have been able to do. Consider for a moment if we had sixty elements in our array. We would not want to print them out as we did above. Arrays give us the power to dump their contents in a simple FOR-NEXT loop.

As you may recall from our new commands in the previous installment of this series, we said the FOR - NEXT loop is used to cycle through a block of code a specified number of times. The command syntax is as follows:

FOR variable = start TO end
statements...
   ...
   ...
NEXT variable

The FOR statement works like this: First time in it assigns the value of start to the variable named "variable" (you can call it what ever you want - I like to use "x" or "y" or something simple like that). Then it will cycle through the statements that are with in the code block (between the FOR statements and the NEXT statement), causing them to be executed each time through. It will increment the value of the variable "variable" each cycle. Once the value of variable is equal to the value of end the program will move on to the statement following the NEXT statement as the execution of the FOR loop will have finished.

Look at what the combination of the FOR-NEXT loop and array variables can do:

for x = 1 to 6
   print name$(x)
next x

Those three simple lines printed the contents of all three variables to the screen. DO you see why? The value of the variable "x" was incrementing as the loop cycled. The first time through it was equal to 1. Next we referenced the array variable by using a variable as the subscript, in this case "x". So the first time through the PRINT statement printed name$(1), the next time through it printed name$(2), and so on until all six were printed.

This is the power of the array. Had our array had sixty elements, we still could have printed them all with only three lines of code!

There is another component involved in array usage. Arrays of 10 or more elements must be declared. In fact it is good programming practice to declare any array of any size. We have been using small arrays, so we have not bothered to declare them yet. Declaring and array causes Liberty Basic to set aside physical memory for the array. You can declare arrays with any number of elements, but every element will consume some of you systems memory while the program is running. The limit is the amount of memory your system has.

Liberty Basic uses the DIM statement to declare and set aside memory for an array. Most people talk about arrays being "DIMensioned" and not declared. The Liberty Basic help file says the following about the DIM command:

DIM array(size)

Description:

DIM sets the maximum size of an array. Any array can be dimensioned to have as many elements as memory allows. If an array is not DIMensioned explicitly, then the array will be limited to 10 elements, 0 to 9.

As you will notice, Liberty Basic allows the programmer to begin using arrays starting with the 0th element. Most programmers begin with the first element, and this is perfectly OK with Liberty Basic. When you DIMension an array for 20 elements you are actually creating an array with 21 elements, numbered from 0 to 20. This gives the programmer flexibility in how they wish to use their arrays, whether they are zero based in your program, or one based such as we have been working with our arrays. This zero or one based arrays is a technicality that I simply would not worry about.

Getting back to the Program -

I know the discussion on arrays can leave your head spinning. It is potentially a confusing subject, but once grasped it is very easy to use and offers incredible power and the ability to simplify a programs operation many times over. Lets consider our Magic 6 ball program. It is an ideal candidate for simplification using arrays.

The first step is to DIMension the array at the to of your code. Here is the first section of the program with some new program line which DIMension the array we will use for answers.

'The Magic 6 ball - version 2
'Written by Joshua Moore and Brad Moore'
'Copyright 2002, all rights reserved
'written for LB3.x
'You are free to incorporate any portion of this code
'into your own programs without notification or credit
'to the authors.  Please do not distribute as-is.

'DIMension a string array to hold the answers
DIM answer$(6)

Next we will need to assign the values to each of the array members. These are the responses that the Magic 6 ball gives in reply to the question. They are assigned one at a time just like any other variable might be assigned:

'Now assign the text strings to each array member
answer$(1) = "MAYBE..."
answer$(2) = "NO! - Never - Don't ask again!"
answer$(3) = "Its not clear now."
answer$(4) = "Yes"
answer$(5) = "Probably not..."
answer$(6) = "You can count on it!"

The next bit of code is unchanged from the previous version to this one. I will post it again for clarity:

'Start the interaction with the user
print "Hello - Ready to play Magic 6 Ball."

[getPlayer]
print "Enter your name please";
input name$
print ""

if name$ = "" then 
  print "You did not enter anything for your name!"
  print "Try agian...";
  goto [getPlayer]
end if

[instructions]
Print ""
print "I am the Magic 6 ball - I can tell the future..."
print "Ask me a yes or no question and I will tell the answer!"
print ""

[loop]
print name$; " What do you wish to ask the Magic 6 ball,"
print "(please make it a 'yes' or 'no' question)";
input question$
print ""

if question$ = "" then 
  print "I am sorry, I did not hear you."
  print "  Perhaps you should type a little louder.";
  goto [loop]
end if

'get a random number between 1 and 6
number = int(rnd(1)*6) + 1

Now comes the real magic in the Magic 6 ball program. We have the random value from the RND statement above, and that value is between 1 and 6 (inclusive). We can use the variable that holds that number to simply print the specific response that goes with that number. There is no need to evaluate anything. All the work is done already in assigning our responses to the answer$() array. The variable number points us to a specific element in the array and we print it. Here is the code:

'now simply print the answer
print answer$(number)

That is all there is to it. Response printed! The rest of the program remains the same:

[playagain]
  'ask if they want to play again
  print ""
  print "Do you want to play again - (y = yes)";
  input answer$
  if answer$ = "y" or answer$ = "Y" then
     cls
     goto [loop]
  end if

print "Thanks for playing"

This version of the program is listed in its entirety in Appendix B. Surely you can tell by looking over the program that it would be very easy to add more responses to the program. Only three changes are needed.

First you would change the size of the array that is being DIMensioned - say from six to eight. Next you would add the two extra elements and assign them their own value for a response. Finally you would need to change the random function, changing the six to an eight (or what ever number of responses you have chosen). That is all. Run the new program and see what happens. If you have not made these changes, I recommend you try. It is by trial and error that you really learn!

I am not going to cover it here, but another enhancement that would fit well into this program (especially if you have many, many responses) is the use of the DATA and READ statements. If you are curious, look them both up in the Liberty Basic Help system and read up on them.