Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

Objectives

• Use one- and two-dimensional arrays in the design


of solutions to simple problems
Arrays
Unit 7 Programming

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"

• Indexes in most languages start at 0 not 1 3 "sjones"


4 "mpatel"
• To add five usernames we would use: 5
array usernames[10] 6
usernames[0] = "psmith"
7
usernames[1] = "ltorvalds"
usernames[2] = "pwatts" 8
usernames[3] = "sjones" 9
usernames[4] = "mpatel"
• How would you add five more usernames?
• How would you find an array’s length?
Arrays
Unit 7 Programming

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"

array’s length? 8 "nwhite"


9 "fdavies"
usernames.length //this returns 10
Arrays
Unit 7 Programming

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

Linear searching an array


• Adapt the code so that the user can enter a
username and find out if it is present in the array
search = input("Type in username: ")
for i = 0 to usernames.length - 1
if usernames[i] == search
print("Name found")
• What type of search algorithm have you used?
• This algorithm is a linear search
Arrays
Unit 7 Programming

Worksheet 4
• Now complete Task 1 on Worksheet 4
Arrays
Unit 7 Programming

Advantages of using arrays


• Imagine that you want to input, sort and print 100
user names. It would be very inconvenient to use
100 different variable names
• Using an array, you can write:
for i = 0 to 99
userNames[i] = input("Enter next name: ")
next i
(Sort the names)
for i = 0 to 99
print(UserName[i])
next i
• What algorithm could be used to sort a list?
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

The bubble sort


• Each item in a list is compared to the one next to it,
and if it is greater, they swap places
• At the end of one pass through the list, the largest
item is at the end of the list
• This is repeated until the items are sorted
• Suppose you have an array of five names to be
sorted:

Sam Ron Tom Bob Mo


Arrays
Unit 7 Programming

Bubble sort algorithm


names = ["Sam", "Ron", "Tom", "Bob", "Mo"]
numItems = len[names]
for i = 0 to numItems - 2
for j = 0 to numItems – i – 2
if names[j] > names[j + 1] then
swap the items
endif
next j
next i
• How can you perform the part of the
algorithm that says ‘swap the items’?
Arrays
Unit 7 Programming

Swapping items in an array


names = [“Sam”, “Ron”, “Tom”, “Bob”, “Mo”]
numItems = len[names]
for i = 0 to numItems - 2
for j = 0 to numItems – i – 2
if names[j] > names[j + 1] then
temp = names[j]
names[j] = names[j + 1]
names[j + 1] = temp
endif
next j
next i
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

• These marks could be put into a 2D array


• You can set individual items in the array like this:
mark[2][ 4] =18
•What is the value of mark[1][0]?
Arrays
Unit 7 Programming

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

© 2020 PG Online Limited

The contents of this unit are protected by 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.

You might also like