Arrays

advertisement
Arrays
Here at RFHall each student is assigned a LOCKER. Each LOCKER has a number. The lockers are by definition an array.
In VB you can declare an array of variables in the following DIM statenments
DIM LOCKER(20) AS INTEGER ‘Create 21 locations in the computer’s memory to store INTEGERS.
0
1
2
3
4
5
6
7
8
9
0
11
12
13
14
15
16
17
18
19
20
-Index value
LOCKER(5) = 222 ‘VB code to store the values 222 into LOCKER(5)
1
2
3
4
5
6
7
8
9
0
11
12
13
14
15
16
17
18
19
20
222
0
2
3
4
5
6
7
THESUM = 0
For a = 0 to 20
THESUM=THESUM+LOCKER(a)
Next a
Print THESUM
8
9
0
11
12
13
14
15
16
17
18
19
20
234
1
12
0
222
LOCKER(6) = 12
LOCKER(8) = LOCKER(5) + LOCKER(6)
}
RESULTING OUTPUT: 468
One of the first uses of computers was to work with lists of information. The computer itself uses lists in many ways: lists of
programming commands, lists of files in a computer's directories, etc. We have also taken many lists from our daily activities
(phone books, lists of friends, etc.) and "computerized" them to make their use even easier.
Let's say that you wanted to store the names of 15 of your friends. Up to this point you would probably create 15 different
variables, likely naming them Friend1, Friend2, Friend3, etc. An array is a simpler way of doing this, since we could create
one variable, with one name but with 15 'compartments'.
Exercises
1. Create a program that allows a user to enter the heights of 10 members of the basketball team (in cm). Your program
should then display the(a) average,(b) highest and (c)lowest of these heights with appropriate labels.
2. Write a program that simulates the rolling of two, six-sided dice. The program should keep tallies of the frequency that each
of the possible totals is obtained: Display the frequency of 2’s(1+1), 3’s (1+2), 4’s(2+2)….up to 12’s(6+6)
3. Create a program that will generate 20 random numbers between 1 and 200. Save each number into an Array. Check the array to see
if the computer created any duplicate numbers. If there is a duplicate replace it with a new random number until the array is full of 20
unique numbers
4. Design a mini-language translator. Store 15 commonly used words into an 'english' array and their french equivalents into
a second array. Allow the user to select or type in one and then see the equivalent in the other language.
5. Create an address book program. It should allow a user to enter a maximum of 5 friends' names, phone numbers, and
addresses. Each set of information should be stored in their own array. Give the user the option to sort the information based
on either the names, phone numbers, or addresses, making sure the corresponding information in the other two arrays get
changed accordingly.
Download