T4 Arrays
T4 Arrays
Starter
• Imagine a medieval computer game for two players
that uses variables to store their data – e.g.
playerName1 = "Olivia"
playerName2 = "Noah"
print(playerName1)
print(playerName2)
• How would you write this
code if the game had
an army with 500
players in it?
Arrays
Unit 7 Programming
Starter
• One solution would be to create a variable for
every player
playerName1 = "Olivia"
playerName2 = "Noah"
…
playerName500 = "Elijah"
• This would involve lots of copying and pasting
of code and become a big problem with
writing algorithms
• Instead other data structures such as arrays are used
Arrays
Unit 7 Programming
Arrays
Usernames
• An array is a data structure 0
that allows you to hold many 1
items of data which is 2
referenced by one identifier 3
4
• All items of data in the array must
5
be of the same data type
6
• An array of 10 strings called 7
userNames could be 8
declared as: 9
• array usernames[10]
Arrays
Unit 7 Programming
Arrays
Usernames
• Information can be read 0 "psmith"
from or added to the array 1 "ltorvalds"
using the index number 2 "pwatts"
Arrays
Usernames
• How would you add five 0 "psmith"
more usernames? 1 "ltorvalds"
2 "pwatts"
usernames[5] = "bwright"
3 "sjones"
usernames[6] = "mgreen"
usernames[7] = "dthomas" 4 "mpatel"
usernames[8] = "nwhite" 5 "bwright"
usernames[9] = "fdavies" 6 "mgreen"
• How would you find an 7 "dthomas"
Arrays
Usernames
• An array has a fixed length 0 "psmith"
1 "ltorvalds"
• We cannot add any more usernames
2 "pwatts"
to the array shown
3 "sjones"
• To add more usernames, we would need
4 "mpatel"
to create a new larger array an copy the
names across 5 "bwright"
6 "mgreen"
• Python doesn’t have an array data structure –
it instead uses lists which can alter their length 7 "dthomas"
8 "nwhite"
• How would you write a FOR loop to 9 "fdavies"
output all the usernames in the array?
Arrays
Unit 7 Programming
Arrays
Usernames
• How would you write a 0 "psmith"
FOR loop to output all the 1 "ltorvalds"
usernames in the array? 2 "pwatts"
3 "sjones"
for i = 0 to usernames.length - 1
4 "mpatel"
print(username[i])
endfor 5 "bwright"
6 "mgreen"
• Adapt the code so that the user can
7 "dthomas"
enter a username and find out if it is 8 "nwhite"
present in the array 9 "fdavies"
• What type of search algorithm have you used?
Arrays
Unit 7 Programming
Worksheet 4
• Now complete Task 1 on Worksheet 4
Arrays
Unit 7 Programming
Linear search
• In a linear search, each item is examined in
sequence until the item is found or the end of the
list is reached
• How can this algorithm be made more efficient?
resort = ["Margate", "Swansea", "Brighton",
"St Ives", "Cromer"]
for n = 0 to resort.length - 1
if townName == resort[n] then
found = True
endif
next n
• Suggest a different loop and Boolean expression
Arrays
Unit 7 Programming
Linear search
• The search should stop as soon as the item is found
resort = ["Margate", "Swansea", "Brighton",
"St Ives", "Cromer"]
found = False
i = 0
while NOT found AND i < resort.length
if townName == resort[i] then
found = True
endif
i = i + 1
endwhile
• How many times is the loop performed if you are
searching for Brighton? Or Bournemouth?
Arrays
Unit 7 Programming
Worksheet 4
• Now complete Task 2 on Worksheet 4
Arrays
Unit 7 Programming
Two-dimensional arrays
• Suppose you wanted to hold in a program, 5 marks
for each of 3 students
0 1 2 3 4
Student1 12 14 8 7 17 0
Student2 6 8 5 3 13 1
Student3 16 15 9 10 18 2
Processing a 2D array
• Find and display the total mark for each student:
Student1 12 14 8 7 17
Student2 6 8 5 3 13
Student3 16 15 9 10 18
for s = 0 to 2
studentTotal = 0
for m = 0 to 4
studentTotal = studentTotal +
mark[s] [m]
next m
print(studentTotal)
next s
Arrays
Unit 7 Programming
Worksheet 4
• Now complete Task 3 on Worksheet 4
Arrays
Unit 7 Programming
Plenary
• Complete the following with the words beneath
_________ allow many data items to be stored
under one _________. Each item in the array can be
accessed by its array _________. Indexes start at
_________. By using a _________ we can easily
loop through an array. Arrays have a _________ and
all items in the array have the same _________.
zero index FOR loop fixed length
arrays data type identifier
Arrays
Unit 7 Programming
Plenary
• Complete the following with the words beneath
Arrays allow many data items to be stored under one
identifier. Each item in the array can be accessed by
its array index. Indexes start at zero. By using a
FOR loop we can easily loop through an array.
Arrays have a fixed length and all items in the array
have the same data type.
zero index FOR loop fixed length
arrays data type identifier
Arrays
Unit 7 Programming
Copyright
This unit and all the worksheets, PowerPoint presentations, teaching guides and other associated files
distributed with it are supplied to you by PG Online Limited under licence and may be used and copied by you
only in accordance with the terms of the licence. Except as expressly permitted by the licence, no part of the
materials distributed with this unit may be used, reproduced, stored in a retrieval system, or transmitted, in any
form or by any means, electronic or otherwise, without the prior written permission of PG Online Limited.
Licence agreement
This is a legal agreement between you, the end user, and PG Online Limited. This unit and all the worksheets,
PowerPoint presentations, teaching guides and other associated files distributed with it is licensed, not sold, to
you by PG Online Limited for use under the terms of the licence.
The materials distributed with this unit may be freely copied and used by members of a single institution on a
single site only. You are not permitted to share in any way any of the materials or part of the materials with any
third party, including users on another site or individuals who are members of a separate institution. You
acknowledge that the materials must remain with you, the licencing institution, and no part of the materials may
be transferred to another institution. You also agree not to procure, authorise, encourage, facilitate or enable any
third party to reproduce these materials in whole or in part without the prior permission of PG Online Limited.