CSC 217
CSC 217
ARRAYS
2.0 Introductions
In many situations, sets of data items such as the set of examination marks for a class
may be conveniently arranged into a sequence and referred to by a single identifier, e.g.
MARK= (56 42 89 65 48). Such an arrangement is a data structure called an array.
Individual data items in the array may be referred to separately by stating their position in
the array. Thus MARK(1) refers to 56, MARK(2) refers to 42, MARK(3) refers to 89, and so
on. The position numbers given in the parenthesis are called subscripts. Variables may also
be used as subscripts, e.g. MARK(i), so when i=2 we are referring to mark (2), i.e. 42, and
when i=4 we are referring to MARK (4), i.e. 65.
Arrays can be declared in various ways in different languages. But consider the following
example:
As per the above illustration, the following are the important points to be considered.
• Index starts with 0.
• Array length is 10 which means it can store 10 elements.
• Each element can be accessed via its index. For example, we can fetch an element at
index 6 as 27.
The following are the basic operations supported by an array.
• Traverse – move across all the array elements one by one.
• Insertion − Adds an element at a given index.
• Deletion − Deletes an element at the given index.
• Search − Searches an element using the given index or by the value.
• Update − Updates an element at the given index.
2.1 Array elements: The individual data items in array are often called its elements. Strictly
speaking however, a data item occupies an element, i.e. elements, rather like pigeonholes, are
regarded as locations into which data items may be placed and removed.
2.3 Matrices are arrays containing only numbers and no alphabetic data. Two dimensional
arrays have elements arranged into rows and columns and are used for a variety of data
tables. For example, the examination marks of a class for several subjects could be placed in
a two-dimensional array thus:
Student English Maths c
1 A (1,1)= 56 A(1,2)=44
2 A(2,1)= 42 A(2,2)=36
3 A(3,1)= 89 A(3,2)= 73
4 A(4,1)= 65 A(4,2)= 86
5 A(5,1)= 48 A(5,2)= 51
We can represent this table as an array called A.
A = 56 44
42 36
89 73
65 86
48 51
Individual elements can be identified with two subscripts [in this particular case]: Rows 1st,
columns 2nd e.g. A (3,1) = 86. Again subscripts may be variables, as for one dimensional
array, so for A(r,c), if r=3 and c = 2, we are referring to A(3,2) = 73.
Comments In line 10, there are 10 elements we are working with, so we started with k
=10. We want to insert data in the fifth position, so we have k =10 to 5.
Step-1 indicates we are moving cells downwards from 10 to 5. In line 11,
Num (+ 1) = num ((k) copies the contents of the next elements 10 to position 11 in the array,
DIM N (10)
CLS
FOR K =1 TO 10
READ N (K)
NEXT K
INPUT "ENTER NUMBER TO SEARCH:", NUM
10 FOUND = 0
FOR K = 1 TO 10
50 IF NUM = N (K) THEN
20 FOUND = 1
PRINT "NUMBER" NUM " FOUND IN POSITION "; K
END IF
NEXT K
30 IF FOUND = 0 THEN
PRINT "NUMBER"; NUM; "NOT FOUND IN ARRAY"
END IF
DATA 45, 67, 23, 89, 12, 67, 89, 34, 58, 90
Comments: Before you begin a search, you usually declare a variable that is used to indicate
to the program if the search is successful or not. Here, the variable FOUND is used. It is
assumed first that the search item is not in the array, so we have FOUND = 0 in line 10.
Line 50 does the actual searching. It compares the value entered from the keyboard in the
variable num with the current array element in N (k). a match is found when the search item
is same as that of the current array element. At the same time, FOUND = 1 indicates that the
item is found. After traversing the array, the value of found determine what to display. If it is
still 0, it means we did not find the item required. An appropriate message is then printed.
Module 3
STRINGS
3.0 Introduction
We now turn our attention to data structures that are used for text handling and related
problems. A finite sequence of characters handled as a unit of data is called a string. For
example, ‘ABC, 3gh’, ‘kossy nwa kene Emefoh’ and ‘ the cat sat on the mat are literal strings.
In other languages in which strings are used, they are present in the form of one –
dimensional arrays of characters. In the programming language BASIC all strings identifiers
have the “$” sign as a suffix. When strings are joined together, they are said to be
concatenated. So if A ‘Kossy’ and B = ‘kene’ the assignment. C: = AB concatenates A and B
to form C where C = “kossyKene”
Part of a string is called a substring, e.g. A = “kossy” and B = “kene” are substrings of C =”
kossykene”
We shall however, be concerned primarily with strings as data structures composed of
substrings that can be operated upon individually.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
T H E C A T S A T O N T H E M A T
A: = “THE CAT SAT ON THE MAT”
1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 18 1 2 2 2 2
0 1 2 3 4 5 6 7 9 0 1 2 3
T H E C A T S A T O N T H E M A T
A(6:7) : = “AT”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
T H E C A T S A T O N T H E H A T
A (20:20): =”H”
1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 2 2 2 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3
T H E C A T A T E U P T H E M A T
A(9:14): = “ATE UP”
Fig. 3.1 (a, b, c, d)
Characte 1 2 3 4 5 6 7 8 9 1 11 1 13 14 15 16 17 18 19 2
r 0 2 0
position
content K O S S Y * Z I K O * A N N * T A M A s
a
v
e
d
s
t
o
r
a
g
e
The diagram above shows four variable-length string concatenated into a single string called
V. The same data [as in fig. 3.2a] is used but the end of each string is indicated by “*” Note
the saving in storage space.
Discuss: the possible advantage (s) variable length string (s) will have over fixed length
string (s). Discuss also the operations on the strings.