5.2 Creating a Simple Array Description

advertisement
5.2 Creating a Simple Array
Description
Related data values can be grouped together and isolated from other data values in a program in
several different ways. One way is to make an array of values of the same type. This array of
values has a single name, and an individual value or element of the array is selected by giving an
index value that uniquely identifies it. One of the most natural ways to do this is to give each
element a number; then you can say, "Get me the third element of the array," or, "Store this in
element 12."
Syntax
Arrays are declared in a
similar manner to other
variables, using the Dim,
Static, and Global
statements. You include the
size or range of the array
(how many elements or
compartments) in
parentheses following the name.
The following, placed in the Declarations section of a form or module, would declare an array or
list of 13 values with the name MonthSales:
Dim MonthSales(12) As Currency
Note the 12 in parentheses results in an array with 13 elements since the elements of the array
would be addressed as MonthSales(0), MonthSales(1), MonthSales(2) and so on up to
MonthSales(12). Specifying the type As Currency is not required but is recommended.
You may also specify a specific range of values for an array's index:
Dim SalesInYears (1994 To 1998) As Single
would create an array variable with the first element SalesInYears(1994), the second element as
SalesInYears(1995) and continuing in the same way until the last element, SalesInYears(1998).
Each value in the array is addressed by the name of the array and the index number of the
value:
MonthSales (5) = 654.32
would store the value 654.32 in the fifth element of the array.
Activity 5.2.1 - Using Arrays
Values inside arrays are most often assigned or retrieved by using a For-Next Loop. To
demonstrate this further, we will use a famous mathematical sequence:
Fibonacci numbers are those that are obtained by adding the two numbers previous to it. For
example, if the first number of a sequence is 1 and the second number is 1 (to keep things
simple!) the third number would be 1 + 1 = 2, the fourth number would be the second and third
added together, 1 + 2 = 3, the fifth would be 2 + 3 = 5, the sixth 3 + 5 = 8 and so on. These
numbers are said to have "magical" properties. One such property is that for any two starting
numbers (as we did above with 1 and 1) the sum of the first 10 of these Fibonacci numbers will
equal eleven multiplied by the seventh value in the sequence. Let's look at an example with the
first number being 9, the second being 4:
1st - 9
2nd - 4
3rd - 13
4th - 17
5th - 30
6th - 47
7th - 77
8th - 124
9th - 201
10th - 325
sum of the first 10 numbers:
9 + 4 + 13 + ... + 325 = 847
eleven times the 7th number:
11 x 77 = 847 We are now going to create an example Visual Basic program that will check this
"magical" property:
Designing the Form
1. Open Visual Basic. If Visual Basic is already open, select New Project from the File menu.
2. Change the caption of the default form to Fibonacci Numbers.
3. Add the following controls and change the properties as given:
Object
Label
Label
TextBox
Property
Setting
Alignment
2 - Center
Caption
First Value
Name
lblFirstVal
Alignment
2 - Center
Caption
Second Value
Name
lblSecondVal
Name
txtFirstVal
Text
TextBox
Name
txtSecondVal
Text
ListBox
Name
lstNumbers
Label
Alignment
2 - Center
Caption
Sum of Numbers
Name
lblSum
Alignment
2 - Center
Caption
11 x Seventh Number
Name
lblProduct
Name
txtSum
Label
TextBox
Text
TextBox
Name
txtProduct
Text
CommandButton
CommandButton
CommandButton
Caption
Calculate
Name
cmdCalculate
Caption
Clear
Name
cmdClear
Caption
Quit
Name
cmdQuit
Note: A list box is similar to a text box, but it displays lists of data, and allows the user to select
items from the list. For more information, see List Box Control in the on-line Help.
Your form should resemble:
4. Add the coding for the program in the appropriate area:
The code is contained within the three command buttons:
Private Sub cmdCalculate_Click()
Dim FibNums(1 To 10) As Integer ' array for the Fibonacci sequence
Dim Nums As Integer ' index for the For Loop
Dim Sum As Integer ' variable to sum values
Dim Product As Integer ' variable to hold product of 11 and seventh value
FibNums(1) = Val(txtFirstVal.Text) ' get the value of the first term in the sequence
lstNumbers.AddItem Str(FibNums(1)) ' use AddItem to put first number in the list box
FibNums(2) = Val(txtSecondVal.Text) ' get the value of the second term
lstNumbers.AddItem Str(FibNums(2)) ' put second number in the list box
Sum = FibNums(1) + FibNums(2) ' add the first two terms to the sum
For Nums = 3 To 10 ' calculate remaining Fibonacci numbers by adding previous two
FibNums(Nums) = FibNums(Nums - 2) + FibNums(Nums - 1)
lstNumbers.AddItem Str(FibNums(Nums)) ' add to list box
Sum = Sum + FibNums(Nums) ' add to running total of all numbers
Next Nums
txtSum = Sum ' put sum of all numbers in text box
txtProduct = FibNums(7) * 11 ' put 11 x the seventh value in text box
End Sub
Private Sub cmdClear_Click()
txtFirstVal.Text = "" ' clear all text boxes
txtSecondVal.Text = ""
txtSum = ""
txtProduct = ""
lstNumbers.Clear ' Clear removes all items from the list box
End Sub
Private Sub cmdQuit_Click()
End ' quit the program
End Sub
5. Run the program to ensure that it works. Test with the numbers 9 and 4. Does the same thing
happen if both numbers are negative? If one is negative and the other positive?
Now, using the above example as a guideline to working with arrays, create your own
program that will simulate drawing 6 random numbers for a weekly lottery. The numbers
drawn must be unique (that is, they may only be drawn once a week), and should be in the
range from 1 to 49.
Hint: To make sure a number is drawn only once, create an array of 49 boolean (true or
false) values and once a number has been drawn, change that number's position in the
array!
Download