index-bas.htm wywtk.com homepage What You Want To Know  Index to my pages about BBC BASIC

Arrays- A very important type of variable

(Page's URL: bas6-introduction-to-arrays.htm)

Don't be alarmed by the "huge" program further down the page! You can do this! (Especially if you've done the earlier tutorials in this series!)

The program isn't too bad.

But if you haven't done the short First Steps with Arrays yet, I suggest you do that now.

But you'll need your wits about you to grasp the important subject that this tutorial introduces...

Arrays

Arrays are a type of variable.

I hope you are already comfortable with....

x%=5
x%=x%+3
PRINT x%

The first line above creates a simple variable called x%

The second line is read "x% becomes what was in x% previously (5) plus 3... in other words, x% will hold 8 after the second line, and so...

... the last line puts an 8 on the screen.

---
Minor digression... Programmers usually count things from zero... If counting chickens, a programmer would say...

My first chicken is chicken "number zero"
My second chicken is chicken "number one"
My third chicken is chicken "number two"

Weird, I know, but there is a reason, but I'm going to skip explaining it. You'll probably see the benefits as you become a more experienced programmer. For now, Just Do It.

---
Returning to arrays...

Suppose you were writing a program to help a teacher keep track of how many good deeds pupils in his/ her class had done.

To get things started, you might create variables...

Alice%=0
Bob%=0
Charlie%=0

... and so on. (=0 each time because we're just getting started.

Better would be...

Pupil0%=0
Pupil1%=0
Pupil2%=0
(etc)

That would be the best job with what you've been shown in these tutorials so far.

But today we start on arrays. For a class with 25 pupils, anyone who knew about arrays would do...

DIM Pupil%(24)

Pupil%(0)=0
Pupil%(1)=0
Pupil%(1)=0
(etc)

So far, so good?

Before you can use the Pupil%(0) variable, you have to prepare the way with the DIM statement. I said the class had 25 pupils in it, so DIM(24) was right... it creates Pupil%(1) Pupil%(2)... up to Pupil%(24). But there's also the first pupil: Pupil%(0).

So far so good (I hope... re-read the earlier part, if not)... but what's the point? What was wrong with Pupil0%, Pupil1%, etc?

There was nothing "wrong" with that... except that you couldn't do...

DIM Pupil%(4)
Index%=0
REPEAT
   Pupil%(Index)=0
   Index%=Index%+1
UNTIL Index%=5

The bit of code above creates the array (with the DIM line), and then puts 0 in every element of the array. (We call, say, Pupil%(2), one of the elements of the array.)

Oh! A bit odd. And you may not (yet) see what use it would be, but I hope the idea is taking hold in your head.

An example

Because you are an expert on drawing squares, we're going to use that to give you an example of using an array.

A the start of the program, we are going define three squares. We won't draw them yet, but we will pick (with RND) where the square should go (we'll pick x and y coordinates for it) and we will pick a number for how long each of the three squares sides should be. We will store those numbers as follows...

The first square... "square zero", remember, will use...
         x%(0), y%(0) and size%(0)
The second square's defining numbers will be in......
         x%(1), y%(1) and size%(1)
And, t last square's numbers will be in...
         x%(2), y%(2) and size%(2)

Don't try putting this in your BBC BASIC IDE yet, but the part of the program which will do all of that will come at the top, and look like the following. Don't worry about the complicated bits with RND in them after the equals signs... they just pick numbers which will make nice squares. Good places. Good sizes.

DIM x%(2),y%(2),side%(2)

ArrayElement%=0

REPEAT
  x%(ArrayElement%)=10+RND(25)*20
  y%(ArrayElement%)=800+RND(25)*20
  side%(ArrayElement%)=30+RND(18)*20

  ArrayElement%=ArrayElement%+1

UNTIL ArrayElement%=3

We'll use the same PROC_DoSquare that we've used in the past.

We'll use a new subroutine called PROC_UpdateValues.

The job of PROC_UpdateValues? It's job will be to put numbers in the (simple) variables called x%, y% and size%. The three variable that PROC_DoSquare looks at to see where draw a square and how big to make it.

Before we call PROC_UpdateValues, we will put a number in an ordinary variable called BaseIndexForUpdate%. If we put, say, 2 in BaseIndexForUpdate%, PROC_UpdateValues will do...

x%=x%(2)
y%=y%(2)
size%=size%(2)

This really is very cool, even if that's not entirely clear to you yet.

If, on a different occasion, BaseIndexForUpdate% is holding 0, then the three simple variables would be filled from the three array elements at index 0 in the arrays.

BUT WHY????

That's all well and good, but WHY have we built those things?

With them, we can build a program which draws three triangles over and over... but also erases an old one every time it draws a new one!

The triangles are drawn on a black background, remember. As before, we will "erase" a triangle by drawing it in black ink.

In every pass through the program's main REPEAT...UNTIL loop, one triangle is drawn, one is erased.

If you are really alert, you will notice that until we've been through the loop two times, the "erase an old one" part doesn't yet have a old instance of the square it is erasing. But that doesn't matter. The "extra" triangle in black does no harm, and it makes the program simpler if we just shrug and say it doesn't matter.

You may want to change the numbers in the WAIT statements, to slow things down so you can think about each stage as it happens.

As the program runs, BaseIndex% just goes up, up, up... 0,1,2...

