Programming With Arrays - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Programming With Arrays

Description:

Arrays are Awesome! Arrays are. Data structures that store multiple items! Just like lists! ... X. END. Now we're ready for some. Awesome Array. P R O G R A M ... – PowerPoint PPT presentation

Number of Views:80
Avg rating:3.0/5.0
Slides: 44
Provided by: IMT
Category:

less

Transcript and Presenter's Notes

Title: Programming With Arrays


1
Arrays are Awesome!
2
Questions
  • What is a variable?
  • A storage location in RAM
  • A name of a storage location
  • An identifier for our data
  • . . .

3
Questions
  • How does the data get input?

4
Ways 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
5
Ways 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
6
Ways to enter data into RAM
If we want to store 5 in X
5
INPUT Type a 5 , X
7
Questions
How many values can be stored in one location at
one time?
X
8
How many values can be stored in X at once?
x
9
Just one number at a time
x
5
10
So whats the problem?
Lots of stuff for this address!
11
Heres 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

15
Problems
  • We cant know the average until we exit the loop!
  • The TotalHours and Count variables
  • arent finished accumulating until then.

16
Problems
  • 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).

17
Problems
  • 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.

18
Solutions?
  • 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.
  • ?

19
How depressing
20
An easier way!!
Arrays are Awesome!
21
Arrays are
  • Data structures that store multiple items!
  • Just like lists!
  • Searchable!
  • Sortable!

? ? Is that a word?
  • Variables with subscripts!
  • Awesome!

22
Variables vs. Arrays
Groceries
Milk
Bread
Eggs
Butter
Jelly
EOF
23
Variables vs. Arrays
Groceries
Groceries( )
Food
24
Arrays can store multiple items!
Hours( )
Name( )
25
Arrays are lists
A number goes here
Groceries( )
26
DEFINITIONS
  • 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

27
Use 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)

28
Use any input to Fill an Array
  • LET Groceries(1)Milk
  • LET Groceries(2)Eggs

etc.
29
READ 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!
30
Or Input with INPUT
  • FOR X 1 TO N
  • INPUT Type a ,Num(X)
  • NEXT X

31
Parallel 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

32
W O W ! ! !
Team( )
City( )
33
PRINTing 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
34
Array Program Algorithm
35
Create the Array
Groceries( )
DIM Groceries(1 TO 5)
36
Create the Array
Better way N 5 DIM Groceries(1 TO N)
37
Fill the Array
Groceries(1) Milk INPUT Groceries(1) READ
Groceries(1)
38
Fill the Array
with READ or INPUT in a loop!
FOR X 1 TO N READ Groceries(X) NEXT X
39
Print 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
40
Sample Program (SPORT1.BAS)
This program stores 5 sports CLS Create array N
5 DIM Sport(1 TO N)
41
Sample Program (SPORT1.BAS)
Fill array FOR X 1 TO N INPUT Type a sport
,Sport(X) NEXT X
42
Sample Program (SPORT1.BAS)
Print array FOR X 1 TO N PRINT
Sport(X) NEXT X END
43
Now were ready for some
Awesome Array
P R O G R A M S !
Write a Comment
User Comments (0)
About PowerShow.com