Title: Programming With Arrays
1Arrays are Awesome!
2Questions
- A storage location in RAM
- A name of a storage location
- An identifier for our data
- . . .
3Questions
- How does the data get input?
4Ways to enter data into RAM
If we want to store 5 in X
5
We can assign X to represent 5.
LET X 5 or X 5
5Ways to enter data into RAM
If we want to store 5 in X And we have this line
in our program DATA 5
5
READ X
6Ways to enter data into RAM
If we want to store 5 in X
5
INPUT Type a 5 , X
7Questions
How many values can be stored in one location at
one time?
X
8How many values can be stored in X at once?
x
9Just one number at a time
x
5
10So whats the problem?
Lots of stuff for this address!
11Heres the problem
It doesnt work!
12- PROBLEM STATEMENT
- A program is needed that will read in the number
of hours different people watched TV last week.
The program should calculate the average viewing
time and display a report showing each person,
each persons hours, and the difference from the
average viewing time.
13- Name Hours Difference
- P. Busch 1 -3
- J. Drake 5 1
- T. Zekly 11 7
- M. Blas 0 -4
- C. Carstens 4 0
- D. Zongas 3 -1
- Average 4 hours of TV viewing
14- Name Hours
- P. Busch 1
- J. Drake 5
- T. Zekly 11
- M. Blas 0
- C. Carstens 4
- D. Zongas 3
- Average 4 hours of TV viewing
15Problems
- We cant know the average until we exit the loop!
- The TotalHours and Count variables
- arent finished accumulating until then.
16Problems
- We cant know the difference from the
averageEVER! - The Hours for each viewer are lost by the time
the loop ends ( we can finally calculate the
average).
17Problems
- Only one value can be stored by a variable at one
time! - Each READ statement gets new values for the
variables old ones are destroyed.
18Solutions?
- Have separate variables for each persons
hoursdont use a loop. - Double up on the DATA lines. READ through once
to calculate the average. READ through again to
PRINT the report. - ?
19How depressing
20An easier way!!
Arrays are Awesome!
21Arrays are
- Data structures that store multiple items!
- Just like lists!
- Searchable!
- Sortable!
? ? Is that a word?
- Variables with subscripts!
- Awesome!
22Variables vs. Arrays
Groceries
Milk
Bread
Eggs
Butter
Jelly
EOF
23Variables vs. Arrays
Groceries
Groceries( )
Food
24Arrays can store multiple items!
Hours( )
Name( )
25Arrays are lists
A number goes here
Groceries( )
26DEFINITIONS
- Array A set of related data items, with the same
variable name, that can be stored in RAM at the
same time - Element One of the individual items in an array
- Subscript A number (in parentheses) designating
an array element
27Use DIM to Create an Array
- The computer needs to know the address (array
variable name) and information about the elements
(1st and last numbers). - DIMension arrays at the beginning of the program.
- DIM Groceries(1 TO 5)
- DIM Name(1 TO 6),Hours(1 TO 6)
-
28Use any input to Fill an Array
- LET Groceries(1)Milk
- LET Groceries(2)Eggs
etc.
29READ is a common way
- To fill an array you would use a loop to fill
the different elements with different values.
FOR X 1 TO N N is how many READ
Name(X) NEXT X DATA Al DATA Bob
DATA Al, Bob works to fill a single array!
30Or Input with INPUT
- FOR X 1 TO N
- INPUT Type a ,Num(X)
- NEXT X
-
31Parallel arrays are easy!
- N 6
- DIM City(1 TO N), Team(1 TO N)
- FOR X 1 TO N
- INPUT City , City(X)
- INPUT Team name ,Team(X)
- NEXT X
-
32W O W ! ! !
Team( )
City( )
33PRINTing data in an array
- Just put the PRINT statement inside a loop and
remember to
use the subscript!
FOR X 1 TO N PRINT Name(X) NEXT X
34Array Program Algorithm
35Create the Array
Groceries( )
DIM Groceries(1 TO 5)
36Create the Array
Better way N 5 DIM Groceries(1 TO N)
37Fill the Array
Groceries(1) Milk INPUT Groceries(1) READ
Groceries(1)
38Fill the Array
with READ or INPUT in a loop!
FOR X 1 TO N READ Groceries(X) NEXT X
39Print the Array
with PRINT in a separate loop!
FOR X 1 TO N PRINT Groceries(X) NEXT X
FOR X N TO 1 STEP -1 PRINT
Groceries(X) NEXT X
40Sample Program (SPORT1.BAS)
This program stores 5 sports CLS Create array N
5 DIM Sport(1 TO N)
41Sample Program (SPORT1.BAS)
Fill array FOR X 1 TO N INPUT Type a sport
,Sport(X) NEXT X
42Sample Program (SPORT1.BAS)
Print array FOR X 1 TO N PRINT
Sport(X) NEXT X END
43Now were ready for some
Awesome Array
P R O G R A M S !