Python - Arrays, Procedures & Functions (Inc Linear & Bubble) - Student
Python - Arrays, Procedures & Functions (Inc Linear & Bubble) - Student
2 – Procedures,
Functions & Arrays
1D & 2D Arrays (Including Linear Search & Bubble Sort)
Unit
Objectives
Silent Starter
1
From the program below identify the inputs / outputs and processes that take place in the
program. You will need to copy orange boxes a number of times.
Input
Output
Process
Silen
t
Star
t
Silent Starter 1: Progress Check
Feed
ba ck
Silent Starter
2
1. Below is a simple program that will take your age in years and convert it into days.
2. Imagine you had to write this program with procedures. List the names of the procedures
you would use?
Answer here:
Silen
t Extension:
Star
t What limitation does this program
have?
Silent Starter 2: Progress Check
Feed
ba ck
Silent Starter
3
1. Below are a list of variables please write the equivalent pseudocode to DECLARE and
initialise these variables.
Silen
t
Star
t
Silent Starter 3: Progress Check
Feed
ba ck
Silent Starter
4
1 89 12 34 26
Consider this array – how could we sort it?
Silen
t
Star
t
Silent Starter 4: Progress Check
Feed
ba ck
Silent Starter 5:
name <-
Procedures
The pseudoco
de for this is:
””
th e
Write PROCEDURE get
do code
pse u
s
Name()
for thi OUTPUT ”Enter
progra
m your name”
INPUT name
END PROCEDURE
PROCEDURE out
putName()
OUTPUT ”Your
name name is”,
END PROCEDURE
Teac
h
CALL getName(
Expl er )
anat CALL outputNa
i me()
on
Silent Starter 5: Progress Check
Feed
ba ck
Task 1: Calculator Part
1. Write a program that will ask the user for two numbers, value1 and
1
value2.
The program will perform FOUR different calculations on those numbers:
Sample Output:
• addition (value1 + value2)
• subtraction (value1 - value2) Enter a number: 3
• multiplication (value1 x value2) Enter a number: 2
• division (value1 / value2)
Addition: 5
Subtraction: 1
2. Screenshot your code onto the next slide.
Multiplication: 6
Division: 1.5
Task
Task 1: Calculator Part
1. Screenshot your code from the previous slide below.
1
Python Code Runtime
Task
Procedures
Presentation
Mode Needed
is th e is su e w ith this
Wh a t
essay?
No paragraphs
Teac
h
Expl er
anat
i
on
Procedures
Nearly all programs will have some form of input, process and output.
Presentation I.e. The user will input some data, the computer will process this and
Mode Needed some output will be generated.
INPUT
Teac OUTPUT
h
Expl er
anat
i
on
Procedures
It would be nice if we could label these segments of code and
essentially treat them as ‘paragraphs’ instead of having one block of
Presentation code….
Mode Needed
We could label them with sensible names like getName for the code
where we input the name, and outputName where the code outputs the
name.
e tt in g the
h is a re a is g is a
T t N a m e
m e , s o ge
n a
e l f o r this.
b
good la
is o u t puting
a
This are m e , so
Teac th e n a
h is a good
Expl er a m e
outputN or this.
anat
on
i label f
a re a s of
Procedures
am e th e
a lly n dures.
To offici n eed p ro c e
y o u
c od e
Presentation
Mode Needed procedure
name
Body of the
procedure is
def is a command indented.
which allows you Unindent to finish.
to define a
procedure or a
function.
Teac
h
Expl er
anat
i
on
Procedures
If I r
Presentation
erro un the
Mode Needed r s, b c
ut n ode, th
ot h
ing ere are
hap
pen no
s???
Teac
h
Expl er This is my perfect code, but…
anat
i
on
Procedures
Presentation
Mode Needed 1. In order to run this procedure
code I need to run ‘call’
getName()
Presentation
Mode Needed
Teac
h
Expl er You would call
anat
i outputName()
on
Procedures
1 The chron
ological o
rder of
Presentation the progr
. am is a s f
Mode Needed ollows…
3
Where do you
. think next?
And lastly?
5
.
Teac
2
h
Expl er
anat . 4
i
on .
Procedures
Presentation
Mode Needed Global
To makevariables
sure a is a
that
variable is the
global
whole program can
access.
In the 2nd line of a
procedure write
Before and
global all variables
list the
are global.
variables.
Teac
h
Expl er
anat
i
on
Procedures
Presentation
Mode Needed
If you declare a variable
inside a procedure this is
a local variable and can
only be used in this
procedure.
Teac
h
Expl er
anat
i
on
Procedures
Presentation
Mode Needed
Is this ok?
Teac
h
Expl er
anat
i
on
Task 2: Fix the Errors
Below is the code to ask for someone’s age, which uses procedures. However there are
mistakes and missing code. Use the yellow boxes to fix the code. There are more boxes than
you need.
age = 0
Task
Feedback: Task 2
Feed
ba ck
Procedures
The pseudoco
de for this is:
name <- ””
Presentation
Mode Needed PROCEDURE get
Name()
OUTPUT ”Enter
your name”
INPUT name
END PROCEDURE
PROCEDURE out
putName()
OUTPUT ”Your
name name is”,
END PROCEDURE
Teac
h
CALL getName(
Expl er )
anat CALL outputNa
i me()
on
Task 3: Calculate area of
Triangle 2
Previously you made this program, but now you have to use procedures.
HINTS:
4 procedures:
• getBase()
Task Delete me for hints.
• getHeight()
• calculateArea()
• outputArea()
Task 3: Calculate area of
Psuedocode of program (missing
Triangle 2
bits)
base <- 0
height <-0 Sample Output:
PROCEDURE getBase()
OUTPUT “Enter the base” Enter base of triangle: 10
INPUT base Enter Height of triangle: 5
END PROCEDURE
Delete me for
PROCEDURE getHeight() The area of your triangle is: 25
//MISSING CODE
END PROCEDUREPseudocode.
PROCEDURE calculateArea()
area <- (base * height)/2
END PROCEDURE
Hints:
Three procedures:
PROCEDURE outputArea()
OUTPUT >>>
getBase()
END PROCEDURE
getHeight()
Task Call getBase()
outputArea()
Call getHeight()
Call calculateArea()
Call outputArea()
Task 3: Calculate area of
1. Screenshot your code from the previous slide below.
Triangle 2
Python Code Runtime
Task
Task 4: Odd or Even 2
Previously you made this program, but now you have to use procedures.
Hints:
Three procedures:
getNumber()
DELETE ME FOR HINTS
checkOddOrEven()
outputOddOrEven()
Task
Task 4: Odd or Even 2
Psuedocode for program
number <- 0
oddOrEven <- False
Sample Output:
PROCEDURE getNumber()
OUTPUT “Enter a number” Enter a number: 5
INPUT number
END PROCEDURE
The number is odd.
DELETE ME FOR
PROCEDURE checkOddOrEven()
MISSING CODE
Pseudocode
END PROCEDURE
PROCEDURE outputOddOrEven()
OUTPUT oddOrEven
END PROCEDURE
Call getNumber()
Call checkOddOrEven()
Call outputOddOrEven()
Task
Task 4: Odd or Even 2
1. Screenshot your code from the previous slide below.
Task
Task 5: Odd or Even 2
Previously you made this program, but now you have to use procedures.
Hints:
Three procedures:
getNumber()
DELETE ME FOR HINTS
eheckOddOrEven()
outputOddOrEven()
Task
Task 4: Odd or Even 2
1. Screenshot your code from the previous slide below.
Task
Task 5: Head and Tails 2
Previously you made this program, but now you have to use procedures.
Hints:
You will need to use coin random.randint()
Three procedures:
Task DELETE ME FOR HINTS
simulateCoin()
checkHeadTails()
outputHeadsTails()
Task 5: Head and Tails 2
Hints:
coin <- 0
headCount <- 0
tailsCount <- 0
counter <- 0
Sample Output:
PROCEDURE simulateCoin()
coin= ROUND(RANDOM( ) * 1,0)
END PROCEDURE Heads and Tails Simulator
PROCEDURE checkHeadsTails()
if coin == 1: Number of Heads: 20
headCount <- headCount + 1 Humber of Tails: 30
DELETE ME FOR HINTS
//MISSING REST OF CODE
END PROCEDURE
PROCEDURE outputHeadsTailsCount()
//MISSING CODE
END PROCEDURE
Task
Task 6: Calculator Part
2i
Make a calculator program which allows the basics functions of a calculator: add,
subtract, multiply and divide.
Task outputAnswer()
Task 2: Calculator Part
1. Screenshot your code from the previous slide below.
2i
Python Code Runtime
Task
Functions
A function is a block of code that only runs when it is called. Python
functions return a value using a return statement, if one is specified. A
function can be called anywhere after the function has been declared.
t he caller.
t u rn e d to
th i s is re
When da t a.
ai n t h e
l co n t
2 wi l
Name
How could we
output name?
Teac
Expl her
anat
io
n
Functions
Teac
Expl her
anat
io
n
Functions
For the function header you
have to declare the return
The pseudoco datatype
de for this is:
FUNCTION getN
ame() RETURNS
OUTPUT ”Enter STRING
your name”
INPUT name
RETURN name n i t to a
as si g
END FUNCTION n ctio n y o u
t ho n)
e f u s py
th ea
To call le (the sam
name2 <- getN v ar i ab
ame()
OUTPUT name2
Teac
Expl her
anat
io
n
Task 1: Functions
On the left is the code to add two numbers
together. The numbers in each box
correspond to the numbers in the table
below, complete the table with correct code.
Line 1 Code
1
2
3
4
5
6
7
Task
Feedback: Task 1
Feed
ba ck
Task 2: Circle
Previously you made this program (or
Circumference
something similar), but now you have to use
functions.
Sample Output:
1. Make a program to calculate the
circumference of a circle Enter the radius: 3
2. The user should enter the radius and the The circumference of this circle is: 18.85
calculation should be performed and
output.
PROCEDURE getRadius()
radius <- 0
area <- 0
Sample Output:
OUTPUT “Enter the radius:
INPUT radius Enter the radius: 3
area <- calculateCircumference()
OUTPUT ”The circumference is” , area The circumference of this circle is: 18.85
END PROCEDURE
Call getRadius()
Task
Task 2: Circle
Circumference
1. Screenshot your code from the previous task below.
Task
Task 3: Rectangle
Previously you made this program (or
Perimeter
something similar), but now you have to use
functions.
Sample Output:
1. Make a program to calculate the
perimeter of a rectangle Enter the length of side 1: 10
Enter the length of side 1: 5
2. The user should enter the length of the
short side and the long side.
The perimeter off the rectangle is: 30
Call getLengths()
Task
Task 3: Rectangle
1. Screenshot your code from the previous task below.
Perimeter
Python Code Runtime
Task
Task 4: Convert Age to
Previously you made this program (or something
Days
similar), but now you have to use functions.
2. The program will calculate and output the How old are you in years?: 10
number of days and months separately
This is 3650 days.
3. Screenshot your code on the next available
slide This is 120 months.
Hints:
- Procedure:
getAge( )
- Function: Delete me for hints.
Task calculateAgeDays()
calculateAgeMonths()
Task 4: Convert Age to
Days
ageYears <- 0
PROCEDURE getAge()
ageDays <- 0
ageMonths <- 0
Sample Output:
OUTPUT “How old are you in years?”
INPUT ageYears How old are you in years?: 10
ageDays <- calculateAgeDays()
OUTPUT “This is “, ageDays, “days.” This is 3650 days.
//missing code.
END PROCEDURE
This is 120 months.
Task
Task 5: Maximum of Three
Numbers
Previously you made this program (or something
similar), but now you have to use functions.
Hints:
- Procedure:
Delete me for
getNumbers( hints.
).- could be 3
- Function:
Task calculateMaxNum( )
Task 5: Maximum of Three
Numbers
num1 <- 0
num2 <- 0
num3 <- 0
PROCEDURE getNumbers()
Sample Output:
maxNo <- 0
OUTPUT “Enter number 1” Enter number 1: 4
INPUT num1 Enter number 2: 5
//MISSING CODE Enter number 3: 6
maxNo <- calculateMaxNum()
The maximum number is: 6
//MISSING CODE
Task
Task 6: Student Grades
Previously you made this program (or something
similar), but now you have to use functions.
PROCEDURE getGrade()
//MISSING CODE
grade <- calculateMaxNum()
Sample Output:
//MISSING CODE
END PROCEDURE Enter the percentage: 81
FUNCTION calculateGrades( ) RETURNS STRING The grade this student received is: A
examGrade <- “”
if percentageMark > 0 and
percentageMark <29 THEN
examGrade <- “E”
Delete me for hints.
//MISSING CODE
END IF
return examGrade
END FUNCTION
Call getGrade()
Task
Task 6: Student Grades
1. Screenshot your code from the previous task below.
Task
Task 7: Weights Convertor
Previously you made this program (or something
similar), but now you have to use functions.
Sample Output:
1. Make a program asks for a weight in kilograms
Enter the weight in kilos: 100
2. The program will then calculate and output
the weight in pounds (lbs) and ounces (oz). This is 3527.4 ounzes
Or this is 220.46 lbs
1kg = 2.20 lbs
1lb = 16 ounces
Task
Task 7: Weights Convertor
1. Screenshot your code from the previous task below.
Task
Parameters and
Arguments
Instead of using global variables you can pass data from one routine to
another using parameters.
If you make a local variable in procedure you are able to pass this variable (or
many variables) to another procedure. In fact it can remove the need for global
variables.
A global variable should only be declared if many procedures need to read /
edit the data stored in this variable.
Teac
Expl her
anat
io
n
Parameters and
Arguments
Teac
Expl her
anat
io
n
Parameters and
Arguments
Teac
Expl her
anat
io
n
Parameters and
Arguments
Teac
Expl her
anat
io
n
Parameters and
The pseudoco
de fo
Arguments
noOfStars <- r this is:
0
Note, for a parameter
you have to state the PROCEDURE sta
rs(noOfStars:
data type. counter <- 0 INTEGER)
WHILE counter
< noOfStars D
OUTPUT ”*” O
counter<- cou
END WHILE nter + 1
END PROCEDURE
OUTPUT(“How m
any stars wou
INPUT noOfSta ld you like?”
Teac rs )
Expl her CALL stars(no
OfStars)
anat
io
n
Task 1: Fix the Errors
Below is the code to ask for someone’s age, which uses 1 parameter / argument. Uses the
yellow boxes to fix the errors. Add more yellow boxes if necessary.
Task
Feedback: Task 1
Feed
ba ck
Task 2a: @ Signs
1. Make a program that asks a user how many @ signs they would like to see.
Hints:
Three procedures:
getNoSigns()
DELETE ME FOR
outputSigns() HINTS
Task
Task 2a: @ Signs
Hints:
noSigns <-
//MISSING Code
Call ???
END PROCEDURE
Task
Task 2a: @ Signs
1. Screenshot your code from the previous slide below.
Task
Parameters and
Arguments
It is possible to send more than 1 argument at a time. These can be different
data types. This is very useful as sometimes you need 2 functions, or more,
need access to the same variables.
Teac
Expl her
anat
io
n
Parameters and
Arguments
1. I have added a new
variable
t h e u se r for
h en a d d I ask
2. I t es th e y w ant
lin
how many
Teac
Expl her
anat
io
n
Parameters and
Arguments
1. I have added a new
variable
t h e u se r for
h en a d d I ask
2. I t es th e y w ant
lin
how many
Teac
Expl her
anat
io
n
Parameters and
Arguments
1. I now required 2
arguments to run the stars
procedure.
an n o w s e n d both
Teac 2. I c
s o v e r to the
Expl her var ia ble
t co m m a
anat e d u re , ju s
io proc
n e p a r a te th e m.
s
The pseudoco
noOfStars <-
de for this is:
0
Parameters and
noOfLines <-
PROCEDURE sta
0
rs(noOfStars:
Arguments
INTEGER, noOf
counter2 <- 0 Lines: INTEGE
R)
while counter
2 < noOfLines
counter <- 0 DO
I know use the 2nd argument
WHILE counter
< noOfto
Strun
ars another
D
loop which
O
OUTPUT ”*” controls the number of lines.
counter<- cou
END WHILE nter + 1
OUTPUT “”
counter2 <- c
END WHILE ounter2 + 1
END PROCEDURE
OUTPUT(“How m
any stars wou
INPUT noOfSta ld you like?”
rs )
OUTPUT(“How m
any lines wou
INPUT noOfLin ld you like?”
es )
Teac CALL stars(no
Expl her OfStars, noOf
Lines)
anat
io
n
Task 2: @ Signs
1. Make a program that asks a user how many @ signs they would like to see in 1
line, it will also ask them how many lines they would like repeated.
Hints:
getLinesSigns( )
Delete me for hints.
outputSigns()
Task
Task 2: @ Signs
PROCEDURE outputSigns(noSigns: INTEGER,
noLines:INTEGER)
//MISSING CODE
Sample Output:
END PROCEDURE
How many @ signs do you want? 10
How many lines do you want to see? 5
PROCEDURE getLinesSigns()
noSigns <- 0
noLines <- 0 @@@@@@@@@@
@@@@@@@@@@
Delete me for hints.
OUTPUT “How many @ signs do you want?
INPUT noSigns
@@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@@
//missing code
Call getLinesSigns()
Task
Task 2: @ Signs
1. Screenshot your code from the previous slide below.
Task
Task 3: Temperature
1. Make a program that asks the
user what the temperature is in
Celsius.
Sample Output:
2. The program will convert this to
Farenheit and output this. What is the temperature (Celsius):
30
YOU MUST USE:
• 1 FUNCTION(S) + 1 This is 86 in Fahrenheit
PARAMETER
• 1 PROCEDURE
Hints:
- Procedure:
Delete me )for hints.
getTempC(
- Function:
calculateTempF()
Task
Task 3: Temperature
PROCEDURE calculateTempF(tempC: INTEGER):
tempF = 0
tempF = (tempC * 1.8) + 32 Sample Output:
OUTPUT ?????
END PROCEDURE
What is the temperature (Celsius):
PROCEDURE getTempC() 30
OUTPUT ”What is the temp
(celcius):” This is 86 in Fahrenheit
INPUT ????
DELETE ME FOR HINTS
Call ???????
END PROCEDURE
Call ????
Task
Task 3: Temperature
1. Screenshot your code from the previous slide below.
Task
Task 4: Is Divisible?
1. Make a program that asks the for
2 numbers
Hints:
- Procedure:
Delete me
getNumbers( ) for hints.
Task - Function:
checkIfItDivides( )
Task 4: Is Divisible?
PROCEDURE getNumbers( )
num1 = 0
num2 = 0
Task
Task 5: Remainder of Two
Make a program that:
Numbers?
1. Asks the user for two numbers
Sample Output:
2. The first number is divided by the
second. The remainder is What is the first number?: 29
calculated.
What is the second number?: 2
3. The remainder is output.
The remainder is 1.
Task
Task 5: Remainder of Two
1. Screenshot your code from the previous slide below.
Numbers?
Python Code Runtime
Task
Task 6: Higher or
1. Make a program that: Lower?
2. Creates a random number between 1 -100
Hints:
- Procedure:
guessNumber( )
Task Delete me for hints.
- Function:
isItCorrect( )
isItHigherOrLower( )
randomNum <- 0 //GLOBAL VARIABLE
Task 6: Higher or
correctGuess = FALSE
PROCEUDRE getRandomNum( )
randonNum <- ROUND(RANDOM( ) * 100,0)
Lower?
OUTPUT “Random Number Generated”
END PROCEDURE
Call getRandomNum( )
While correctGuess == False DO
guess <- guessNumber( )
Task correctGuess <- isItCorrect(guess)
if correctGuess != FALSE THEN
isItHigherOrLower(guess)
END IF
END WHILE
Task 6: Higher or
1. Screenshot your code from the previous slide below.
Lower?
Python Code Runtime
Task
1D Arrays
Vide
o
1D Arrays
Teac
Expl her
anat
io
n
1D Arrays
Presentation
Mode Needed
6
Teac
Expl her
anat
io
n
1D Arrays
Presentation
Mode Needed Index: 0 1 2 3 4 5
5
Teac
Expl her
anat
io
n
1D Arrays
Length:
6
Presentation
Mode Needed Index
0 1 2 3 4 5
:
Feed
ba ck
1D Arrays
To output th
e first eleme
array you wil nt in the
l do this..
Teac
Expl her Please note that some languages use 1-based arrays, and therefore the first location is
anat 1.
io
n
1D Arrays
Teac
Expl her
anat
io
n
Task 2a: Array Outputs
For each array code given below, state what the output will be:
Code food = [’Milk’, ‘Cheese’, ‘Apples’, ‘Bread’, ‘Bananas’]
print(food[0])
Output
Excel
ask
TCode food = [’Milk’, ‘Cheese’, ‘Apples’, ‘Bread’, ‘Bananas’]
print(food[len(food)-1])
Task Output
Feedback: Task 2
Feed
ba ck
1D Arrays
Presentation
Mode Needed
How could we output each of these items in the
array?
Pseudocode for this:
OUTPUT food[0]
We could m OUTPUT food[1]
a n ua l l y
Teac output eac OUTPUT food[2]
Expl her h item in th OUTPUT food[3]
anat e OUTPUT food[4]
n
io list…
1D Arrays
Is there a pattern to
this, which we could
use?
Teac
Expl her
anat
io
n
1D Arrays
Presentation
Mode Needed
ke a
1. We can easily ma
m 0 to 4…
counter to count fro
Teac
Expl her
anat
io
n
1D Arrays
Presentation
Mode Needed
6
Teac
Expl her
anat
io
n
1D Arrays
Presentation
Mode Needed Index: 0 1 2 3 4 5
5
Teac
Expl her
anat
io
n
1D Arrays
Length:
6
Presentation
Mode Needed Index
0 1 2 3 4 5
:
Feed
ba ck
1D Arrays
To output th
e first eleme
array you wil nt in the
l do this..
Teac
Expl her Please note that some languages use 1-based arrays, and therefore the first location is
anat 1.
io
n
1D Arrays
Teac
Expl her
anat
io
n
Task 2a: Array Outputs
For each array code given below, state what the output will be:
Code food = [’Milk’, ‘Cheese’, ‘Apples’, ‘Bread’, ‘Bananas’]
print(food[0])
Output
Excel
ask
TCode food = [’Milk’, ‘Cheese’, ‘Apples’, ‘Bread’, ‘Bananas’]
print(food[len(food)-1])
Task Output
Feedback: Task 2
Feed
ba ck
1D Arrays
Presentation
Mode Needed
How could we output each of these items in the
array?
Pseudocode for this:
OUTPUT food[0]
We could m OUTPUT food[1]
a n ua l l y
Teac output eac OUTPUT food[2]
Expl her h item in th OUTPUT food[3]
anat e OUTPUT food[4]
n
io list…
1D Arrays
Is there a pattern to
this, which we could
use?
Teac
Expl her
anat
io
n
1D Arrays
Presentation
Mode Needed
ke a
1. We can easily ma
m 0 to 4…
counter to count fro
Teac
Expl her
anat
io
n
1D Arrays
Presentation
Mode Needed
Pseudocode f
or this:
DECLARE food
as ARRAY[1..5]
STRING of
DECLARE coun
ter as INTEGER
counter <- 0
food <- [‘Milk’,
‘Cheese’, ‘App
‘Bananas’] les’, ’Bread’,
Excel
ask
TCode food = [’Milk’, ‘Cheese’, ‘Apples’, ‘Bread’, ‘Bananas’]
print(food[len(food)-1])
Task Output
Task 3a: Cars
1. Using the following array:
Iterate through the array and display each of the cars, with a count next
to them, use a WHILE loop to do this.
Sample Output:
2. Screenshot your code onto the next slide.
1. Mustang
Use 2. Dodge
• 1 Array 3. Nissan
• 1 Procedure 4. Ferrari
Task
Task 3: Cars
Hints:
PROCEDURE outputCars ()
counter <- 0
WHILE counter < LENGTH(cars) DO
OUTPUT cars[counter]
counter <- counter + 1
END WHILE Sample Output:
END PROCEDURE
Task
Task 3: Cars
1. Screenshot your code from the previous slide below.
Task
Task 3: Cars
2. Complete the pseudocode that matches the previous code.
Pseudocode
Task
Task 4: Numbers List
1. Using the following array:
numbers = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
Iterate through the array and display each of the numbers, with a count
next to them, use a for loop to do this.
Iterate through the array and display each of Sample Output:
the numbers, with a count next to them,
use a WHILE loop to do this. 1. 100
2. 200
…
2. Screenshot your code onto the next slide.10. 1000
Task
Task 4: Numbers List
Hints:
nums <- [100, 200, 300, 400, 500, 600, 700, 800, 900,
1000]
PROCEDURE outputNums ()
counter <- 0 Sample Output:
WHILE counter < LENGTH(nums) DO
OUTPUT nums[counter]
counter <- counter + 1 1. 100
END WHILE 2. 200
DELETE ME FOR HINTS
END PROCEDURE
…
CALL outputNums() 10. 1000
Task
Task 4: Numbers List
1. Screenshot your code from the previous slide below.
Task
1D Arrays
Presentation
Mode Needed
Teac
Expl her
anat
io
n
1D Arrays
Presentation
Mode Needed
These are
locations 0 and 1.
Somatter
No we canhowuse large
this
Teac
Expl her
the list,here…
the program
anat will output all items.
io
n
1D Arrays
Presentation
Mode Needed
Pseudocode f
or this:
DECLARE food
as ARRAY[1..5]
STRING of
DECLARE coun
ter as INTEGER
counter <- 0
food <- [‘Milk’,
‘Cheese’, ‘App
’Oranges’] les’,
Iterate through the array and find the largest and smallest from the range
Task
Task 5: Smallest and Largest
Hints:
minNo <- 0
maxNo <- 0
numbers <- [3, 41, 26, 12, 9, 74, 15]
PROCEDURE checkLargest()
counter <- 0
while counter < LENGTH(numbers) DO
//MISSING CODE
counter<- counter + 1
END WHILE
Sample Output:
END PROCEDURE
Largest Number: 74
PROCEDURE checkSmallest() Smallest Number: 3
//MISSING CODE
END PROCEDURE DELETE ME FOR HINTS
PROCEDURE outputLargest()
OUTPUT “The largest number is:”, maxNo
END PROCEDURE
PROCEDURE outputSmallest()
OUTPUT “The largest number is:”, maxNo
END PROCEDURE
CALL checkLargest()
Task CALL checkSmallest()
CALL outputLargest()
CALL outputSmallest()
Task 5: Smallest and Largest
1. Screenshot your code from the previous slide below.
Task
Task 6: Adding Numbers to a
List
1. Write a program that will ask for ten numbers, saves these into an array
and then print out their sum (the total must be calculated by iterating
through the array.
Sample Output:
Enter number 1: 5
2. Screenshot your code onto the next Enter number 2: 10
Enter number 3: 15
slide. Enter number 4: 20
Enter number 5: 25
Enter number 6: 30
Use 3 procedures min Enter number 7: 35
Enter number 8: 40
Enter number 9: 45
Enter number 10: 50
Task
Task 6: Adding Numbers to a
Hints:
List
Numbers <- []
total <- 0
Task
String as an Array
A string is a array of characters (1 letter / 1 number / 1
symbol)
0 1 2 3 4
H E L L O
Task
Task 7: String as a List
1. Write a Python program which accepts the user's first and last name
and print them in reverse order with a space between them.
rm renniks
Task
Task 7: String as a List
Hints:
firstName <-””
lastName <- “”
PROCEDURE getFirstName( )
OUTPUT “Enter first name:”
INPUT firstName
Sample Output:
END PROCEDURE
Enter first name: Mr
PROCEDURE getLastName( ) Enter second name: Skinner
DELETE ME FOR HINTS
OUTPUT “Enter first name:”
Your name backwards:
INPUT lastName
END PROCEDURE
rm renniks
PROCEDURE outputNamesBackwards()
counter <- 0
counter <- LENGTH(firstName)
while counter > 0 DO
Task OUTPUT firstName[counter]
//CODE MISSING
END PROCEDURE
Task 7: String as a List
1. Screenshot your code from the previous slide below.
Task
Task 8: Quotes
1. Add the quotes below to a list / array. Make a program which outputs a
random quote.
Task
Task 8: Quotes
Hints:
Quotes <- [” Fools use tomorrow, wise use
today”, …]
randomNo <- 0
PROCEDURE getRandomNo()
//MISSING CODE Sample Output:
END PROCEDURE Fools use tomorrow, wise use
today
PROCEDURE getQuote()
DELETE ME FOR HINTS
OUTPUT quotes[randomNo]
END PROCEDURE
Would you like another quote
Y/N: Y
WHILE choice =“Y” DO
OUTPUT ”Would you like another quote? Grasp opportunities to create the
Y/N” future
INPUT choice
//MISSING CODE
END WHILE
Task
Task 8: Quotes
1. Screenshot your code from the previous slide below.
Task
Task 9: Bingo
1. Create a bingo program which picks a random number and calls it out, then stores this in
an array. After each new number is displayed, the lists of all the previous numbers are
displayed on screen. The program will always ask the user if they want a new number to
appear, or not (someone has won the game). It will need to generate a random number.
Bingo Ball: 22
Bingo Ball: 33
Task
Hints:
chosenBalls <- []
bingoBalls <- [1,2,3…100] Task 9: Bingo
choice <- ”Y”
chosenBallIndex <- 0
PROCEDURE getBall()
randomNo <- 0
ballOK <- FALSE
//missing code
WHILE ballOK = FALSE DO
chosenBall <- bingoBall[randomNo)
ballOK <- checkBall(chosenBall) Sample Output:
END WHILE
chosenBalls[chosenBallIndex] <- chosenBall Bingo Ball: 22
DELETE ME FOR HINTS
OUTPUT chosenBall
END PROCEDURE
Would you like another go
FUNCTION checkBall(chosenBall): RETURNS Boolean Y/N? Y
counter = 0
WHILE COUNTER < LENGTH(chosenBalls) DO Bingo Ball: 33
if chosenBalls[counter] =
chosenBall THEN
Found <- True
END IF
counter <- counter + 1
END WHILE
Task RETURN Found
END FUNCTION
Task
2D Arrays
Presentation
So far you have learnt 1D Arrays (lists)
Mode Needed
But what for each item in an array was…another array
0 1 2 2
0 David 0
A B C D
1
1 Jean 2 E F G H
2 James 3
I J K L
4
3 Frank M N O P
Teac
4 Peaches Q R S T
Expl her
anat
io
n
2D Arrays
Teac
Expl her
anat
io
n
2D Arrays
Teac
Expl her
anat
io
n
2D Arrays
Teac
Expl her
anat
io
n
2D Arrays
top5Teams = [ [‘Man City’, ‘Man Utd’, ‘Liverpool’,
Presentation ‘Chelsea’, ‘Leicester City’] , [Athletico Madrid], [‘Real
Mode Needed Madrid’, ’Barcelona’,’Sevilla’, ‘Real Sociedad’] ,
[‘LOSC’, ‘PSG’, ‘Monaco’, ‘Lyon’, ‘Marseille’] ]
0 1 2 3 4
0
Answer the questions 1 about the22D array structure.
below 3 4
0
Wales England Switzerlan Turkey
1 d
2 Belgium Finland Russia Denmark
Task
Task 2: Create a 2D Array
1. Below is a 2D array which contains numbers and a few erroneous letters.
2. Make a python program which has this array – declare the array as it is below.
0
1 “A” 3 4
1
2 5 6 “B” 8
Task
9 “C” 11 12
Task 2: Create a 2D Array
1. Screenshot below your solution to the previous task.
Task
Feedback: Task 2
Feed
ba ck
Simple 2D array Output Loop
Presentation
Mode Needed
We need to iterate through each part of the number array. Such as index 0, 1 or
2.
Numbers[1][?]
Numbers[0][?]
Teac
Expl her
anat
io
n
Simple 2D array Output Loop
Presentation
Mode Needed
We need to iterate through each part of the number array. Such as index 0, 1, or
2.
Numbers[1][?]
Numbers[2][?]
Numbers[0][?]
Teac
Expl her
anat
io
n
Simple 2D array Output Loop
Presentation
Mode Needed
But we also need to iterate through each array inside the ‘main array’.
Teac
Expl her
anat
io
n
Simple 2D array Output Loop
Presentation
Mode Needed
But we also need to iterate through each array inside the ‘main array’.
Teac
Expl her
anat
io
n
Simple 2D array Output Loop
Presentation
Mode Needed
But we also need to iterate through each array inside the ‘main array’.
Teac
Expl her
anat
io
n
Simple 2D array Output Loop
Presentation
Mode Needed
But we also need to iterate through each array inside the ‘main array’.
Teac
Expl her
anat
io
n
Simple 2D array Output Loop
Presentation
Mode Needed
But we also need to iterate through each array inside the ‘main array’.
Teac
Expl her
anat
io
n
Simple 2D array Output Loop
Presentation
Mode Needed
But we also need to iterate through each array inside the ‘main array’.
Teac
Expl her
anat
io
n
Simple 2D array Output Loop
Presentation
Mode Needed
But we also need to iterate through each array inside the ‘main array’.
Teac
Expl her
anat
io
n
Simple 2D array Output Loop
Presentation
Mode Needed
But we also need to iterate through each array inside the ‘main array’.
Teac
Expl her
anat
io
n
Simple 2D array Output Loop
Presentation
Mode Needed
So to iterate through the ‘main array’, you would use the following code:
counter1 = 0
while counter1 < len(numbers):
//some more code soon….
counter1 = counter1 + 1
Teac
Expl her
anat
io
n
Simple 2D array Output Loop
Presentation
Mode Needed
And to iterate through the ‘minor array(s)’ you would add the following.
counter1 = 0
counter2 = 0
while counter1 < len(numbers):
while counter < len(numbers[counter1]):
//OUTPUT the value
counter2 = counter2 + 1
Teac
Expl her
counter1 = counter1 + 1
anat
io
n
Simple 2D array Output Loop
Presentation
Mode Needed
And to iterate through the ‘minor array(s)’ you would add the following.
counter1 = 0
How do we output each item in
counter2 = 0 the list?
while counter1 < len(numbers):
while counter < len(numbers[counter1]):
//OUTPUT the value Here
counter2 = counter2 + 1
Teac
Expl her
counter1 = counter1 + 1
anat
io
n
Simple 2D array Output Loop
Presentation
Mode Needed
And to iterate through the ‘minor array(s)’ you would add the following.
counter1 = 0
counter2 = 0
while counter1 < len(numbers):
while counter < len(numbers[counter1]):
//OUTPUT the value Here
counter2 = counter2 + 1
Teac
Expl her
counter1 = counter1 + 1
anat
io print(numbers[counter1][counter2])
n
Simple 2D array Output Loop
Presentation
Mode Needed
And to iterate through the ‘minor array(s)’ you would add the following.
counter1 = 0
Why only 1, 2,3, and
counter2 = 0 4?
while counter1 < len(numbers):
while counter < len(numbers[counter1]):
print(numbers[counter1][counter2])
counter2 = counter2 + 1
Teac
Expl her
counter1 = counter1 + 1
anat
io
n
Simple 2D array Output Loop
Presentation , 4.
Mode Needed 2,3
g 1,
t ti n 3
t pu =
o u r 2 ua l t o3
er e is eq
ft u nt r 1 ]
A co co unte
rs
bewould [
And to iterate through the ‘minor array(s)’ ( n u m
you add the following.
len
So th
counter1 = 0 e wh
i l e lo
op is
counter2 = 0 effec
tively
while counter1 < len(numbers): whil askin
e 3 g?
A s th
while counter < len(numbers[counter1]): < 3?
i s is f
print(numbers[counter1][counter2]) a l se
t he i n
counter2 = counter2 + 1 ne
Teac again r loop nev
… er ru
Expl her
counter1 = counter1 + 1
ns
anat So to
io fix it…
n
Simple 2D array Output Loop
Presentation
Mode Needed
I reset counter2.
And to iterate through the ‘minor array(s)’ you would add the following.
counter1 = 0
counter2 = 0
while counter1 < len(numbers):
while counter < len(numbers[counter1]):
print(numbers[counter1][counter2])
counter2 = counter2 + 1
Teac
Expl her
counter1 = counter1 + 1
anat
io
n
Simple 2D array Output Loop
Presentation
Mode Needed Pseudocode for this is
Numbers<-[[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Counter1 <- 0
Counter2 <- 0
while counter 1 < LENGTH(numbers) DO
counter2 <- 0
while counter 2 < LENGTH(numbers [counter
1]) DO
OUTPUT(numbers[counter 1,counter 2])
Teac counter2 <- counter2 + 1
Expl her END WHILE
anat
io counter 1 <- counter1 + 1
n
END WHILE
Task 3: Create a 2D Array
Adapt your program from task 1, so it now does the following (new additions in
red).
1. Make a python program which has this array – declare the array as it is
below.
3. Then make the following changes to the array, using programmatic statements.
• Change the letter ”A” to a 2.
• Change the letter “B” to 7
• Change the letter “C” to 10
4. Output the array with the updated contents.
0 1 2 3
5. Output the array in full again.
0
1 “A” 3 4
6. Screenshot onto the next slide.
1
2 5 6 “B” 8
Task
9 “C” 11 12
Task 3: Create a 2D Array
1. Screenshot your code from the previous slide below.
Task
Task 3: Create a 2D Array
2. Write the pseudocode for the array from the previous tasks.
Pseudocode
Task
Task 4: Create a 2D Array
1. Write a Python which has a pre-
made 2D-array on it, the footballers Sample Output:
and their stats (career goals, career
Name: Messi
yellow cards, career red cards).
Total Career Goals: 450
Total Career Yellow Cards: 21
2. The program should output each Total Career Red Cards: 5
players name and stats
Use 1 procedure(s) 0 1 2 3
0
“Messi” 450 21 5
1
2 “Ronaldo
”
800 45 8
Task
“Kane” “390” 11 4
Task 4: Create a 2D Array
Hints:
counter2 <- 0
WHILE
counter2<LENGTH(players[counter1) DO
OUTPUT players[counter1,
counter2]
counter2<- counter2 + 1
END WHILE
counter1 <- counter1 + 1
Task END WHILE
END PROCEDURE
Task 3: Create a 2D Array
1. Screenshot your code from the previous slide below.
Task
Task 4a: Cinema Booking
1. Write a Python which has an empty
array of 4 x 5 elements
Use 1 procedure(s)
Use 1 function(s)
Task
Task 4a: Cinema Booking
Hints:
RETURNS boolean
empty <- False Sample Output:
IF seats[row, seat] == “X” THEN
empty <- True
END IF Which row: 5
RETURN empty Which seat: 1
DELETE ME FOR HINTS
END FUNCTION
Seat Booked
PROCEDURE bookSeat() Have a nice day.
OUTPUT “Which row?”:
INPUT row
OUTPUT ”Which seat?”
INPUT seat
seatEmpty <- checkSeats(row, seat)
if seatEmpty == TRUE THEN
Task OUTPUT “Seat Booked”
Else THEN
OUTPUT “Seat already booked”
END IF
Task 4a: Cinema Booking
1. Screenshot your code from the previous slide below.
Task
Task 4b: Cinema Booking
1. Extend the program you made in
task 4a to output the seats of the
cinema.
2. You can use any key you would like Sample Output:
the example shows “X” for free and
”B” for booked. XXXXX
XXBXX
XXXXX
3. Make a menu to allow a user to
XBBXX
either output the seats or book a XXXXX
seat.
Use 1 procedure(s)
Task
Task 4b: Cinema Booking
Hints:
PROCEDURE outputSeats( )
counter1<-0 Sample Output:
counter2<-0
while counter1 < LENGTH(seats) DO
counter2 <- 0
XXXXX
XXBXX
DELETE ME FOR HINTS
WHILE
counter2<LENGTH(seats[counter1) DO XXXXX
OUTPUT seats[counter1, XBBXX
counter2] XXXXX
counter2<- counter2 + 1
END WHILE
counter1 <- counter1 + 1
END WHILE
END PROCEDURE
Task
Task 4b: Cinema Booking
1. Screenshot your code from the previous slide below.
Task
Task 5a: Tic Tac Toe
1. Create a program that allows two
players to play tic-tac-toe (noughts
and crosses)
Task
Task 5a: Tic Tac Toe
Hints:
PROCEDURE outputTicTacToe( )
counter1<-0 Sample Output:
counter2<-0
while counter1 < LENGTH(seats) DO
counter2 <- 0 EEE
WHILE
counter2<LENGTH(TTT[counter1) DO EEE
DELETE ME FOR HINTS OUTPUT TTT[counter1,
counter2] EEE
counter2<- counter2 + 1
END WHILE
counter1 <- counter1 + 1
END WHILE
END PROCEDURE
Task
Task 5a: Tic Tac Toe
1. Screenshot your code from the previous slide below.
Task
Linear Search
Presentation
Mode Needed
, w h a t w o uld
h / list would be tob e
A really useful functionth
o a ctu a
toeimplementl s a
ineanrc
array
wetodsee if an item is in there or not.
orearray
Befthe
need to do?
search
Teac
ExpTlaskher
anat
io
n
Linear Search
Presentation Firstly you would need to asks the user what they want to
Mode Needed search for.
Teac
ExpTlaskher
anat
io
n
Linear Search
Presentation
Mode Needed
Teac
ExpTlaskher
anat
io
n
Linear Search
Presentation
Mode Needed
I have my basic lo
op which
will iterate through
Teac the array.
ExpTlaskher
anat
io
n
Linear Search
Presentation
Mode Needed
a l i.e. th e items
e do if th ey a re equ
What should w
is found.
Presentation
Mode Needed
= T r ue t o tell the
w e u se th e f ou nd
How could is fo u nd o r n o t?
user if the item s
Teac
ExpTlaskher
anat
io
n
Linear Search
Presentation
Mode Needed
a te m e n t, if fou nd
d d a s im ple if s t
u c a n a ther
After the loop yo u tp u t “ Item F o u n d ” or a n y o
e c a n o
== True – w ria te m e ss a g e.
ap p ro p
Teac
ExpTlaskher
anat
io
n
Linear Search
Counter =
0
Presentation
Mode Needed
index 0 1 2 3 4
If the user enters ‘Bread’ the program we made will start at the first
index of the array.
‘Bread’ 'Milk’
Teac
ExpTlaskher
anat
io
n
Linear Search
Counter =
1
Presentation
Mode Needed
index 0 1 2 3 4
If the user enters ‘Bread’ the program we made will start at the first
index of the array.
‘Cheese
‘Bread’
’
Teac
ExpTlaskher
anat
io
n
Linear Search
Counter =
2
Presentation
Mode Needed
index 0 1 2 3 4
If the user enters ‘Bread’ the program we made will start at the first
index of the array.
‘Bread’ ‘Apples’
Teac
ExpTlaskher
anat
io
n
Linear Search
Counter =
3
Presentation
Mode Needed
index 0 1 2 3 4
If the user enters ‘Bread’ the program we made will start at the first
index of the array.
‘Bread’ ‘Bread’
Teac
ExpTlaskher
anat
io
n
Linear Search
Counter =
4
Presentation
Mode Needed
index 0 1 2 3 4
If the user enters ‘Bread’ the program we made will start at the first
index of the array.
‘Bananas
‘Bread’
’
Teac
ExpTlaskher
anat
io
n
Linear Search
Counter =
4
Presentation
Mode Needed
les s if th e ite m s is
co n t in u e s r e g a rd
T h e loop still
index 0 1 found2. 3 4
u e .
m s it w o u ld c o n tin
1 00 m o r e it e
If th ere were o n ce t h e it e m s is
If the user enters ‘Bread’
ld w e s t o p
the th e
programlo opwe made will start at the first
How cou index of the array.
found.
‘Bananas
‘Bread’
’
Teac
ExpTlaskher
anat
io
n
Linear Search
Presentation
Mode Needed
t o c o n t rol this
n ee d t o be able e i t em is
We n t h
so i t st o ps whe
l oo p found.
By ad
di n g
need t hi s a
to be nd b
TRUE oth sides
Once f or t h o f th
found e loop is an
to r u d
Teac = TR n .
U E, t
ExpTlaskher he l o
op w
anat ill en
io d.
n
Task 1: Searching Array
Task
Bubble Sort
Presentation
Mode Needed
8 5 3 1
Consider this array – how could we sort
it?
Teac
ExpTlaskher
anat
io
n
Bubble Sort
Presentation
Mode Needed
8 5 3 1
In a bubble sort you:
1. start with the first number.
2. check it is in the correct order (with next number)
3. if not you swap them.
4. Repeat 2 - 3 until number is in correct location.
5. Repeat 1 – 4 until all numbers are sorted.
Teac
ExpTlaskher
anat
io
n
Bubble Sort
Presentation
Mode Needed
8 5 3 1
h em
a p
Are these two t
numbers in order?
SSowwhatushould
! !
d be do?
o n
Teac
a r
ExpTlaskher
anat
io
n
Bubble Sort
Presentation
Mode Needed
5 8 3 1
h em
a p
Are these two t
numbers in order?
SSowwhatushould
! !
d be do?
o n
Teac
a r
ExpTlaskher
anat
io
n
Bubble Sort
Presentation
Mode Needed
5 8 3 1
h em
a p
Are these two t
numbers in order?
SSowwhatushould
! !
d be do?
o n
Teac
a r
ExpTlaskher
anat
io
n
Bubble Sort
Presentation
Mode Needed
5 3 8 1
h em
a p
Are these two t
numbers in order?
SSowwhatushould
! !
d be do?
o n
Teac
a r
ExpTlaskher
anat
io
n
Bubble Sort
Presentation
Mode Needed
5 3 8 1
e m
Are these two numbers in order?
h
Pass 1a p t
Complete
S w
So what should
d !
be!do?
o u n
Teac
ExpTlaskher
anat
io
ar
n
Bubble Sort
Presentation
Mode Needed
5 3 1 8
And so we continue… until all numbers are in
order.
The sort gets it name as the largest unsorted
Teac number will float like a bubble up the array.
ExpTlaskher
anat
io
n
Bubble Sort
Presentation
Mode Needed
3 5 1 8
And so we continue… until all numbers are in
order.
The sort gets it name as the largest unsorted
Teac number will float like a bubble up the array.
ExpTlaskher
anat
io
n
Bubble Sort
Presentation
Mode Needed
3 1 5 8
And so we continue… until all numbers are in
Pa s s 2
order.
p l e te
The sort gets it name as the largest unsorted
m
Teac Co
number will float like a bubble up the array.
ExpTlaskher
anat
io
n
Bubble Sort
Presentation
Mode Needed
3 1 5 8
And so we continue… until all numbers are in
order.
The sort gets it name as the largest unsorted
Teac number will float like a bubble up the array.
ExpTlaskher
anat
io
n
Bubble Sort
Presentation
Mode Needed
1 3 5 8
And so we continue… until all numbers are in
s s
Saorted
P 3
order.
p l e te
The sort gets it name as the largest unsorted
m
Teac Co
number will float like a bubble up the array.
ExpTlaskher
anat
io
n
Task 2 : Bubble Sort
numbers = [7,8,5,2,4,6,3]
Sample Output:
1. Using the above list of numbers
create a program that will perform The sorted array:
2 3 4 5 6 7 8
a bubble sort.