Title: Arrays Intro One Dimensional 2 Dimensional
1Data Types Arrays in Programming One
Dimensional Arrays
Illawarra Grammar School
2- When one is coding, variables are eventually
used. - variables are the things that change in value
(varies) in different parts of computer coding.
Eg. counter in a loop.
- Most programming languages usually automatically
create / declare by default 10 stand alone
variables. After, that we have to manually
declare / create the variables at the beginning
of the coding. - It is good practice to declare / create all
variables manually
- Two types of variables are generally used text
numbers. - Examples of number variables include
- number_1, number_2, counter, guess, answer,
location
- Text variables are also known as STRING/STRINGS
- Examples of text variables include
- guess, answer, reply, name, surname
We give variable names that make some sort of
sense within the coding. This make debugging
much easier.
3Stand alone variables are created by using coding
statements like DECLARE reply, name, sname as
STRING DECLARE number_1, number_2, counter as
INTEGER
Stand alone variables quickly become cumbersome
and too difficult to maintain. Imagine that one
had to create a booking system for the school
hall that held 250 seats. It would be very time
consuming to create 250 separate stand alone
variables eg. seat1, seat2, seat3, seat4, seat5,
seat6, seat7 seat250
Of course the gurus have come up with a method(s)
to overcome the short comings of stand alone
variables.
- When a lot of data are related to each other, we
can use something called ARRAYS. - We can quickly put data into arrays and extract
data from arrays.
4Stand alone variable data is put into a memory
box
name
INPUT Enter Name name User types
Matthew Matthew is stored in memory
Matthew
When one DECLAREs /creates a variable, a place in
memory is created a memory box for lack of a
better term.
PRINT name Means locate the data in the memory
box called name and display it to the screen
Display
name
Matthew
Matthew
5Related data can be placed in a one dimensional
array
means the data is text or a string
Week_array
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Data in an array is referred to by using a
co-ordinate system like the cell of a spreadsheet.
Week_array(1) Monday
Week_array(5) Friday
Week_array(2) Tuesday
Week_array(6) Saturday
Week_array(3) Wednesday
Week_array(7) Sunday
Week_array(4) Thursday
6An arrays co-ordinate system consists of two
parts
A variable name (in this case an array)
Week_array(X)
And a number X indicating the items position in
the sequence.
7An array is called a data structure.
Week_array(X) This data structure uses one
variable name to store several consecutive data
items of a similar nature.
- This array Week_array(X) is called all the
following - a Linear array
- a one dimensional array
- a list or sometimes
- a vector
- The number in parenthesis Week_array(6) showing
precisely where each item of data is to be found
in the list is called all the following - a subscript
- an address
- a pointer
- a location or
- an index
8Week_array
means the data is text or a string
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
1
2
3
4
5
6
7
- To ensure that
- there are sufficient memory locations available
and - the memory locations are consecutive
- It is necessary to reserve sufficient number
locations at the start of the computer program.
This is called - DIMENSIONING the array
- In BASIC it is created using the statement DIM
Day(7) - This indicates 7 consecutive memory locations are
needed.
9The Input / Output of Data Items
To place data into an array, the coding may look
something like
DIM Week_array(7) position 1 While position lt
7 Input Enter a day of week
Week_array(position) position position
1 Endwhile
To display data that is in an array, the coding
may look something like
position 1 While position lt 7 Print A
day of the week is Week_array(position)
position position 1 Endwhile
10If we exaggerate a little bit we now can quickly
create an array that will cater for 250 seats in
the hall.
DIM hallseats_array(250) Position 1 FOR
position 1 to 250 INPUT Enter booking Name
hallseats_array(position) NEXT position END
It is hoped one can see this method of using
arrays is much faster as compared to declaring
250 stand alone variables!
To display the booking names Position 1 FOR
position 1 to 250 PRINT hallseats_array(posit
ion) NEXT position END
11Coding task Using QBASIC READ/DATA
statements Create a program that reads 10 names
into an array in sequence and then prints the
contents of the array onto the screen in reverse
order.
12Data Types Programming Two Dimensial Arrays
13Two Dimensional Arrays
Dim Table(5,6)
A table of data can be addressed using two
numbers. The two numbers represent the ROW
position and the COLUMN position
respectively. Table(row,column)
Table(2,1) 2 Table(3,4) 0 Table(1,1)
1 Table(6,5) Undefined
Note that each data item consists of three parts
A variable name
? Table(row, column)
14Since we are thinking in terms of tables, we
sometimes use the term tabular data which fits
into the table cells. Also the row, column
co-ordinates can be referred to as doubly
subscripted variables. In Basic we can reserve
memory for tabular data by using two dimensional
arrays consisting of letter boxes or pigeon holes
arranged in rows and columns. Eg. DIM Table(5,6)
reserves 56 30 consecutive memory locations
consisting of 5 rows and 6 columns.
Array named Table(Row,Column) With R rows and C
columns. The total number of Items is RC
. . .
15A suitable algorithm for storing RC items in an
array named Table(R,C) would be using a pair of
nested counting loops as follows
ENTER Input data into a table EXIT
FOR Row1 TO R NEXT Row
FOR Column 1 to C NEXT Column
INPUT Table(Row,Column)
16To output items stored in a 2 dimensional array
is basically the same with the exception that the
INPUT statement is replaced by an output
statement such as PRINT, DISPLAY, WRITE, OUTPUT
ENTER Output data from a table EXIT
FOR Row1 TO R NEXT Row
FOR Column 1 to C NEXT Column
PRINT Table(Row,Column)
17Summing Rows Columns
The sum of each row and column are shown in an
extra column and extra row.
The first row contains the integers 23, 17, 10,
8 which sum to 58. Their positions within the
array are given by A(1,1), A(1,2), A(1,3),
A(1,4)
For the variable Column taking values 1 through
4 we can rewrite positions as A(1,Column). The
sum of this row can be placed in an extra
location to the right of the table at position
A(1,5)
A sample algorithm which calculates stores this
sum is
ENTER Sum first row with 4 columns FOR Column
1 to 4 A(1,5) A(1,5) A(1,Column)
NEXT Column EXIT
18ENTER Sum first row with 4 columns FOR Column
1 to 4 A(1,5) A(1,5) A(1,Column)
NEXT Column EXIT
58
This works fine for an array with 3 rows 4
columns. To generally sum the first row of an
array with C columns rather than simply 4 columns
the algorithm is altered as follows. Note the
sum is now stored in a column numbered C1 rather
than the 5th (41) column
ENTER Sum first row with C columns FOR Column
1 to C A(1,C1) A(1,C1)
A(1,Column) NEXT Column EXIT
19The algorithm can be further extended to sum all
R rows of the array by placing a second loop,
which varies the row number, around the previous
process as follows
ENTER Sum first row with C columns FOR Row
1 TO R FOR Column 1 to C
A(Row,C1) A(Row,C1) A(Row,Column)
NEXT Column NEXT Row EXIT
58
Row 1
42
Row 2
55
Row 3
20A final refinement sums each individual column
places the answers across the extra row at the
bottom of the table.
ENTER Sum all rows and columns FOR Row 1 TO
R FOR Column 1 to C
A(Row,C1) A(Row,C1) A(Row,Column)
A(R1,Column)A(R1,Column)A(Row,Column)
NEXT Column NEXT Row EXIT
Col1
Col2
Col3
Col4
Row 1
23
40
50
58
10
23
Row 2
4
42
12
13
31
55
Row 3
23
27
17
23
10
23
8
27
24
41
51
49
Note the 4 columns are dealt with first before
going to the next row.
21Bibliography
Software Design Development HSC Course C.
Wilson Software Design Development HSC
Course S. Davis Software Design Development
HSC Course A. Fowler Concepts Exercises in
Computer Studies Unit 5 Further
Algorithms I.J. Lynch