0% found this document useful (0 votes)
41 views237 pages

Python - Arrays, Procedures & Functions (Inc Linear & Bubble) - Student

The document outlines programming concepts related to procedures, functions, and arrays, including tasks for creating programs that utilize these concepts. It provides examples of pseudocode, input/output processes, and specific programming tasks such as calculating the area of a triangle and determining if a number is odd or even. Additionally, it emphasizes the importance of using procedures and functions to organize code effectively.

Uploaded by

sm0004837
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views237 pages

Python - Arrays, Procedures & Functions (Inc Linear & Bubble) - Student

The document outlines programming concepts related to procedures, functions, and arrays, including tasks for creating programs that utilize these concepts. It provides examples of pseudocode, input/output processes, and specific programming tasks such as calculating the area of a triangle and determining if a number is odd or even. Additionally, it emphasizes the importance of using procedures and functions to organize code effectively.

Uploaded by

sm0004837
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 237

8.

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.

Variable and Pseudocode to DECLARE


Details
1D Array called Toys
Length: 5
Type: String

2D Array called Tasks


Length: 5 x 5
Type: String

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.

The program below just has input and output.

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()

2. To call a procedure you


Teac
h
Expl er simply just write the name…
anat
i
on
Procedures

Presentation
Mode Needed

So how would I call


outputName() ?

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?

Yes its perfectly fine – but


they are different variables

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.

1. Make a program which calculates the


area of a triangle. Sample Output:
It should ask for the base and the height Enter base of triangle: 10
and output the answer. Enter Height of triangle: 5
YOU MUST USE The area of your triangle is: 25
• 4 PROCEDURES MIN.

2. Screenshot your code onto next slide but


one.

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.

1. Make a program which calculates if a


number is odd or even.
Sample Output:
YOU MUST USE:
Enter a number: 5
• 3 PROCEDURES MIN.
The number is odd.
2. Screenshot your code onto the next slide.

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.

Python Code Runtime

Task
Task 5: Odd or Even 2
Previously you made this program, but now you have to use procedures.

1. Make a program which calculates if a


number is odd or even. Sample Output:
YOU MUST USE : Enter a number: 5
• 3 PROCEDURES MIN.
The number is odd.
2. Screenshot your code onto the next slide.

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.

Python Code Runtime

Task
Task 5: Head and Tails 2
Previously you made this program, but now you have to use procedures.

1. Make a program which calculates


simulates the number of times a coin Sample Output:
will get heads or tails
Heads and Tails Simulator
The program should output the
number of heads and tails. Number of Heads: 20
Humber of Tails: 30
YOU MUST USE 3 PROCEDURES MIN.

2. Screenshot your code onto the next slide


but one.

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

while counter < 0:


CALL simulateCoin()
CALL checkHeadTails
counter <- counter + 1
Task CALL outputHeadsTailsCount()
Task 5: Head and Tails 2
1. Screenshot your code from the previous slide below.

Python Code Runtime

Task
Task 6: Calculator Part
2i
Make a calculator program which allows the basics functions of a calculator: add,
subtract, multiply and divide.

YOU MUST USE 7 PROCEDURES MIN Sample Output:


Enter Number 1: 6
2. Screenshot your code onto the next slide.
Enter Number 2: 10
Press 1 to add
Press 2 to subtract
Hints: Press 3 to divide
Seven procedures: Press 4 to Multiply.
Please make a choice: 1
getNum1( ) The answer is: 16
getNum2( )
DELETE add(
ME )
FOR
subtract( )
HINTS
multiply( )
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.

It is the same as a procedure, apart from it returns a value(s).

This will return the value ‘name’


Teac to whever getName( ) was
Expl her called.
anat
io
n
Functions

2. The procedure is run.

3. It asks for the user’s name


and the input

4. Returns the value in name


to the caller…

How can we remember the


5. But when it gets there it
1. This calldata in name?
statement called
isn’t saved to anything…
getName( )
Teac and just disappears.
Expl her
anat
io
n
Functions
How can we remember the
data in name?

HINT: Could use a variable.

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

Could we call this variable


“name’ as well?

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.

YOU MUST USE


• 1 Procedure
• 1 Function
Hints:
- Procedure:
getRadius( )
Delete me for hints.
- Function:
Task calculateCircumference()
Task 2: Circle
Circumference
radius <- 0

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

