Lecture Outline ! " Arrays ([KOFF98] Chapter 10) –" Array data structure –" Sequential access to array elements –" Manipulating Arrays –" Two-dimensional arrays
COMP152 PASCAL\PROGRAM DEVELOPMENT
Presented by Dr Ioanna Dionysiou Copyright (c) 2008 Ioanna Dionysiou
Array Data Structure
2
Declaring Arrays
! "It is a structure in which we store a collection of data items of the SAME type
! "First, we describe the structure of an array in an array type declaration
–" Example data items •" exam scores •" Names
(Integer) (String)
TYPE array-name = array[subscript-type]of element-type;
–" We can •" refer to this collection using a single name –" Array name
•" access each member of this collection
name of the array type
–" Array elements
Copyright (c) 2008 Ioanna Dionysiou
3
boolean, char, enumerated type, subrange
type of each element in the array. All elements are the same type
Copyright (c) 2008 Ioanna Dionysiou
4
Declaring Arrays
Declaring Array Variable ! "Second, declare a variable of that array type
TYPE RealArray = array[1..8]of Real;
IntArray = array[5..15]of Integer;
CharArray = array[‘A’..’Z’]of Boolean;
Array type : RealArray Number of elements : 8 (1..8) Type of element : Real
TYPE RealArray = array[1..8]of Real; IntArray = array[5..15]of Integer; CharArray = array[‘A’..’Z’]of Boolean;
Array type : IntArray Number of elements : 11 (5..15) Type of element : Integer
VAR X : RealArray; Y : IntArray; Z : CharArray;
Array type : CharArray Number of elements : 26 (‘A’..’Z’) Type of element : Boolean
Array type : RealArray Number of elements : 8 (1..8) Type of element : Real X is a variable of type RealArray Array type : IntArray Number of elements : 11 (5..15) Type of element : Integer Y is a variable of type IntArray Array type : CharArray Number of elements : 26 (‘A’..’Z’) Type of element : Boolean Z is a variable of type CharArray
Copyright (c) 2008 Ioanna Dionysiou
Copyright (c) 2008 Ioanna Dionysiou
5
Summary
6
Summary
TYPE RealArray = array[1..5]of Real;
TYPE RealArray = array[1..5]of Real;
TYPE RealArray = array[1..5]of Real; VAR X : RealArray;
VAR X : RealArray;
What do we have at this point?
Copyright (c) 2008 Ioanna Dionysiou
What do we have at this point?
7
Copyright (c) 2008 Ioanna Dionysiou
8
Accessing Array Elements TYPE RealArray = array[1..5]of Real;
Accessing Array Elements
X[1] X[2] X[3] X[4] X[5]
TYPE RealArray = array[5..9]of Real;
? ? ? ? ?
VAR X : RealArray;
? ? ? ? ?
VAR X : RealArray;
To process data stored in an array we must be able to access its individual elements.
array name + array subscript (also known as index) X[5] X[6] X[7] X[8] X[9]
array name + array subscript (also known as index) X[1] X[2] X[3] X[4] X[5]
X[5] X[6] X[7] X[8] X[9]
access the 1st element of the array access the 2nd element of the array access the 3rd element of the array access the 4th element of the array access the 5th element of the array
Must not access x[1], x[10], etc..why?
Copyright (c) 2008 Ioanna Dionysiou
Copyright (c) 2008 Ioanna Dionysiou
9
Manipulating Array Elements TYPE RealArray = array[1..5]of Real;
access the 1st element of the array access the 2nd element of the array access the 3rd element of the array access the 4th element of the array access the 5th element of the array
Manipulating Array Elements TYPE RealArray = array[1..5]of Real;
X[1] X[2] X[3] X[4] X[5] 5.0 21.0
?
?
10
?
VAR X : RealArray;
X[1] X[2] X[3] X[4] X[5] 5.0
0.0
4.0
?
?
VAR X : RealArray;
To manipulate array elements, we use array name + array subscript (also known as index) = expression
To manipulate array elements, we use array name + array subscript (also known as index) = expression
X[1] := 5.0 stores value 5.0 to the 1st element of the array X[2] := 15 + 6.0 stores value 21.0 to the 2nd element of the array
X[3] := sqrt(16) stores result of the function to the 3rd element of the array X[2] := 0.0 replaces value 21.0 with 0.0 in the 2nd element of the array
Copyright (c) 2008 Ioanna Dionysiou
11
Copyright (c) 2008 Ioanna Dionysiou
12
Manipulating Array Elements TYPE RealArray = array[1..5]of Real;
Manipulating Array Elements TYPE RealArray = array[1..5]of Real;
X[1] X[2] X[3] X[4] X[5] 5.0
0.0
4.0
9.0
?
VAR X : RealArray;
X[1] X[2] X[3] X[4] X[5] 5.0
0.0
4.0
9.0
6.0
VAR X : RealArray;
To manipulate array elements, we use array name + array subscript (also known as index) = expression
To manipulate array elements, we use array name + array subscript (also known as index) = expression
X[4] := X[1]+X[3] stores the summation of values of X[1] and X[3] in X[4]
X[5] := 2 +X[3] stores the summation of 2 and value of X[3] in X[5]
Copyright (c) 2008 Ioanna Dionysiou
Manipulating Array Elements TYPE RealArray = array[1..5]of Real;
Copyright (c) 2008 Ioanna Dionysiou
13
Initializing an Array
X[1] X[2] X[3] X[4] X[5]
PROGRAM ArrayInit(Input,Output);
5.0
TYPE RealArray = array[1..5]of Real;
0.0
4.0
9.0
14
6.0
VAR X : RealArray;
VAR X : RealArray; count : Integer; X[1] X[2] X[3] X[4] X[5]
BEGIN for count:=1 to 5 do X[count] := 2.0; END.
To manipulate array elements, we use array name + array subscript (also known as index) = expression
2.0
2.0
2.0
2.0
2.0
X[6] := 10 What happens here? Standard Pascal would display Index expression out of bounds during runtime Turbo Pascal does not check for invalid array subscripts unless range checking is enabled with the {$R+} compiler directive Copyright (c) 2008 Ioanna Dionysiou
15
Copyright (c) 2008 Ioanna Dionysiou
16
Initializing an Array
Initializing an Array
PROGRAM ArrayInit(Input,Output); TYPE IntArray = array[1..5]of Integer;
PROGRAM ArrayInit(Input,Output); TYPE CharArray = array[1..5]of Char;
VAR X : IntArray; count : Integer;
VAR X : CharArray; count : Integer;
BEGIN for count:=1 to 5 do BEGIN Writeln(‘Enter value for X[‘,count,’] element : ‘); Readln(X[count]); END; END.
BEGIN for count:=1 to 5 do X[count] := chr(64+count); END.
Suppose user enters at the appropriate prompts 10 20 X[1] X[2] X[3] X[4] X[5] 30 10 20 30 40 50 40 50 Copyright (c) 2008 Ioanna Dionysiou
FYI, letter ‘A’ has an ord value of 65
X[1] X[2] X[3] X[4] X[5]
Copyright (c) 2008 Ioanna Dionysiou
17
Initializing an Array
18
Initializing an Array
PROGRAM ArrayInit(Input,Output);
PROGRAM ArrayInit(Input,Output);
TYPE CharArray = array[1..5]of Char;
TYPE BoolArray = array[‘a’..’e’]of Boolean;
VAR X : CharArray; count : Integer;
VAR X : BoolArray; count : Integer;
BEGIN for count:=1 to 5 do X[count] := chr(64+count); END.
BEGIN for count:=‘a’ to ‘e’ do X[count] := true; END.
FYI, letter ‘A’ has an ord value of 65
X[‘a’]
true
X[1] X[2] X[3] X[4] X[5] A
B
Copyright (c) 2008 Ioanna Dionysiou
C
D
X[‘b’]
true
X[‘c’]
X[‘d’]
X[‘e’]
true
true
true
E
19
Copyright (c) 2008 Ioanna Dionysiou
20
Lecture Outline
Sequential Access to array elements ! "Many programs require that all elements of an array be processed in sequence, starting with the first element. For example: –" Initialize an array –" Print array contents –" To perform any other sequential processing task such as searching
Copyright (c) 2008 Ioanna Dionysiou
Use for loop Use counter variable as subscript
Copyright (c) 2008 Ioanna Dionysiou
21
22
Cubes of Integers
Sequential Access to Array Elements
PROGRAM Cubes(Input,Output); TYPE CubeArray = array[1..10] of Integer;
! " Suppose that we want to –" Initially, store the cubes of the first 10 integers in an array –" Get user input
VAR X : CubeArray; count : Integer; num : Integer;
•" If the user enters -1 –" print cubes for all 10 integers
•" If the user enters a number between 1 and 10 –" print the cube for a specific integer
! " Steps –" Declare the array data structure •" Data element type? Array Size? –" Store data in array •" Use for loop –" Read and display contents of entire array or individual array element
Copyright (c) 2008 Ioanna Dionysiou
BEGIN /* initialize the array X */ for count:=1 to 10 do X[count] := count * count * count; writeln(‘Enter number ‘); readln(num); if (num = -1) then for count:=1 to 10 do /* print all cubes for all numbers */ Writeln(‘The cube for ‘,count, ‘is ‘, X[count]) else /* print the cube for number num */ Writeln(‘The cube for ‘,num, ‘is ‘, X[num]) /*no error checking*/ END. 23
Copyright (c) 2008 Ioanna Dionysiou
24
Cubes of Integers
Lecture Outline
PROGRAM Cubes(Input,Output); TYPE CubeArray = array[1..10] of Integer; VAR X : CubeArray; count : Integer; num : Integer; BEGIN /* initialize the array X */ for count:=1 to 10 do X[count] := count * count * count; writeln(‘Enter number ‘); readln(num); if (num = -1) then /*perform error checking*/ for count:=1 to 10 do /* print all cubes for all numbers */ Writeln(‘The cube for ‘,count, ‘is ‘, X[count]) else if (num >=1) and (num