Stumbling Through a Maze of Arrays

by Welopez

"A raise! A raise! My kingdom for a raise!"

"Most sorry, King Welopez, but you are highly overpaid as it is!"

I know that voice... it's that smarmy Artie Abacii, the Worble in charge of my math co-processor.

"The rebel moon is in sight!" (Wheeze...wheeze.) "Prepare the A-Rays to destroy the rebel base!"

"Most sorry, Lord Vader, the Worbles running the targeting computer have gone on strike until you designate a holiday as Inter-Galactic Worble Appreciation Day!"

"DIM thisArray(10)" I said in desperation.

"Ahhh, now you're speaking my language, Welopez," that pesky Worble replied. "And what would you like to do with thisArray()?"

"How should I know, Artie? I just read that in the help file. I don't yet understand what to do with an array. What the heck is it?"

"A simple definition would be to call an array a collection of values belonging to a group, Welopez. You might think of an array as a list of values. Type this code into your IDE:"


      radius(1)=2
      radius(2)=3.5
      radius(3)=6
      radius(4)=7
      radius(5)=10

      FOR i=1 TO 5
      PRINT radius(i)
      NEXT i
      END

"Okay, it prints out all the values we entered, Artie, but where's the DIM statement? I thought we had to dimension all arrays before we could use them? We haven't even declared an array in this code." Like most Worbles, Artie assumes that everyone understands computer code, even mere humans.

"It's good practice to dimension an array anytime you need to use one, Welopez, but Just Basic will do that for you in case you forget. By default, any array created by the program will hold only 11 elements, zero through ten, or items of your group. If you have a large list of items, you must issue a DIM statement or you'll encounter an "Out of bounds..." error at run time."

"So if this group of radius values has 25 elements, I just type DIM radius(25)?" I asked.

"Isn't that easy, Welopez? Actually, you could type DIM radius(24), because radius(0) would point to the first value in your array. Many coders however, prefer to work from item one to the end of the array. Many flavors of Basic allow the user to declare an OPTION BASE for the index of an array, in which case you could make radius(1) the first address in your array, but we don't have this option in Just Basic. To avoid confusion, you can DIM this array as radius(25) if you choose."

"Okay, we have an array now. What can I do with it?"

"In the example above, you have the radius of 5 circles. Who knows where they came from? Perhaps you entered the data manually, or perhaps you got it from the inventory of the Acme Circle Manufacturing Company. Assume you're the purchasing agent for Acme, and you have to order wire to construct frames for your circles. How much wire will you need? Modify your code to look like this:"


      radius(1)=2
      radius(2)=3.5
      radius(3)=6
      radius(4)=7
      radius(5)=10
      PRINT "Radius", "Diameter", "Circumference (wire needed)"
      FOR i=1 TO 5
      PRINT radius(i), radius(i)*2, USING("###.##",(radius(i)*2)*3.14159)
      total=total+(radius(i)*2)*3.14159)
      NEXT i
      PRINT "The total amount of wire needed is "; USING("###.##",total)
      END

"Hey, that's neat, Artie. The diameter and circumference of all the circles to be manufactured are neatly calculated and printed out. But do I have to purchase 179 inches of wire, or feet, or miles?"

"Do I have to do everything for you, Welopez? If your radius is in inches, your circumference will be in inches, and the total will be in inches, unless you choose to convert the total to feet and add 5 or 10 percent for waste. Can you do that?"

"Oh, yeah, well... sure! What else can I do with arrays?"

"Arrays are almost universally used for sorting routines," Artie the Snot told me. "It's a simple matter for my Worble staff to look at the values for each element of an array and create an ordered list of elements. See the tutorial at http://justbasic.conforums.com/index.cgi?board=tutorial&action=display&num=1120928790 for an introduction to sorting routines."

"Simple for you, Artie, but I'm just learning this basic-speak stuff. Can I make an array that will hold all my email contacts?"