FUNCTION calculateCircumference()RETURNS INTEGER


area <- 2 * 3.142 * radius
Delete me for hints.
return area
END FUNCTION

Call getRadius()

Task
Task 2: Circle
Circumference
1. Screenshot your code from the previous task below.

Python Code Runtime

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

3. The perimeter should be calculated and


output.

YOU MUST USE


• 1 Procedure
• 1 Function
Hints:
- Procedure:
Task Delete me for
getLengths( ) hints.
- Function:
calculatePerimeter()
Task 3: Rectangle
Perimeter
length1 <- 0
length2 <- 0 Sample Output:
PROCEDURE getLengths() Enter the length of side 1: 10
OUTPUT “Enter the 1st Length” Enter the length of side 1: 5
INPUT length1
//missing code
perimeter <- calculatePerimeter()
OUTPUT “Perimeter off the rectangle is The perimeter off the rectangle is: 30
“, perimeter
Delete me for hints.
END PROCEDURE

FUNCTION calculatePerimeter()RETURNS INTEGER


perimeter <- 0
// missing code
return ????
END FUNCTION

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.

1. Make a program that asks a user to input their


age in years. Sample Output:

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.

YOU MUST USE


• 1 Procedure min
• 2 Functions min

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.

Delete me for hints.


FUNCTION calculateAgeDays() RETURNS INTEGER
//missing code
return ageMonthsTemp
END FUNCTION

FUNCTION calculateAgeMonths() RETURNS INTEGER


//missing code
END FUNCTION

Task Call getAge()


Task 4: Convert Age to
1. Screenshot your code from the previous task below.
Days
Python Code Runtime

Task
Task 5: Maximum of Three
Numbers
Previously you made this program (or something
similar), but now you have to use functions.

1. Make a program that asks a user to input


three numbers Sample Output:

2. The program should output the largest of the Enter number 1: 4


three numbers Enter number 2: 5
Enter number 3: 6
3. Screenshot your code on the next available
slide The maximum number is: 6

YOU MUST USE


• 1 Procedure min
• 1 Functions min

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

Delete me for hints.


END PROCEDURE

FUNCTION calculateMaxNum( ) RETURNS INTEGER


maxNo <- 0
if num1 > num2 and num1 > num3 THEN
maxNo <- num1
END IF
//MISSING CODE
return maxNo
END FUNCTION

Task Call getNumbers()


Task 5: Maximum of Three
1. Screenshot your code from the previous task below.
Numbers
Python Code Runtime

Task
Task 6: Student Grades
Previously you made this program (or something
similar), but now you have to use functions.

1. Make a program that asks a to input a


percentage for exam grades. Sample Output:
2. The program should output the grade. Enter the percentage: 81
0% – 29% - E
30% - 49% - D The grade this student received is: A
50% – 69% - C
70% - 79% - B
80% - 89% - A
90% - 100% - A*
3. Screenshot your code on the next available
slide

YOU MUST USE


• 1 Procedure min
• 1 Functions min
Hints:
- Procedure:
Task Delete me for hints.
getGrade()
- Function:
calculateGrade( )
Task 6: Student Grades
percentageMark <- 0

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.

Python Code Runtime

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

Or 1kg = 3527.4 ounces

Task
Task 7: Weights Convertor
1. Screenshot your code from the previous task below.

Python Code Runtime

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

Here we have a procedure


which will output a certain
amount of stars.

I want the user to control the


number of stars that are
printed.

Teac
Expl her
anat
io
n
Parameters and
Arguments

So I am going to call the procedure stars


and let it know how many stars I want to
see...
Teac
Expl her This is an argument. You can pass in as
anat many as you want, separated by a
io
n comma
Parameters and
Arguments
noOfStars =
10
This will accept 1 piece
of data…this is called a
parameter

…when a call is made


Teac
Expl her
anat
we now pass this
io
n argument.
Parameters and
Arguments
What could we put here to
control the loop?

Teac
Expl her
anat
io
n
Parameters and
Arguments

What could we add to allow


the user to enter the number
of stars they want to see.

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.

The program will output the desired


number of @ signs. Sample Output:

How many @ signs do you want? 10


YOU MUST USE:
• 2 Procedures(s) @@@@@@@@@@
• 1 Parameter / Argument

