8. Arrays Part 2

Arrays are often used in programming so it's important to fully understand how they work.  In the last post I mentioned a one dimensional array for holding Fender guitar serial numbers.  Owen, a prodigious FreeBASIC programmer and FreeBASIC regular, has provided the following code for the 1 dimensional array example.

dim as integer i

dim as integer fender(3)




for i = 1 to 3

    print "fender guitar serial #";i;" is worth $";fender(i)



Let's see what is happening.  The first line sets up an integer variable, i,  and the next line sets up an integer array variable that can hold three items, fender(3).  The next three lines assign values to each of our array locations.  This is then followed by a for next loop, we've seen how this works in a previous post, that prints out the contents of each array variable one line at a time.  The sleep command pauses the program so we can see the output.  If we don't sleep the world flashes before our eyes before we an make any sense of what is happening.

The output follows:

The second example I mentioned  was about a typical suburban street with a row of houses, each with a numbered letter box. Again Owen has provided some example code to demonstrate the 2 dimensional array.

dim as integer x,y

dim as string house(2,4) ' first name, last name, box number, street name




house(1,4)="Elm Ave"




house(2,4)="Oak Street"

for x=1 to 2

     print house(x,1)

     for y=1 to 4

          print house(x,y);" ";





Let's work through this code.  The first line creates two integer variables, x and y.  Note how both variables are created on the same line.  This line is followed by 
dim as string house(2,4) ' first name, last name, box number, street name
In this line we create a string array that is 2 by 4 in size. Items after an apostrophe are not used by the FreeBASIC interpreter but are valuable comments for us to understand the program.  So first name, last name, and so on remind us about the structure of the house(2,4) array.

The next eight lines of code that  look like:
load the array. You can see that the first dimension of the array defines the house, while the second dimension hold a specific attribute of the house, such as the owners first name, their last name, the box number, and the street name.

Finally we have the nested for next loops that work their way, by using the x and y variables, through each house and attributes.

The output follows:

Variables are commonly used in programming and it is worth the trouble of trying a few examples for yourself to really understand how they work.

A big thanks to Owen for his code and his feedback on the FreeBASIC forum:   If you haven't had a look, please go ahead, you will find very knowledgeable programmers on the site who are very generous with their support and feedback.

If you want an example of what can be done with FreeBASIC, you need only go to Owen's site, to see an impressive graphical program written entirely in FreeBASIC!

As always, please feel free to leave a comment and see you in the next post.

No comments:

Post a Comment