CIS1400 Programming Logic and
Technique
Topic 7 Advanced Data Types
Chapter Topics
8.1
8.2
8.3
8.4
8.5
8.6
Array Basics
Sequentially Searching an Array later topic
Processing the Contents of an Array
Parallel Arrays
Two-Dimensional Arrays
Arrays of Three or More Dimension
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.1 Array Basics
An array allows you to store a group of items of the same
data type together in memory
Why? Instead of creating multiple similar variables such as
employee1, employee2, employee3 and so on
Its more efficient to create just one variable to hold all
possible values
The number in the [ ] is the size of the array
Declare String employees[50]
Declare Integer units[10]
Declare Real salesAmounts[7]
In most languages, an arrays size cannot be changed while the
program is running.
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.1 Array Basics
The storage locations in an array are elements
Each element of the array has a unique number called a
subscript that identifies it the subscript always starts
at 0 (memory offset)
Figure 8-1 Array subscripts
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.1 Array Basics
Assigning values can be accessed individually using a
subscript
Set
Set
Set
Set
Set
numbers[0]
numbers[1]
numbers[2]
numbers[3]
numbers[4]
=
=
=
=
=
20
30
40
50
60
Array elements are
accessed by name and a
subscript, in brackets
But, it is much more efficient to use a Loop to step
through the array
Constant Integer SIZE = 5
Integer numbers[SIZE]
[0] [1] [2] [3] [4]
20 30 40 50 60
5
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.1 Array Basics
Array elements are accessed by
name and a subscript, in brackets
Constant Integer SIZE = 5
Declare Integer numbers[SIZE]
Declare Integer index, value = 20
For index = 0 To SIZE 1
Set numbers[index] = value
Set value = value + 10
End For
Constant Integer SIZE = 5
Integer numbers[SIZE]
[0] [1] [2] [3] [4]
20 30 40 50 60
6
Note how loop
counter goes from
0 to maximum
array subscript
(which is one less
than the size)
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.1 Array Basics
Assigning array elements through user input
Program 8-3
Constant Integer SIZE = 3
Declare Integer hours[SIZE]
Declare Integer index
For index = 0 To SIZE 1
Display Enter the hours worked by
Display employee number , index + 1
input
Input hours[index]
End For
For index = 0 To SIZE 1
Display hours[index]
display
End For
7
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.1 Array Basics
Arrays can be initialized to 0 or specific values in
declaration statement
Declare String days[7] = Sunday, Monday,
Tuesday, Wednesday, Thursday,
Friday, Saturday
days
CIS1400 Programming Logic and Technique
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
7 -- Advanced Data Types
8.1 Array Basics
Array bounds checking should be performed to avoid use
of an invalid subscript
Set days[7] = Saturday
is invalid because there is no 7 index
A common error is running a loop one time more than is
necessary, exceeding the bounds of the array
Usually causes a runtime error (program crash)
Off-by-one Error
Remember: arrays subscripts start at 0 rather than 1
Constant Integer SIZE = 100
Declare Integer numbers[SIZE]
Declare Integer index
For index = 1 To SIZE 1
Set numbers[index] = 0
End For
9
Constant Integer SIZE = 100
Declare Integer numbers[SIZE]
Declare Integer index
For index = 0 To SIZE
Set numbers[index] = 0
End For
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.3 Processing the Contents of an Array
Totaling the values in an array and calculating average
Program 8-10
Constant Integer SIZE = 5
Declare Real scores[SIZE] = 2.5, 8.3, 6.5, 4.0, 5.2
Declare Real average, total = 0
Declare Integer index
For index = 0 To SIZE - 1
total
Set total = total + scores[index]
End For
average
Set average = total / SIZE
Display The average of the array elements is , average
Program Output
The average of the array elements is 5.3
10
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.3 Processing the Contents of an Array
Totaling the values in an array and calculating average
total
average
11
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.3 Processing the Contents of an Array
Finding the highest & lowest values input
Create variables to hold the highest and lowest values
Assign the first input number to the highest and lowest
variables
Use a loop to step through the user input data
Each iteration, a comparison is made between the user
input value to the highest and lowest variables
12
Lab #4 used sentinel value of -99
If the element is greater than the highest value, that value is then
the assigned to the highest variable
If the element is less than the lowest value, that value is then the
assigned to the lowest variable
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.3 Processing the Contents of an Array
Finding the highest & lowest values in an array
The highest
Create a variable to hold the highest value
Assign the value at element 0 to the highest
Use a loop to step through the rest of the elements
Each iteration, a comparison is made to the highest variable
13
Based upon array size
If the element is greater than the highest value, that value is then
the assigned to the highest variable
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.3 Processing the Contents of an Array
Finding the highest & lowest values in an array
The highest
Program 8-11
Constant Integer SIZE = 5
Declare Integer numbers[SIZE] = 8, 1, 12, 6, 2
Declare Integer index
Declare Integer highest
Set highest = numbers[0]
For index = 1 To SIZE 1
If numbers[index] > highest Then
Set highest = numbers[index]
End If
End For
Display The highest value in the array is , highest
Program Output
The highest value in the array is 12
14
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.3 Processing the Contents of an Array
Finding the highest & lowest values in an array
The highest
15
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.3 Processing the Contents of an Array
Finding the highest & lowest values in an array
The lowest
Same process, but checks if the element is less than the
lowest value
Program 8-12
Constant Integer SIZE = 5
Declare Integer numbers[SIZE] = 8, 1, 12, 6, 2
Declare Integer index
Declare Integer lowest
Set lowest = numbers[0]
For index = 1 To SIZE 1
If numbers[index] < lowest Then
Set lowest = numbers[index]
End If
End For
Display The lowest value in the array is , lowest
Program Output
The lowest value in the array is 1
16
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.3 Processing the Contents of an Array
Finding the highest & lowest values in an array
The lowest
17
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.3 Processing the Contents of an Array
Copying an array can be done using a loop to copy the
individual elements
Constant Integer SIZE = 5
Declare Integer firstArray[SIZE] = 100, 200, 300,
400, 500
Declare Integer secondArray[SIZE]
Declare Integer index
For index = 0 To SIZE 1
Set secondArray[index] = firstArray[index]
End For
firstArray
18
secondArray
0 100
100
200
200
300
400
300
400
500
500
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.3 Processing the Contents of an Array
Passing an Array as an Argument
Usually must pass the array and the size
The module call
Set sum = getTotal(numbers, SIZE)
30
10
The module header
Function Integer getTotal (Integer array[], Integer
arraySize)
19
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.3 Processing the Contents of an Array
Passing an Array as an Argument
Program 8-13
Module main()
Constant Integer SIZE = 5
Declare Integer numbers[SIZE] = 2, 4, 6, 8, 10
Declare Integer sum
Set sum = getTotal(numbers, SIZE)
Display The sum of the array elements is , sum
End Module
argument only uses variable
name;
parameter uses data type,
parameter name, and brackets
Function Integer getTotal(Integer array[], Integer arraySize)
Declare Integer index
Declare Integer total = 0
For index = 0 to arraySize 1
Set total = total + array[index]
End For
Return total
End Function
Program Output
The sum of the array elements is 30
20
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.3 Processing the Contents of an Array
Passing an Array as an Argument
2 4 6 8 10
numbers
array
arraySize
SIZE
sum
30
total
21
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.4 Parallel Arrays
Two or more arrays that hold related
data and related elements are
accessed with common subscript
No special terminology in array
definition
Arrays should be of same size
Arrays need not be of same data type
Related elements are accessed with a
common subscript
22
Figure 8-14 The names
and addresses arrays
Stored in same relative location of each
array
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.4 Parallel Arrays
Constant Integer SIZE = 5
Declare String names[SIZE]
Declare String addresses[SIZE]
Declare Integer index
For index = 0 To SIZE 1
Display Name: , names[index]
Display Address: , addresses[index]
End For
In The
Spotlight
Using Parallel
Arrays (p316)
23
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.5 Two-Dimensional Arrays
A two-dimensional array is like several identical (data type)
arrays put together
Useful for working with multiple sets of data
Suppose a teacher has six students who take five tests
Figure 8-17 Twodimensional array with six
rows and five columns
24
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.5 Two-Dimensional Arrays
Two size variables are required when
declaring
Constant Integer ROWS = 3
Constant Integer COLS = 4
Declare Integer values[ROWS][COLS]
Declare Integer row, col
26
Accessing is done with two loops, and
both subscripts
Get values to store in array
For row = 0 To ROWS - 1
For col = 0 To COLS 1
Display Enter a number.
Input values[row][col]
End For
End For
25
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.5 Two-Dimensional Arrays
Accessing is done with two
loops, and both subscripts
25
Display values in array
Display Here are the values
you entered.
For row = 0 To ROWS - 1
For col = 0 To COLS 1
Display values[row][col]
End For
0
1
2
3
End For
0
1
2
26
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
8.6 Arrays of Three or More
Dimensions
Arrays can also be three or more dimensions
Use a bracket for each dimension in declaration
Constant Integer ROWS = 2, COLS = 3, DEPTH = 5
Declare Integer values[ROWS][COLS][DEPTH]
Use a bracket for each dimension in accessing one element
Set values[1][2][4] = 30
Use a loop for each dimension when accessing all elements
For x = 0 To ROWS -1
For y = 0 To COLS 1
For z = 0 To DEPTH - 1
Display Enter a number.
30
Input values[x][y][z]
End For
End For
End For
27
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
Chapter Topics
12.1 Introduction
12.2 Character-by-Character Text Processing
28
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
12. 1 Introduction
Functions covered so far operate on strings
29
Chapter 6 Functions
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
12.2 Character-by-Character Text
Processing
Most languages provide a way to access the individual
characters in a string
Often this is done with subscript notation, similar to accessing
the elements of an array
Program 12-2
Module main()
Declare String name = Jacob
Declare Integer index
For index = 0 to length(name) - 1
Display name[index]
End For
End Module
Program Output
J
a
c
o
b
30
Loop used to step
through all
characters in string.
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
12.2 Character-by-Character Text
Processing
Individual characters of a string
Can be modified:
Declare String str = "Coffee"
str[0] = "T"
Displays Toffee
Display str
Although Python does
support subscript notation of
strings, strings are
immutable and they cannot
change once they have been
created.
Can be tested:
In The
Spotlight
Validating a
Password
(p482)
31
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
12.2 Character-by-Character Text
Processing
Python string methods
32
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
12.2 Character-by-Character Text
Processing
Program 12-4
Module main()
Declare String str
Declare Integer index
Declare Integer upperCaseCount = 0
Display Enter a sentence.
Input str
For index = 0 to length(str) - 1
If isUpper(str[index]) Then
Set upperCaseCount = upperCaseCount + 1
End If
End For
Display That string has , uppercaseCount, uppercase letters.
End Module
Program Output
Enter a Sentence.
Ms. Jones will arrive TODAY! [Enter]
That string has 7 uppercase letters.
33
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
12.2 Character-by-Character Text
Processing
Pseudocode functions for inserting and deleting
characters
Since Python strings are immutable, they cannot change
once they have been created.
34
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
12.2 Character-by-Character Text
Processing
Insert example
Declare String str = "New City"
insert(str, 4, "York ")
Display str // displays New York City
N e w
C i t y
Y o r k
N e w
35
Y o r k
C i t y
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types
12.2 Character-by-Character Text
Processing
Delete example
Declare String str = "I ate 1000 blueberries!"
delete(str, 8, 9)
Display str // displays I ate 10 blueberries!
a t e
1 0 0 0
b l u e b e r r i e s !
0 0
I
36
a t e
1 0
b l u e b e r r i e s !
CIS1400 Programming Logic and Technique
7 -- Advanced Data Types