"Sheesh!" the snot declared, "didn't you READ that tutorial, Welopez? We created just such a file for you, and used a multi-dimensioned array."

"I thought multi-dimensions only existed in science-fiction, Artie?"

"Oh, ye of little faith, Welopez. Trust me, I would not steer you wrong on this. When we speak of multi-dimensions in an array, think of an array like a filing cabinet. The first dimension is the number of drawers you have in the cabinet, the length of the array list in this case, with each drawer representing one of your contacts. Inside each drawer of this filing cabinet are four folders, or fields as they might be called in a data application. In myContacts$() array, the first folder has the contacts surname, the second folder holds the contacts Christian name, the third holds the age, and the fourth holds the telephone number."

"Oh... that's clear, Artie. I just never looked at it that way. But there's one thing I don't understand, why is this array dollars and cents? Why is it named myContacts$()?"

Artie Abacii turned a brilliant shade of crimson and glared at me. "Welopez, if I weren't bald as a cue-ball, I would be tearing my hair out about now. When programming in basic, string variables are identified using the $ symbol. That doesn't mean the variable is money, although it could be, but I'm sure that subject is beyond your abilities at this moment.

"String variables are alphanumeric characters which you humans use to create words. Because words don't mean anything to us Worbles, I tend to think of them as pictures. You humans are barely above the comic book stage anyway. Strings are things like "John" "loves" "Mary." Icky stuff like that, you know? When you put them together you have a sentance. Most documents are simply super-strings, which may contain millions of characters combined to make words and sentances, and may also contain characters used for formatting. Worbles use CHR$(10) to cause a line feed, CHR$(13) creates a carriage return, CHR$(9) represents the horizontal tab, CHR$(44) represents a comma, which may be used as a field delimeter in some programs, depending upon how the application was written."

"You're getting over my head, Worble, which is pretty difficult considering I am at least a million times larger than you."

"I'll speak more slowly so you can understand, Welopez. When you have an array of more than one dimension, you must use the DIM statement to define it for the program. In the case of myContacts$(), the array has ln length and fld fields, so your statement would be DIM myContacts$(ln,fld)."

"Got it!" I told him.

"You can fill the array just as we did before, by manually entering all values from the keyboard, providing you remember myContacts$(1,1) is the value for the surname of the first individual, myContacts$(1,2) is the Christian name of the same individual, myContacts$(1,3) is that persons age, and myContacts$(1,4) is the phone number.

"But that's an awful lot of typing, and I know you're going to make a lot of errors, so it's best to read a data file to load your array. We worbles rarely make mistakes, even though you humans are always referring to computer errors when you actually mean operator error."

"So, let me see if I have this straight, Artie: an array is a group of related items, either numerical or alphanumeric. A multi-dimensioned array is a similar group, but each item has additional parameters for item, such as last name, first name, age, and telephone."

Artie applauded with those tiny, two-fingered hands all Worbles have. "Bravo for you, Welopez. You're not as stupid as you look!"

Okay, I did learn somethings from Artie.

  1. An array is a group of related objects.
  2. Basic automatically provides for arrays up to 11 items, including the 0 element, but I have to create larger arrays by declaring the array with a DIM statement.
  3. Numeric arrays require no identification, but string arrays must be identified with the $ symbol.
  4. Arrays can hold mixed data, both numeric or string, but such an array must be declared as a string array with the $ symbol.
  5. A multi-dimensioned array simply provides more description for a single item. The item could be Mary, who has blue eyes, is 155 centimeters tall, and weighs 47 kilograms. (We'll leave out Mary's measurements because this is a PG rated forum) The color eyes, height, and weight are all attributes of Mary and are associated with Mary's array element.
  6. An array, and any dimensions it contains, is a convenient construction to contain multiple variables and address them simply by the array number (the file drawer) and folder number (additional attributes) without creating a variable name for every single value used by the program.

Hmmm, maybe if I read some of these help files and tutorials, I won't have to depend on those snobbish Worbles as much? It's worth a shot!