But just before the UNTIL, we not only do the "make it one bigger" part ( BaseIndex%=BaseIndex%+1), but we also do...

  IF BaseIndex%>2 THEN BaseIndex%=0

... and so every time it gets to 2, it goes back to 0 just before the next pass through the loop.

BaseIndexForUpdate% changes in a more complicated way.

  BaseIndexForUpdate%=BaseIndex%+2
  IF BaseIndexForUpdate%>2 THEN BaseIndexForUpdate%=BaseIndexForUpdate%-3

In working with code, you will have to become skilled at working out things like the following analysis of the code above.

In the code above, if BaseIndex% is zero when we come to this code, by the end of it, BaseIndexForUpdate% will be 2.

(This bit of code is working out the index to use when doing the "erase a square" step. We've just drawn a square using x%(0), y%(0) and size%(0) if BaseIndex% is zero at the moment.)

However...

If BaseIndex%=1 when we come to the code above,
  then BaseIndexForUpdate% will be 0 when we leave, and
If BaseIndex%=2 when we come to the code above,
  then BaseIndexForUpdate% will be 1 when we leave

All of that is a bit tricky! And not, happily, the main point of this tutorial. So. Try to grasp it, but don't worry to much about it.

The main point is to give you a first taste of the special variables called arrays. And I hope you will be amused by what the program when you copy/paste everything below into your BBC BASIC and run it!

The whole program... Enjoy!

REM bas6-into-to-arrays-1st-prgm.bbc
REM vers 24 Feb 25, 13:10

REM PLEASE do not imagine that I think this is
REM  the best way to write this program. It is
REM  written as it is to fit in with where
REM  learners have got to if they are following
REM  the other tutorials for BBC BASIS for SDL at...
REM https://wywtk.com/prgm/bas/index-bas.htm
REM (This is one of the tutorials in that series.)


MODE 12
PRINT "One moment please.."
DIM x%(2),y%(2),side%(2)

SquaresToDoStill%=24

REM- don't worry about why the RND statements
REM     are so complex.

REM Fill three arrays- x%(), y%() and side%()
REM Each array has three elements,
REM   E.g... there is an x%(0), and x%(1) and an x%(2)

ArrayElement%=0
REPEAT
  x%(ArrayElement%)=10+RND(25)*20
  y%(ArrayElement%)=800+RND(25)*20
  side%(ArrayElement%)=30+RND(18)*20
  ArrayElement%=ArrayElement%+1
UNTIL ArrayElement%=3

REM (end of fill three arrays)

REM Now start drawing and erasing squares...

BaseIndex%=0
REPEAT
  REM Prepare to draw a square...
  BaseIndexForUpdate%=BaseIndex%

  REM Fill x%, y%, side%...
  PROC_UpdateValues

  REM Draw it in a color other than black. (We are drawing on
  REM black "paper")

  REM Choose a color from 1 to 15 (inclusive)... but NOT
  REM   8 (a hard to see dark gray). 0 would draw the square
  REM   in Black, so we don't choose that either.
  REPEAT
    GrColor%=RND(14)+1
  UNTIL GrColor%<>8

  GCOL GrColor%
  PROC_DoSquare
  WAIT (30)

  BaseIndexForUpdate%=BaseIndex%+2
  IF BaseIndexForUpdate%>2 THEN BaseIndexForUpdate%=BaseIndexForUpdate%-3
  PROC_UpdateValues

  GCOL 0
  PROC_DoSquare
  WAIT(20)

  REM get ready for next pass through loop
  BaseIndex%=BaseIndex%+1
  IF BaseIndex%>2 THEN BaseIndex%=0

  SquaresToDoStill%=SquaresToDoStill%-1
UNTIL SquaresToDoStill%=0
PRINT "Pretty? Goodbye."
WAIT(200)
QUIT

END

REM Subroutine definition....

DEF PROC_DoSquare
MOVE x%,            y%
DRAW x%+side%,y%
DRAW x%+side%,y%+side%
DRAW x%,           y%+side%
DRAW x%,           y%
ENDPROC

DEF PROC_UpdateValues
x%=x%(BaseIndexForUpdate%)
y%=y%(BaseIndexForUpdate%)
side%=side%(BaseIndexForUpdate%)
ENDPROC

Next time...

We'll do a MUCH simpler (but quite boring) program using arrays.

A few words from the sponsors...

Please get in touch if you discover flaws in this page. Please mention the page's URL. (wywtk.com/prgm/bas/bas6-introduction-to-arrays.htm).

If you found this of interest, please mention in forums, give it a Facebook "like", Google "Plus", or whatever. If you want more of this stuff, help!? There's not much point in me writing these things, if no one feels they are of any use.



index sitemap
What's New at the Site Advanced search
Search tool (free) provided by FreeFind... whom I've used since 2002. Happy with it, obviously!

Unlike the clever Google search engine, this one 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"....

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

My SheepdogSoftware.co.uk site, where you'll find my main homepage. It has links for other areas, such as education, programming, investing.

My SheepdogGuides.com site.

My SkyWoof.com site.

My site at Arunet.




How to email or write this page's editor, Tom Boyd. Please cite page's URL if you write.


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

AND tested for... Valid CSS?



(eXTReMe tracker icon) Why is there a script or hidden graphic on this page? I have my web-traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Neither my webpages nor my programs incorporate spyware, but if the page has Google ads, they also involve scripts. Why do I mention the script? Be sure you know all you need to about spyware.

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