Transcript Array
Data Types & Structures
Using Arrays
3.2.3 – F452
Data Types
Basic data types used in programming languages are:
Numeric data types, including:
Integer: whole numbers, e.g. 7
Real: numbers which may have a decimal point,
e.g. 7.2
Alphanumeric data types, including:
Char: a single character, digit or symbol, e.g. ‘c’
String: several characters grouped together, e.g.
‘Mrs. Smith’
Boolean
data which can only take one of two values
(usually TRUE or FALSE)
Array Definition
A data structure that contains several data
items…
of the same data type…
grouped under one identifier…
Individual items are accessed using an index
Stored contiguously in computer memory
Array Declaration
Items which should be specified when declaring
an array
Identifier/name
Data type
Dimensions (1 or 2)
Size of the array (number of elements)
Why use an Array?
Why use an array rather than separate
variables?
Code is easier to manage as there are fewer
variables.
Can use iteration instead of dealing with
each variable separately.
Pascal Arrays (1 Dimensional)
Var
counter : Integer;
myArray : Array[1..5] of Integer;
Begin
myArray[2] := 25;
counter := myArray[2];
writeln(counter)
End.
25
1 Dimensional Arrays
Var
counter : Integer;
myArray : Array[1..5] of Integer;
myArray
counter
Data
1
2
3
4
5
25
Initialise 1-D Array
FOR counter=1 TO 5
myArray(counter)= 0
NEXT counter
Set values of each element within the array
to 0
Search a 1-Dimensional Array
Var
counter : Integer;
names : Array[1..5] of string;
names
counter
Data
1
Adam
2
Senna
3
Dharmesh
Get Search string
4
Look at each element of Array
Does search string = element of Array?
5
If yes then Output “Found Name”
Stop
If no look at next element of array
Continue until search string found or at end of array
Bharvesh
Akshay
Search Algorithm
Var
counter : Integer;
names : Array[1..5] of string;
name : string
found : boolean;
Begin
Readln(name)
Begin
Found = false
For count = 1 to NumberofElements do
If string = names[count] then
Begin
Found = true
Output names[count]
Exit loop
End
End
If found = false then
Begin
Output “Name not found”
End
End
Pseudo Code
2 Dimensional Arrays
Var
my2DArray : Array[1..3,1..5] of integer
Begin
my2DArray[2][4] := 10;
End.
1
1
2
3
2
3
4
10
5
Fill a 2-D Array
Var
my2DArray : Array[1..3,1..5] of integer
row, column : integer
For row = 1 to 3 do
begin
For column = 1 to 5 do
begin
My2DArray [row] [column] = 10
end
end
Pascal Programming Challenge – 1 D Array
Can you…
A string array of 10 elements
Input a name into each element
Ask user for name to search for
Search the array
Display name and element number if found
Display message if not found eg.
“The name Fred is not in this list”
Concatenating the message – user friendly
Look at the algorithm on slide 10
Search Pseudo Code
The algorithm on slide 10 has a small bug.
Can you work it out? What if you have 2
elements with the same name?
How can you alter your code to
accommodate this?
Pascal Programming Challenge – 2 D Array
Can you…
Generate a multiplication table
10x10 using a 2 D array
Fill each element using a loop
Display the grid eg…
1
2
3
4
2
4
6
8
3
6
9
12