Arrays
Many programs deal with
large amounts of data.
For example, a program that works with
images might deal with millions of numbers.
If each number were kept in
its own variable,
there would be millions of variables,
each with its own name.
This would be awkward.
QBasic
can use an array to
store a large amount of data,
avoiding the need for so many variables.
Chapter Topics:
- Collections of things.
- Arrays.
- Subscripts.
- The
LETstatment and arrays. - Variables as subscripts.
- Expressions as subscripts.
QUESTION:
Imagine that you had a collection of 500 hats, and that you wanted to name each hat. What scheme could you use to name your hats?If you own just a few hats, individual names might work, but if you own many hats it is much easier to number them. The first hat is numbered "1," the second hat is numbered "2," and so on up to "500."
Now you could ask for "hat 32" or maybe send "hat 84" out for cleaning. If you add new hats to your collection, you can assign each one a new number in sequence. By using numbers to designate hats, you have a never-ending supply of hat names.
Collections of things often have acquisition numbers like these so that the items of the collection can easily be identified.
QUESTION:
Now imagine that you also have a collection of 100 ties. Can you give each tie its own number?You feel like stepping out for the night, and look at your collection. You decide to wear
hat 73 tie 19No problem.
There are two things that are needed to ask for an item:
- The name of the collection ("hat" or "tie").
- The item number.
QUESTION:
Say (as before) that you have 500 hats. Can you ask for hat number 577?In programming, when you have a collection of similar items, each with an item number, the collection is called an array, and the item number is called a subscript.
To refer to a specific item in an array do this:
arrayName( subscript )The name of the array is followed by the item number (the subscript) inside of parentheses.
For example, say that
DAY$ is an array of
strings—one for each day of the week.
Then,
LET DAY$(1) = "Monday"makes "Monday" the first string of the array:
DAY$is the name of the array (the collection of items).- the
(1)selects item number 1.
QUESTION:
What (do you suppose) the following does?LET DAY$(1) = "Monday" PRINT "The day is", DAY$(1)(Don't run this program yet. It needs more work.)
Tell QBasic how many items are in your array like this:
DIM arrayName( 1 TO size )
DIM asks for an array.
The arrayName is the name of the array.
It will have subscripts 1, 2, 3, 4, ... up to size.
Here is how to ask for an array named
DAY$
that holds strings numbered from one to seven:
DIM DAY$( 1 TO 7 )
QUESTION:
IfDAY$ is as above,
is the following statement correct?
DAY$(8) = "Memorial Day"
Here is a program that uses an array:
DIM DAY$(1 TO 7) ' Make an array of seven strings. LET DAY$(1) = "Monday" ' Make "Monday" the first string. PRINT "The day is", DAY$(1) ' Print out the first string. END The program is complete. You can enter it into QBasic and run it. It writes out:
The day is Monday When this program begins, it creates an array. The array holds seven items, and each item is a string. Each string starts out as the "empty string"—it contains no characters. Then the string "Monday" is put into the first position. Here is a picture of the array after the first two statements have executed:
|
Monday
|
|
|
|
|
|
|
|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
DAY$(1).
QUESTION:
Fill in the blank so that "Tuesday" is put into the second slot.LET DAY$(_____) = "Tuesday"
Here is how this statement would work as part of a complete program:
DIM DAY$(1 TO 7) ' Make an array of seven strings LET DAY$(1) = "Monday" ' Make "Monday" the first item LET DAY$(2) = "Tuesday" ' Make "Tuesday" the second item PRINT "The day is", DAY$(1) ' Print out the first item. END After the second LET statement, the array looks like this:
|
Monday
|
Tuesday
|
|
|
|
|
|
|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
QUESTION:
Put a statement at the end of the program that will print out the second day.You can change the data in an array. For example, the following program changes the first item of the array:
DIM DAY$(1 TO 7) ' Make an array of seven strings
LET DAY$(1) = "Monday" ' Make "Monday" the first item
PRINT "The day is", DAY$(1) ' Print out the first item.
LET DAY$(1) = "Manic Monday" ' Change the first item to "Manic Monday"
PRINT "The day is", DAY$(1) ' Print out the first item.
END
When the program runs,
it will print out:
The day is Monday The day is Manic Monday
QUESTION 8:
Fill in the blanks of the following program so that the arrayDAY$ is filled with the days of the week, in order:
DIM DAY$(1 TO 7) ' Make an array of seven strings LET DAY$(1) = "Monday" ' Make "Monday" the first item LET DAY$( _____ ) = _________ LET DAY$( _____ ) = _________ LET DAY$( _____ ) = _________ LET DAY$( _____ ) = _________ LET DAY$( _____ ) = _________ LET DAY$( _____ ) = _________ END
Here is the program that fills every slot of the array with the name of a day:
DIM DAY$(1 TO 7) ' Make an array of seven strings LET DAY$( 1 ) = "Monday" ' Make "Monday" the first item LET DAY$( 2 ) = "Tuesday" LET DAY$( 3 ) = "Wednesday" LET DAY$( 4 ) = "Thursday" LET DAY$( 5 ) = "Friday" LET DAY$( 6 ) = "Saturday" LET DAY$( 7 ) = "Sunday" END After all the slots have been filled, here is how the array looks:
|
Monday
|
Tuesday
|
Wednesday
|
Thursday
|
Friday
|
Saturday
|
Sunday
|
|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
QUESTION:
Imagine that the following statements are in the program, just before END. Fill in the blanks so that the days of the week are printed out in order.PRINT "The days are ", DAY$( __ ), DAY$( __ ), DAY$( __ ), DAY$( __ ) PRINT DAY$( __ ), DAY$( __ ), " and " DAY$( __ )
The program does not do anything practical. It is just an example of how arrays work. Here is another not-very-practical program: Say that you have an important date this week, and want the program to print out what day it is on. The following program uses the string variable
HOTDATE$ to hold the name of the important day.
That name will be copied from one of the array slots:
DIM DAY$(1 TO 7) ' Make an array of seven strings LET DAY$(1) = "Monday" ' Make "Monday" the first item LET DAY$(2) = "Tuesday" LET DAY$(3) = "Wednesday" LET DAY$(4) = "Thursday" LET DAY$(5) = "Friday" LET DAY$(6) = "Saturday" LET DAY$(7) = "Sunday" LET HOTDATE$ = DAY$(5) ' Copy the fifth DAY$ into HOTDATE$ PRINT "Important date on:", HOTDATE$ END
QUESTION:
What does the above program write out?An expression such as this
DATE$(5)is called a subscripted variable. The name of the whole array is
DATE$.
The subscript, 5, is used to pick a single slot of the array.
A subscripted variable is the combination
of the array name and a subscript.
You can use a subscripted variable anyplace an ordinary variable can go. For example, in the above program a value was copied from a subscripted variable to an ordinary variable:
LET HOTDATE$ = DATE$(5)
QUESTION:
Your Friday evening hot date has been cancelled. Change the statement so thatHOTDATE$
is changed to "Saturday"
(change only the subscript in the program).
Well, that was easy. To ask for the 6th item in the array, all you do is use subscript 6. What if the 6 is kept in a variable? Inspect the following program:
DIM DAY$(1 TO 7) ' Make an array of seven strings LET DAY$(1) = "Monday" ' Make "Monday" the first item LET DAY$(2) = "Tuesday" LET DAY$(3) = "Wednesday" LET DAY$(4) = "Thursday" LET DAY$(5) = "Friday" LET DAY$(6) = "Saturday" LET DAY$(7) = "Sunday" LET WHICH = 6 LET HOTDATE$ = DAY$(WHICH) PRINT "Important date on:", HOTDATE$ ENDThe program uses correct syntax. The expression
DAY$(WHICH) is a correct subscripted variable.
QUESTION:
What will the above program write out?Remember that when a
LET statement executes,
it goes through several steps:
A LET Statement
- Finds memory for each NEW variable in the statement. (If there are no new variables, this step does nothing.)
- Does the calculation on the RIGHT of the equal sign. (If there is nothing to calculate, it just uses the value that is there.)
- Replaces the contents of the variable to the LEFT of the equal sign with the result of the calculation.
LET WHICH = 6
+------- Step 1. Memory is found for the variable HOTDATE$
|
|
LET HOTDATE$ = DAY$( WHICH )
| |
| |
| +----- Step 2. A value is found for the expression on the RIGHT.
| This now involves getting the value in WHICH,
| then getting the value in DAY$(6),
| the string "Saturday"
|
+------ Step 3. The value is copied into the variable HOTDATE$
This is easy enough with the above statement,
but things can get much more complicated.
QUESTION:
Do things have to get more complicated?Here is an example of things getting a little bit complicated:
DIM DAY$(1 TO 7) ' Make an array of seven strings LET DAY$(1) = "Monday" ' Make "Monday" the first item LET DAY$(2) = "Tuesday" LET DAY$(3) = "Wednesday" LET DAY$(4) = "Thursday" LET DAY$(5) = "Friday" LET DAY$(6) = "Saturday" LET DAY$(7) = "Sunday" LET WHICH = 1 LET HOTDATE$ = DAY$(WHICH+2) PRINT "Important date on:", HOTDATE$ ENDIn this program, the subscript in the last
LET statement is
the result of a calculation.
QUESTION:
What does this program write out?Here are the complicated parts of the program:
LET WHICH = 1 LET HOTDATE$ = DAY$(WHICH+2)You can understand what goes on by going through the steps of a
LET statement:
- First, memory is found for
HOTDATE$. - Second, a value for the right of the "=" is calculated:
WHICHis replaced with its value: 1- The calculation 1+2 is done, resulting in: 3
- The string in
DAY$(3)is found: "Wednesday"
- The value "Wednesday" is copied into
HOTDATE$
Go through these steps a few times. Understanding how this works now will save you effort later on.