Hints:
Three procedures:
getNoSigns()
DELETE ME FOR
outputSigns() HINTS
Task
Task 2a: @ Signs
Hints:

noSigns <-

PROCEDURE outputSigns(noSigns) Sample Output:


counter <- 0
while counter < noSigns DO
//MISSING CODE How many @ signs do you want? 10
counter <- counter + 1
END PROCEDURE @@@@@@@@@@
PROCEDURE getNoSigns()

DELETE ME FOR HINTS


want?”
OUTPUT “How many @ signs do you

//MISSING Code
Call ???
END PROCEDURE

Task
Task 2a: @ Signs
1. Screenshot your code from the previous slide below.

Python Code Runtime

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.

How many * would you like? 10


How many lines would you like to see? 5 I would like to
**********
**********
extend this program
**********
********** so that it prints a
**********
number of lines.

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.

The program will output the desired


Sample Output:
number of @ signs and repeated number
of lines. How many @ signs do you want? 10
How many lines do you want to see? 5
YOU MUST USE:
@@@@@@@@@@
• 2 PROCEDURE(S) @@@@@@@@@@
• 2 PARAMETER(S) @@@@@@@@@@
@@@@@@@@@@
@@@@@@@@@@

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 outputSigns(noSigns, noLines)


END PROCEDURE

Call getLinesSigns()

Task
Task 2: @ Signs
1. Screenshot your code from the previous slide below.

Python Code Runtime

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.

Python Code Runtime

Task
Task 4: Is Divisible?
1. Make a program that asks the for
2 numbers

2. The program will check if the 1st


Sample Output:
number is divisible by the 2nd
What is num1? : 50
3. An appropriate output will be What is num2? : 2
given to say of the numbers are
divisible or not. 50 can be divided by 2.

YOU MUST USE:


• 2 PROCEDURES(S)
• 2 PARAMETER(S)

Hints:
- Procedure:
Delete me
getNumbers( ) for hints.
Task - Function:
checkIfItDivides( )
Task 4: Is Divisible?
PROCEDURE getNumbers( )
num1 = 0
num2 = 0

OUTPUT “What is num1:?” Sample Output:


INPUT num1
What is num1? : 50
//MISSING CODE
What is num2? : 2
Call checkIfItDivides(num1, num2)
50 can be divided by 2.
END PROCEDURE

Delete me for hints.


PROCEDURE checkIfItDivides(num1, num2:
INTEGER)
answer = num1 MOD num2
if answer == 0 THEN
OUTPUT num1 , “can be
divided by”, num2
//MISSING CODE
END IF
END PROCEDURE
Task
Call getNumbers()
Task 4: Is Divisible?
1. Screenshot your code from the previous slide below.

Python Code Runtime

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.

YOU MUST USE:


• 1 PROCEDURE(S)
• 1 FUNCTION(S)
• 2 PARAMETER(S)
Hints:
- Procedure:
Delete me for
getNumbers( ) hints.
Task - Function:
getRemainders()
Task 5: Remainder of Two
PROCEDURE getNumbers( )
Numbers?
num1 <- 0
num2 <- 0
answer <- 0
Sample Output:
OUTPUT “What is the first number?”
INPUT num1
What is the first number?: 29
OUTPUT “What is the second number?”
INPUT num2 What is the second number?: 2

answer <- getRemainders(?????) The remainder is 1.


//MISSING CODE
Delete me for hints.
END PROCEDURE

FUNCTION getRemainders(num1, num2: INTEGER)


answer <- 0
answer <- num1 MOD num2
RETURN(????)
END FUNCTION

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

3. Asks the user to guess the number Sample Output:


4. Outputs whether the user is either:
• New Number Guessed
Correct
• Incorrect - Too high
• What do you think the numbers is: 10
Incorrect – Too low
• Too low
Allow the user to guess again.
What do you think the numbers is: 90
Too high
YOU MUST USE:
• 1 PROCEDURE(S) What do you think the number is: 87
• Correct
FUNCTION(S)
• 1 GLOBAL VARIABLES
• 1 PARAMETER(S)

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

FUNCTION guessNumber( ) Sample Output:


OUTPUT “What do you think the number is
INPUT guess
New Number Guessed
return guess
END FUNCTION
What do you think the numbers is: 10
Too low
FUNCTION isItCorrect(numGuessed: INTEGER) What do you think the numbers is: 90
Delete me for hints.
//MISSING CODE
RETURN??
Too high
What do you think the number is: 87
END FUNCTION
Correct
FUNCTION isItHigherOrLower(numGuessed: INTEGER)
//MISSING CODE
RETURN??
END FUNCTION

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

Here I have declared an array called


myList
And assigned 1,2…6 to it.
Teac
Expl her
anat
io
n
1D Arrays

The first location is in location 0.


This is called an index.
e fi r st location is
s, a nd therefore th
y
s use 1 - based arra
a t som e language 1.
se note th
Teac Plea
Expl her
anat
io
n
1D Arrays

The second location is in location 1.

This is called an index.

Teac
Expl her
anat
io
n
1D Arrays
Presentation
Mode Needed

How many items are in my array?

6
Teac
Expl her
anat
io
n
1D Arrays
Presentation
Mode Needed Index: 0 1 2 3 4 5

What is the last index?

5
Teac
Expl her
anat
io
n
1D Arrays
Length:
6
Presentation
Mode Needed Index
0 1 2 3 4 5
:

So what is relationship between the length of an array and


the last index?

The last index is always the size of the


Teac array - 1
Expl her
anat
io
n
Task 1: Lengths of Array /
Indices
For each array given below state the length and the index for the last
item.
Array: cars = ['Mustang', 'Dodge', 'Nissan', 'Ferrari’]
Length: Index of Last
Item:

Array: dogs = [‘Max', ‘Fido', ‘Rover']


Length: Index of Last
Item:

Array: numbers = [10,20,30,40,50]


Length: Index of Last
Task Item:
Feedback: Task 1

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

Line 2 assigns location 0 to


100.

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

Code food = [’Milk’, ‘Cheese’, ‘Apples’, ‘Bread’, ‘Bananas’]


print(food[3])
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:

DECLARE food as ARRAY[1..5]


of STRING

food <- [‘Milk’, ‘Cheese’,


‘Apples’, ’Bread’, ‘Bananas’]

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

But we would have to keep recoding- if the array


changed size, so is there another method.
Could we use a loop?

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

How could we use the counter to access the items


in the array?
Teac
Expl her
anat
io
n
1D Arrays

Here I have declared an array called


myList
And assigned 1,2…6 to it.
Teac
Expl her
anat
io
n
1D Arrays

The first location is in location 0.


This is called an index.
e fi r st location is
s, a nd therefore th
y
s use 1 - based arra
a t som e language 1.
se note th
Teac Plea
Expl her
anat
io
n
1D Arrays

The second location is in location 1.

This is called an index.

Teac
Expl her
anat
io
n
1D Arrays
Presentation
Mode Needed

How many items are in my array?

6
Teac
Expl her
anat
io
n
1D Arrays
Presentation
Mode Needed Index: 0 1 2 3 4 5

What is the last index?

5
Teac
Expl her
anat
io
n
1D Arrays
Length:
6
Presentation
Mode Needed Index
0 1 2 3 4 5
:

So what is relationship between the length of an array and


the last index?

The last index is always the size of the


Teac array - 1
Expl her
anat
io
n
Task 1: Lengths of Array /
Indices
For each array given below state the length and the index for the last
item.
Array: cars = ['Mustang', 'Dodge', 'Nissan', 'Ferrari’]
Length: Index of Last
Item:

Array: dogs = [‘Max', ‘Fido', ‘Rover']


Length: Index of Last
Item:

Array: numbers = [10,20,30,40,50]


Length: Index of Last
Task Item:
Feedback: Task 1

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

Line 2 assigns location 0 to


100.

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

Code food = [’Milk’, ‘Cheese’, ‘Apples’, ‘Bread’, ‘Bananas’]


print(food[3])
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:

DECLARE food as ARRAY[1..5]


of STRING

food <- [‘Milk’, ‘Cheese’,


‘Apples’, ’Bread’, ‘Bananas’]

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

But we would have to keep recoding- if the array


changed size, so is there another method.
Could we use a loop?

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

How could we use the counter to access the items


in the array?
Teac
Expl her
anat
io
n
1D Arrays
Instead of writing a number I
Before, I just manually entered
could just user counter (which is
the index to output.
an integer)
Presentation
Mode Needed

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’,

while counter <


5 DO
OUTPUT food[c
ounter]
Teac counter <- cou
Expl her END WHILE nter + 1
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

Code food = [’Milk’, ‘Cheese’, ‘Apples’, ‘Bread’, ‘Bananas’]


print(food[3])
Output

Excel
ask
TCode food = [’Milk’, ‘Cheese’, ‘Apples’, ‘Bread’, ‘Bananas’]
print(food[len(food)-1])
Task Output
Task 3a: Cars
1. Using the following array:

cars = ['Mustang', 'Dodge', 'Nissan', 'Ferrari’]

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:

cars <- [”Mustang”, “Dodge”, ”Nissan”, “Ferrari”

PROCEDURE outputCars ()
counter <- 0
WHILE counter < LENGTH(cars) DO
OUTPUT cars[counter]
counter <- counter + 1
END WHILE Sample Output:
END PROCEDURE

DELETE ME FOR HINTS


CALL outputCars() 1. Mustang
2. Dodge
3. Nissan
4. Ferrari

Task
Task 3: Cars
1. Screenshot your code from the previous slide below.

Python Code Runtime

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.

Python Code Runtime

Task
1D Arrays
Presentation
Mode Needed

I have added more


foods?

But when I run the code I


get the same list as
Teac before?
Expl her
anat
io
n
1D Arrays
Presentation
Mode Needed
Change this to a
7 7?

What changes should we make?

Teac
Expl her
anat
io
n
1D Arrays
Presentation
Mode Needed

These are
locations 0 and 1.

So I remove most of the items,


Teac It crashes
and the it crashes.
as it tries to output
Expl her
anat
io location 2 (which doesn’t
n
exist)
1D Arrays
Presentation
Mode Needed

So is there a way to iterate through an array no matter


the size.

Teac Yes, the function LEN( ) allows you find


Expl her out how many items (the length) are in
anat
io
n my array.
1D Arrays
Presentation
Mode Needed

Now what would this


Currently this would
Teac return areturn?
value of 2.
Expl her
anat
n
io 4
1D Arrays
Presentation
Mode Needed

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’,

while counter <


LENGTH(food)
OUTPUT food[c DO
ounter]
counter <- cou
Teac END WHILE nter + 1
Expl her
anat
io
n
Task 5: Smallest and Largest
1. Using the following array:
numbers = [3, 41, 26, 12, 9, 74, 15].

Iterate through the array and find the largest and smallest from the range

2. Screenshot your code onto the next slide. Sample Output:

Use 2 – 4 procedures Largest Number: 74


Smallest Number: 3

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.

Python Code Runtime

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

The sum of these numbers is 270

Task
Task 6: Adding Numbers to a
Hints:
List
Numbers <- []
total <- 0

PROCEDURE getNumbers() Sample Output:


counter <- 0 Enter number 1: 5
tempNum <- 0 Enter number 2: 10
while counter < 10 DO Enter number 3: 15
OUPTUT “Enter a Enter number 4: 20
number” Enter number 5: 25
INPUT tempNum Enter number 6: 30
numbers[counter] <- Enter number 7: 35
tempNum
END WHILE DELETE ME FOR HINTS
Enter
Enter
number
number
8: 40
9: 45
END PROCEDURE Enter number 10: 50

The sum of these numbers is 270


PROCEDURE calcTotal()
counter <- 0
total <- 0
while counter < 10 DO
//MISSING CODE
END WHILE
END PROCEDURE
Task
PROCEUDRE outputTotal()
//MISSING CODE
END PROCEDURE
Task 6: Adding Numbers to a
1. Screenshot your code from the previous slide below.
List
Python Code Runtime

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

Therefore you can access any letter in a string.

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.

2. Screenshot your code onto the next slide.

Use 2 – 4 procedures Sample Output:

Enter first name: Mr


Enter second name: Skinner

Your name backwards:

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.

Python Code Runtime

Task
Task 8: Quotes
1. Add the quotes below to a list / array. Make a program which outputs a
random quote.

2. Screenshot your code onto the next slide.


1. Fools use tomorrow, wise use today Sample Output:
2. You will be hungry again in 1 hour Fools use tomorrow, wise use
3. All things come to him who goes after today
them
4. Small opportunities are often the beginning Would you like another quote
of great enterprises Y/N: Y
5. Someone has Googled you recently
6. Grasp opportunities to create the future Grasp opportunities to create the
7. That wasn’t beef future
8. You will soon have food poisoning

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.

Python Code Runtime

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.

2. Screenshot your code onto the next slide.


Sample Output:

Bingo Ball: 22

Would you like another go


Y/N? Y

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

WHILE choice == “Y” DO


• Task 9: Bingo
1. Screenshot your code from the previous slide below.

Python Code Runtime

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

To create a 2D array you simply declare


multiple arrays inside another array.
Lets make an array of the top 5
premiership teams at the end of season
2020-2021.
top5Teams = [ [‘Man City’, ‘Man Utd’, ‘Liverpool’,
‘Chelsea’, ‘Leicester City’] ]

Teac
Expl her
anat
io
n
2D Arrays

Lets add the top five teams from the Spanish


league from the same season.
top5Teams = [ [‘Man City’, ‘Man Utd’, ‘Liverpool’,
‘Chelsea’, ‘Leicester City’] , [Athletico Madrid], [Real
Madrid’, ’Barcelona’,’Sevilla’, ‘Real Sociedad’] ]

Each array is separated by a


comma (just like an item in a
1D array)

Teac
Expl her
anat
io
n
2D Arrays

Lets add the top five teams from the


French League….
top5Teams = [ [‘Man City’, ‘Man Utd’, ‘Liverpool’,
‘Chelsea’, ‘Leicester City’] , [Athletico Madrid], [‘Real
Madrid’, ’Barcelona’,’Sevilla’, ‘Real Sociedad’] ,
[‘LOSC’, ‘PSG’, ‘Monaco’, ‘Lyon’, ‘Marseille’] ]

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 Man City Man Utd Liverpool Chelsea Leicester City


1
Athletico Real Barcelona Sevilla Read Sociedad
2 Madrid Madrid

LOSC PSG Monaco Lyon Marseille

To output the first element I would need to code:


The first element is in the first address of the first list, is in location 0, 0
print(top5Teams[0][0])
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 Man City Man Utd Liverpool Chelsea Leicester City


1
Athletico Real Barcelona Sevilla Read Sociedad
2 Madrid Madrid

LOSC PSG Monaco Lyon Marseille

How would you output the last item on the list?


Teac
Expl her print(top5Teams[2][4])
anat
io
n
2D Arrays
top5Teams = [ [‘Man City’, ‘Man Utd’, ‘Liverpool’,
‘Chelsea’, ‘Leicester City’] , [Athletico Madrid], [‘Real
Madrid’, ’Barcelona’,’Sevilla’, ‘Real Sociedad’] ,
[‘LOSC’, ‘PSG’, ‘Monaco’, ‘Lyon’, ‘Marseille’] ]
0 1 2 3 4

0 Man City Man Utd Liverpool Chelsea Leicester City


1
Athletico Real Espanyol
2 Barcelona Sevilla Read Sociedad
Madrid Madrid

LOSC PSG Monaco Lyon Marseille

To change an item in an array (for example Sevilla) you would need to


know the location
Task top5Teams[1][3] = “Espanyol”
Task 1: Identify the Index
Address
Below are groups A, B and C for the recent Euro 2020 competition.
Italy
0
Answer the questions 1 about the22D array structure.
below 3 The array4 is called
Euro2020
0
Wales England Switzerlan Turkey
1 d
2 Belgium Finland Russia Denmark

Netherland Austri Ukrain Greec


s a e e

What is the python code to do: Answer Here:


OUTPUT WALES
OUTPUT DENMARK
Change Greece to North Macedonia
Task Change England to Italy
Feedback: Task 1
Below are groups A, B and C for the recent Euro 2020 competition.

0
Answer the questions 1 about the22D array structure.
below 3 4

0
Wales England Switzerlan Turkey
1 d
2 Belgium Finland Russia Denmark

Netherland Austri Ukrain Greec


s a e e
What is the python code to do: Answer Here:
OUTPUT WALES print(Euro2020[0][0])
OUTPUT DENMARK print(Euro2020[1][3])
Change Greece to North Macedonia Euro2020[2][3] = “North Macedonia”

Feed Change England to Italy Euro2020[0][1] = “Italy”


ba ck
2D Arrays
top5Teams = [ [‘Man City’, ‘Man Utd’, ‘Liverpool’,
‘Chelsea’, ‘Leicester City’] , [Athletico Madrid], [‘Real
Madrid’, ’Barcelona’,’Sevilla’, ‘Real Sociedad’] ,
[‘LOSC’, ‘PSG’, ‘Monaco’, ‘Lyon’, ‘Marseille’] ]

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.

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.
4. Screenshot onto the next slide.
0 1 2 3

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

DECLARE numbers : Array[1:3, 1:4] of INTEGER


DECALRE counter1, counter2 AS INGTEGER

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.

2. Output the array in in full.

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.

Python Code Runtime

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

2. Screenshot your code onto the next


available slide.

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:

Players <- [[“Messi”, 450, 21, 5], Sample Output:


[“Ronaldo”, 800, 45,
8]…]
Name: Messi
Total Career Goals: 450
PROCEDURE outputPlayers( )
counter1<-0 Total Career Yellow Cards: 21
DELETE ME FOR HINTS
counter2<-0
while counter1 < LENGTH(players) DO
Total Career Red Cards: 5

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.

Python Code Runtime

Task
Task 4a: Cinema Booking
1. Write a Python which has an empty
array of 4 x 5 elements

2. The program should ask a user for a


seat number (for a booking). It Sample Output:
should ask for the row and then the
seat. Which row: 5
Which seat: 1
Eg. 5, 1 would be 1st seat on the 5th
Seat Booked
row. Have a nice day.

3. When this seat is selected (and is


available) the seat should be ‘booked’)

Use 1 procedure(s)
Use 1 function(s)

Task
Task 4a: Cinema Booking
Hints:

Seats <- [[“X”]] //EMPTY ARRAY WITH SOME ROGUE


VALUE

FUNCTION checkSeats(row: INTEGER, seat: INTEGER)

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.

Python Code Runtime

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:

Seats <- [[“X”]] //EMPTY ARRAY WITH SOME ROGUE


VALUE

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.

Python Code Runtime

Task
Task 5a: Tic Tac Toe
1. Create a program that allows two
players to play tic-tac-toe (noughts
and crosses)

2. Make 1 procedure for your game Sample Output:


that allows the players to see the
where each others noughts or EEE
crosses are. It could also show “E”
for a empty slot. EEE
Use 1 procedure(s)
EEE

Task
Task 5a: Tic Tac Toe
Hints:

TTT <- [[“E”]] * 3 x 3//EMPTY ARRAY WITH SOME ROGUE


VALUE

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.

Python Code Runtime

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

I will use a counte


r to control the
loop, but I will also
use a Boolean
to stop the loop if
the item is
found.

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.

This will check, do


es the item the
user wanted to loo
Teac This variable tells us that the k for equal the
curre
ExpTlaskher items has been found. nt item.
anat
io
n
Linear Search

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

1. Write a program that has a pre-


filled list (you can add your choice
Sample Output:
of items – football clubs, cars, food What is the team?
etc). Barcelona

2. Have the program ask the user


Team found
what to search for.

3. If the searched item is found the


user should be told.

4. Have the program stop looping if


the item is found.

Task 5. Use procedures / functions for your


program – if you haven’t already
done so.
Task 1: Searching Array
1. Screenshot your code from the previous slide below.

Python Code Runtime

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.

2. The program will output the sorted


array.

3. You will need:


• 1 array (obviously)
• Nested loops (2 loops)
• Max of 3 variables
Task
Plenary Task 1
I know / I know Can I do it? I know / I know Can I do it?
how to how to
I can use procedures I can declare procedures
correctly with pseudocode
I can use functions I can declare functions with
correctly pseudocode
Declare a 1D array in Declare a 1D array in
python pseudocode
Declare a 2D array in Declare a 2D array in
python pseudocode
Add an item to a 1D Add an item to a 1D array
array in pseudocode
Add an item to a 2D Add an item to a 2D array
array in pseudocode
Output all items in a 1D Output all items in a 1D
array in python array in pseudocode
Output all items in a 2D Output all items in a 2D
array in python array in pseudocode
Task Perform a linear search
in a 1D array
Perform a bubble
search in a 1D array

You might also like