Pseudocode Fundamentals and Exercises
Pseudocode Fundamentals and Exercises
Successful Pseudocoding
Edition 2.01 alpha
[Link]
1
Successful Pseudocoding
© Code for Schools, [Link]
Table of Contents
Notes to Edition 2.0 alpha 3
Chapter 1 - Introduction to Pseudocode 3
Chapter 2 - Variables 4
2A - Variables 4
2B - Variables on the Right Hand side of ← 7
2C - Trace Tables 10
2D - Self-assignment 15
2E - Writing Pseudocode 17
Chapter 3 - Boolean Expressions 19
3A - Boolean Expressions 19
3B - Compound Boolean Expressions 21
3C - Boolean Expressions involving Variables 23
3D - Writing Pseudocode 26
Chapter 4 - Conditional Statements 28
4A - IF Statements Analysis 28
4B - Writing Pseudocode 34
4C - IF / ELSE IF / ELSE Statements Analysis 36
4D - Writing Pseudocode 42
Chapter 5 - While Loops 45
5A - While Loops Analysis 45
5B - Writing Pseudocode 52
5C - Applications 55
Chapter 6 - Functions 60
6A - Functions Analysis 60
6B - Writing Functions 66
6C - Multiple Functions Analysis 70
Chapter 7 - Introduction to Lists 73
7A - Initialising Lists 73
7B - List Elements 76
7C - Appending to Lists 78
7D - List Indices 81
7E - Indexing Lists 82
7F - Indexing Lists with Expressions 84
7G - Looping Through Lists (Analysis) 86
7H - Looping Through lists (Writing Code) 91
7I - Updating lists (Analysis) 92
7J - Updating lists (Writing Code) 96
Appendix A - Pseudocode to Python 97
Solutions to Exercises 98
2
Successful Pseudocoding
© Code for Schools, [Link]
As a result, this resource now provides a complete course of tools and techniques for students
to analyse and design mathematical algorithms required at senior secondary level.
Still to do:
- Deeper and clearer elaborations of concepts
- Links to animated / video walkthroughs of trace tables
- Fix errors in questions and solutions (of which I’m sure there are many!)
Any questions, comments, feedback, or errors please let the author know via
toan@[Link]
3
Successful Pseudocoding
© Code for Schools, [Link]
Pseudocode is a structured way of representing instructions for humans to follow the algorithm.
Like coding languages which you may have come across previously (Scratch, Python, Java,
C++ etc.) pseudocode does have a set of rules to follow with regard to how to use its symbols
(operators) and how to represent numbers etc. However, unlike these coding languages, the
rules are more relaxed and pseudocode is designed for humans to read and understand.
Pseudocode also provides a common format for us humans to share algorithms between
ourselves. For instance, when one person only knows how to program in Python and another
person only knows how to program in Javascript, pseudocode provides a relaxed “common
ground” for us both.
If we would like to have a computer run our algorithm, we will need to translate pseudocode into
actual program code by using a particular programming language. Appendix A covers briefly
how to convert the algorithms in this book into the Python programming language - so you can
see these algorithms “come to life” on the computer.
4
Successful Pseudocoding
© Code for Schools, [Link]
Chapter 2 - Variables
2A - Variables
A variable is named location in the computer’s memory which we use to store data values.
We use variables to represent unknown quantities, represent quantities which change
(vary-able) and to generally keep track of values.
Whenever we see an ← sign, a variable must be on the left hand side (because we’re storing a
value into it).
We may also see variables on the right hand side too (see later section).
x ← 10
We read this as “10 is assigned to x” or “x is assigned the value of 10”
Any valid mathematical expression can be found on the right hand side of the assignment
operator ←. Any arithmetic expressions are evaluated using standard B(O/I)DMAS rules.
5
Successful Pseudocoding
© Code for Schools, [Link]
Examples:
Which of these are valid assignment statements?
a.
c ← 42 True
b.
10 ←c False
(the left hand side must be a variable)
c. 14 → x False
(the assignment operator is in the
wrong direction)
d.
y ← (10 + 2) ✕ 3
True
e. (20 - 8) / 3 → d False
(the assignment operator is in the
wrong direction)
g. x + 3 False
(there is no assignment operator - the
value in the x variable does not change)
h.
area ← 42 True
6
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Which of these are valid assignment statements?
1.
d ←0
2.
x ← 30.0 - 2
3. 0→x
4. (20 - 8) / 3 → y
5.
x + 2 ←4
6.
z + x ← (10 + 2) + 3
7.
g ← ((10 / 2) ✕ (3 + 5)) - 3.3
8. a / 3
9. x + z + 3
10.
perimeter ← 2 * 10 + 2 * 5
7
Successful Pseudocoding
© Code for Schools, [Link]
Variable on the right hand side: value is read from the variable.
Variable on the left hand side: value is written to the variable.
Any variable found on the right hand side does not change its value.
If we wish to update the value in a variable we must put it on the left hand side.
When there are two or more pseudocode instructions, they are executed in order, one line at a
time, from top to bottom.
Any variable found on the right hand side of the ← , must already have a value assigned to it
otherwise the pseudocode is invalid.
E.g.
x ←y+3
The above pseudocode is invalid there has been no value assigned to the y variable.
y ←3
x ← y + 3
The above pseudocode is now valid (read from top to bottom).
y ←x-3
x ← y + 3
The above pseudocode is invalid because in the very first line, x does not have a value
assigned to it (read from top to bottom).
x ←0
y ← x - 3
x ← y + 3
The above pseudocode is now valid (read from top to bottom).
8
Successful Pseudocoding
© Code for Schools, [Link]
Examples:
What is the final value of x and y in each of the following pseudocode programs:
a.
y ← 42 x: 21
y: 42
x ← 21
b.
y ← 42 x: 44
y: 42
x ← y + 2
c.
y ← 10 + 2 x: 6
y: 12
x ← y / 2
9
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
What is the final value of x and y in each of the following pseudocode programs:
1.
y ← -3
x ← -6
2.
y ← 0.4
x ← y / 2
3.
y ← -3 + 2
x ← y + 2
10
Successful Pseudocoding
© Code for Schools, [Link]
2C - Trace Tables
Trace tables are an extremely useful tool for keeping track of variable values throughout the
execution of pseudocode. Once you learn how to use trace tables effectively, you will be able to
deconstruct algorithms and see how they work.
The trace table below consists of 2 columns, one listing the variable names, and the other
containing the values found within the corresponding variable.
Variable Value
To use a trace table with pseudocode, we step through the pseudocode, one line at a time, and
record any changes to variables and their values in the tracetable. It is important to keep the
trace table updated with every line of pseudocode!
11
Successful Pseudocoding
© Code for Schools, [Link]
y ←0
y ← 3
x ← y - 2
y ← x
x ← y + 5
y ←0
Variable Value
y ← 3
y 0
x ← y - 2
y ← x
x ← y + 5
y ←0
Variable Value
y ← 3
y 0, 3
x ← y - 2
y ← x
x ← y + 5
y ←0
Variable Value
y ← 3
y 0, 3
x ← y - 2
x 1
y ← x
x ← y + 5
12
Successful Pseudocoding
© Code for Schools, [Link]
y ←0
Variable Value
y ← 3
y 0, 3, 1
x ← y - 2
x 1
y ← x
x ← y + 5
y ←0
Variable Value
y ← 3
y 0, 3, 1
x ← y - 2
x 1, 6
y ← x
x ← y + 5
13
Successful Pseudocoding
© Code for Schools, [Link]
Examples:
Using trace-tables, execute the following pseudocode and determine the final values of all the
variables:
Pseudocode Trace-table
a.
a ←1
Variable Value
b ← a - 3
a 1, -1
a ← b + 1
b -2
b.
x ←3-2
Variable Value
y ← x / 2
x 1, 3
z ← x + y
y 0.5
x ← z / y z 1.5
c.
x ← -3
Variable Value
y ← x + x
x -3, 2
x ← y / x
y -6, 4
y ← x * x
14
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Using trace-tables, execute the following pseudocode and determine the final values of all the
variables:
Pseudocode Trace-table
1.
a ← 10
b ← 20
a ← b
b ← a
2.
x ← 10
y ← 20
z ← x
x ← y
y ← z
3.
x ←8
y ← x - x / 2
x ← y + x
y ← x * x
4.
x ←1
y ← 1
z ← x + y
x ← y + z
y ← x + z
z ← x + y
15
Successful Pseudocoding
© Code for Schools, [Link]
2D - Self-assignment
The same variable can be found on both sides of the assignment operator.
E.g.
x ←2
x ← x + 1
When this occurs, as usual, we evaluate the right hand side of the assignment operator first and
read from the variable, operate on it, and then assign it back to the variable.
Remember that any operations on the right hand side of the assignment statement does not
change the value of any variables. The values are only read from the variables.
Examples:
Using trace-tables, execute the following pseudocode and determine the final values of all the
variables:
Pseudocode Trace-table
a.
x ←0
Variable Value
x ← x + 1
x 0, 1
b.
x ←2*3
Variable Value
x ← x * 2
x 6, 12, 144
x ← x * x
c.
a ← -3
Variable Value
b ← a + a
a -3, 0.5
a ← a / b
b -6
16
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Using trace-tables, execute the following pseudocode and determine the final values of all the
variables:
Pseudocode Trace-table
1.
a ← 10
a ← a / 2
2.
x ←8
y ← (x + 4) / 3
y ← y * 3
y ← y - 4
3.
x ←4
y ← 8
y ← x + y
x ← y - x
y ← y - x
17
Successful Pseudocoding
© Code for Schools, [Link]
2E - Writing Pseudocode
Examples:
Write the pseudocode to achieve the goal provided.
Goal Code
18
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Write the pseudocode to achieve the goal provided.
Goal Code
19
Successful Pseudocoding
© Code for Schools, [Link]
3A - Boolean Expressions
Boolean expressions are expressions which evaluate to either True or False.
The following operators are used to compare the left and right hand operands.
They will always evaluate to True or False.
The not operator will negate (flip between True and False) whatever boolean expression follows
it.
Examples:
Expression Evaluation
a. 1<3 True
b. 3 == 1 + 2 True
c. True True
d. 3 != 1 True
f. not (2 == 2) False
20
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Expression Evaluation
1. 5 == 2
2. 10 < 10
3. 2 != 2
4. 32 >= 32
5. (5 + 3) < 8
6. 7 + 2 * 2 == (11 - 0)
7. True == False
9. False
21
Successful Pseudocoding
© Code for Schools, [Link]
We can combine two or more boolean expressions together using the and and or operators.
The and operator will evaluate to True only when both the LHS and RHS are True. Any other
combination will evaluate to False.
The or operator will evaluate to True when either the LHS or RHS are True. If both LHS and
RHS are False, it will evaluate to False.
Examples:
Evaluate the following combined boolean expressions.
Expression Evaluation
a. 1 == 1 and 2 == 2 True
b. 4 == -4 and 10 == 10 False
e. 6 == 5 or 7 < 8 True
f. 10 > 3 or 4 == 3 True
22
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Evaluate the following combined boolean expressions.
Expression Evaluation
2. 8 > 0 or 1 < 0
3. 10 == (9 + 1) and 3 < 5
5. 50 < 34 or 34 > 43
6 10 == 10 and 10 < 10
8. False or True
23
Successful Pseudocoding
© Code for Schools, [Link]
Examples:
Using trace-tables, execute the following pseudocode and determine the final values of all the
variables:
Pseudocode Trace-table
a.
age ← 10
Variable Value
result ← age < 18
age 10
result True
b.
height ← 1.83
Variable Value
six_feet ← height >= 1.83
height 1.83
six_feet True
c.
f ← 32.0
Variable Value
c ← (f - 32.0) * 5/9
f 32.0
freezing ← c <= 0
c 0
freezing True
24
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Using trace-tables, execute the following pseudocode and determine the final values of all the
variables:
Pseudocode Trace-table
1.
age ← 18
result ← age < 18
2.
date ← 31/01/2001
return_by ← 28/02/2001
3.
height ← 180
height ← height / 100
4.
t ← 200.0
t ← (t - 32.0) * 5/9
5.
hours ← 150
age ← 19
test_passed ← True
Test_passed
25
Successful Pseudocoding
© Code for Schools, [Link]
6.
distance ← 1500
distance ← distance / 1000
fast_food ← True
rating ← 4
26
Successful Pseudocoding
© Code for Schools, [Link]
3D - Writing Pseudocode
Examples:
Requirements Pseudocode
27
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Requirements Pseudocode
28
Successful Pseudocoding
© Code for Schools, [Link]
4A - IF Statements Analysis
The IF statement must always be followed by a boolean expression (this is called the
condition).
If the boolean expression (condition) evaluates to True, any sequence of instructions which are
indented underneath the IF statement is executed.
If the condition evaluates to False, the sequence of instructions indented underneath the IF
statement is skipped and the next instruction underneath the IF statement at the same
indentation level is executed.
Examples:
a.
x ← 0
if x <= 0 Variable Value
x ←1 x 0, 1
b.
x ← 10
if x >= 5 Variable Value
x ←x/2 x 10, 5, 15
x ← x + 10
29
Successful Pseudocoding
© Code for Schools, [Link]
c.
x ←2
Variable Value
y ← 1
x 2
min ← x
y 1
if y < x
min ←y min 2, 1
d.
x ←2
Variable Value
y ← 1
x 2
max ← x
y 1
if y > x
max ←y max 2
e.
x ←6
if x > 2 and x < 10 Variable Value
←x+2
x x 6, 8, 4
x ← x / 2
f.
x ←12
if x > 0 and x < 5 Variable Value
←x-2
x x 12, 6
x ← x / 2
30
Successful Pseudocoding
© Code for Schools, [Link]
g.
t ← 21
Variable Value
fan_on ← False
t 21
if t >= 20 and fan_on == False
h.
x ←3
if x >= 0 Variable Value
x ←
-x x 3, -3, 0
if x <= 0
x ←x+3
i.
x ←-3
if x >= 0 Variable Value
x ←
-x x -3, 0
if x <= 0
x ←x+3
31
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
1.
y ←-3
if y <= 0
y ←y*2
2.
x ←1
if x >= 1
x ←x+2
x ← x - 10
3.
x ←4
if x > 4 and x < 6
←x-1
x
x ← x / 2
4.
z ←10
if z > 0 and z < 50
←x/2
x
x ← x - 2
5.
t ← 21
fan_on ← True
if t >= 20 and fan_on == False
fan_on ← True
32
Successful Pseudocoding
© Code for Schools, [Link]
6.
t ← 24
fan_on ← False
if t >= 20 and fan_on == False
fan_on ←
True
if t <= 16 and fan_on == True
fan_on ← False
7.
y ←1
if y >= 0
y ←
-y
if y <= 0
y ← y * -10
8.
x ←10
if x > 5 and x < 15
x ←
-x
if x <= 0
← x + 10
x
x ← x + 3
9.
day_hours ← 10
night_hours ← 2
is_night ← False
if is_night and night_hours < 120
night_hours ←
night_hours + 1
if not is_night and day_hours <
120
day_hours ← day_hours + 1
33
Successful Pseudocoding
© Code for Schools, [Link]
10.
x ←8
y ← 10
z ← 2
min ← x
if y < x and y < z
min y←
if z < x and z < y
min ←z
11.
x ←8
y ← 10
z ← 2
max ← x
if y > x and y > z
max y←
if z > x and z > y
max ←z
34
Successful Pseudocoding
© Code for Schools, [Link]
4B - Writing Pseudocode
For the exercises below, write the pseudocode to fulfill the requirements
Examples:
Requirements Pseudocode
time ← time + 1
c. ●
●
Assign 18 to john_age
Assign 20 to jane_age john_age ← 18
● Assign 22 to jack_age
● Calculate the largest age and assign it jane_age ← 20
to the variable largest_age
jack_age ← 22
largest_age ← john_age
if jane_age > jack_age and
jane_age > john_age
largest_age ←
jane_age
if jack_age > jane_age
and jack_age > john_age
largest_age ← jack_age
35
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Requirements Pseudocode
3. ● Assign 18 to john_age
● Assign 20 to jane_age
● Assign 22 to jack_age
● Calculate the middle age and assign it
to the variable mid_age
36
Successful Pseudocoding
© Code for Schools, [Link]
When we would like the program to execute a sequence of instructions when the condition to an
IF statement is False, we use the else statement.
All the instructions indented beneath the else statement will be executed when the IF condition
is False.
Examples:
a.
x ← 0
if x > 0 Variable Value
x ←1 x 0, 2
else
x ←2
b.
x ← 10
if x > 20 Variable Value
x ←x+2 x 10, 8, 4
else
←x-2
x * Unindented code after IF
statements are always executed
x ← x / 2 sequentially afterwards
c.
x ← 16
if x > 20 Variable Value
x ←
x + 2 x 16, 14, 7
else if x > 10
x ←x-2
else
←2
x
x ← x / 2
37
Successful Pseudocoding
© Code for Schools, [Link]
d.
x ←8
if x > 20 Variable Value
else
←x+4
x
x ← x + 2
x ← x / 2
e.
a ← 32
Variable Value
b ← 42
a 32, 40
if a > 20 and b < 40
a ←b+2 b 42, 36
b ← a + 4
else if a > 20 and b < 50
a ←b-2
b ← a - 4
else if x > 30 and b < 50
a ←b+4
b ← a + 2
else
a ←b-4
b ← a - 2
38
Successful Pseudocoding
© Code for Schools, [Link]
f.
x ← 1
if x >= 1 Variable Value
x ←
x + 1 x 1, 2
else if x >= 2
x ←
x - 1
else if x >= 3
x ←x-2
else
x ←0
g.
x ← 1
if x >= 1 Variable Value
x ←
x + 1 x 1, 2, 1, 0
if x >= 2
x ←
x - 1 * Notice how the sequential IF
if x >= 3 statements are always checked
x ←x-2 (contrast with the example
above where the ELSE IFs are
else
only checked when the IF
x ←0 statement before it is false)
39
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
1.
y ← -2
if y > 0
y ←y+1
else
y ←y-2
2.
z ← 20
if z >= 30
z ← z - 20
else
← z + 20
z
z ← z / 4
3.
x ← 2
if x >= 2
x ←
x + 2
else if x < 0
x ←x-2
else
←4
x
x ← x * 2
40
Successful Pseudocoding
© Code for Schools, [Link]
4.
y ←0
if y > 10
y ←y+2
y ← y + 4
else if y > 5
y ←y-2
y ← y + 4
else
←y+4
y
y ← y + 2
y ← y * 0.5
5.
a ← 16
b ← 20
if a > 20 and b < 30
a ←b+2
b ← a + 4
else if a > 10 and b < 30
a ←b-3
b ← a - 2
else if a > 0 and b < 50
a ←b+4
b ← a + 2
else
a ←b-4
b ← a - 2
41
Successful Pseudocoding
© Code for Schools, [Link]
6.
x ← 2
if x >= 1
x ←
x - 1
else if x >= 2
x ←
x + 1
else if x >= 3
x ←x-2
else
x ←0
7.
z ← 2
if z >= 2
z ←
z + 1
if z >= 3
z ←
z - 1
if z >= 4
z ←z-2
else
z ← z * 10
42
Successful Pseudocoding
© Code for Schools, [Link]
4D - Writing Pseudocode
For the exercises below, write the pseudocode to fulfill the requirements
Examples:
Requirements Pseudocode
grade ←4
else
grade ←5
OR
score ← 61
if score >= 80
grade ←1
else if score >= 70
grade ←2
else if score >= 60
grade ←3
else if score >= 50
grade ←4
else
grade ←5
43
Successful Pseudocoding
© Code for Schools, [Link]
y ← -x
else
y ←0
44
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Requirements Pseudocode
45
Successful Pseudocoding
© Code for Schools, [Link]
There are a few types of loops in Python, in this section we will discuss the while loop.
A while loop operates similarly to an IF statement. The while keyword is followed by a boolean
expression (called the condition), like the IF statement. If the boolean expression (condition)
evaluates to True, the code indented underneath the while statement is executed (just like in the
IF statement). We call this "entering the loop".
However, unlike the IF statement, when the indented sequence of instructions are completed,
the while condition is re-evaluated. If the condition is True, then the indented code is executed
again. After each execution of the sequence of indented code, the condition is always
re-evaluated, and the indented block of code is always repeated while the condition is True. To
avoid the loop going on forever (an infinite loop) we need to ensure that some part of the code
within the indented sequence of instructions will eventually cause the while condition to become
False.
When the condition is False, the next unidented instruction underneath the while statement is
executed. We call this "exiting the loop".
Examples:
a.
x ← 0
while x < 3 Variable Value
x ←x+1 x 0, 1, 2, 3
b.
x ← 0
while x <= 3 Variable Value
x ←x+1 x 0, 1, 2, 3,
4
46
Successful Pseudocoding
© Code for Schools, [Link]
c.
i ←1
Variable Value
t ← 0
i 1, 2, 3, 4,
while i <= 4
5
t ←t+i t 1, 3, 6, 10
i ← i + 1
d.
a ←0
Variable Value
b ← 1
a 0, 1, 3, 8, 21
while a <= 8
a ←a+b b 1, 2, 5, 13, 34
b ← a + b
* produces the fibonacci
sequence
e.
x ← -2
Variable Value
sum ← 0
x -2, -1, 0, 1, 2, 3
while x <= 2
y ←x*x y 4, 1, 0, 1, 4
sum ← sum + y
sum 0, 4, 5, 5, 6, 10
x ← x + 1
* sums all the values of x2 for x
𝞊 Z and -2 <= x <= 2
47
Successful Pseudocoding
© Code for Schools, [Link]
f.
x ← -1
Variable Value
min ← ∞
x -1, 0, 1, 2, 3
while x < 3
y ←(x - 1)2 + 3 y 7, 4, 3, 4
if y < min min ∞, 7, 4, 3
min ←y
x ←x+1 * finds the minimum y value for
-1 <= x <= 2
g.
a ←0
Variable Value
b ← 1
a 0, 1, 2
f ← 0
b 1, 2, 3
n ← 1 f 0, 1, 1, 2, 3
while n < 6
if n == 1 n 1, 2, 3, 4, 5, 6
f ←a
else if n == 2
* f will be the nth fibonacci
f ←b number
else
f ←a+b
a ← b
b ← f
n ← n + 1
48
Successful Pseudocoding
© Code for Schools, [Link]
h.
x1 ←0
Variable Value
max_grad_x← x1 x1
max_grad ← -∞
max_grad_x
x2 ← x1 + 0.5 x2
x1 ← x2
49
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
1.
x ←3
while x > 0
x ←x-1
2.
x ←0
while x <= 6
x ←x+2
3.
i ← 10
t ← 0
while i <= 15
t ←t+i
i ← i + 1
4.
a ←1
b ← 2
while a <= 21
a ←a+b
b ← a + b
5.
x ←0
sum ← 0
while x <= 2
y ← -x + 2x2
sum ← sum + y
x ← x + 0.5
50
Successful Pseudocoding
© Code for Schools, [Link]
6.
x ←0
min ← ∞
while x <= 2
y ←x2 - 2x
if y < min
min ←y
x ← x + 0.5
7.
x ←0
max ← -∞
while x <= 2
y ←-x3 + 3x
if y > max
max ←y
x ← x + 0.5
51
Successful Pseudocoding
© Code for Schools, [Link]
8.
a ←2
b ← 1
L ← 0
n ← 1
while n < 6
if n == 1
L ←a
else if n == 2
L ←b
else
L ←a+b
a ← b
b ← L
n ← n + 1
52
Successful Pseudocoding
© Code for Schools, [Link]
5B - Writing Pseudocode
For the exercises below, write the pseudocode to fulfill the requirements
Examples:
Requirements Pseudocode
sum← sum + i
i ← i + 1
square ←i*i
sum← sum + square
i ← i + 1
y ←0.5x4 - x3 -2x2
if y < min
min ←y
x ←x+1
53
Successful Pseudocoding
© Code for Schools, [Link]
y ←0.5x3-x2–0.18x+0.36
if y == 0
x_intercept ←x
x ← x + 0.5
e. ● Find the sum of y values for
y = 0.5x4 - x3 -2x2 for x ∈ [-1, 3], x ∈ Z x← -1
sum ← 0
while x <= 3
y ← 0.5x - x
4 3
-2x2
sum ← sum + y
x ← x + 1
54
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Requirements Pseudocode
55
Successful Pseudocoding
© Code for Schools, [Link]
5C - Applications
For the exercises below, write the pseudocode to fulfill the requirements
Examples:
Requirements Pseudocode
factorial ← factorial * n
n ←n-1
OR
n ←1
factorial ←1
while n <= 20
factorial ← factorial * n
n ←n+1
56
Successful Pseudocoding
© Code for Schools, [Link]
sign ← 1
● Calculate the value for PI after
100 iterations denom ← 1
while n <= 100
← sign * 1 / denom
term
sign ← -sign
denom ← denom + 2
n ← n + 1
PI ← sum * 4
57
Successful Pseudocoding
© Code for Schools, [Link]
c.
● Calculate 3
using bisection a ← -2
b ← -1
method, where a = 2 and b = -1
c ← (a + b) / 2
f_c ← c - 32
accuracy ← |f_c|
answer ← a
while accuracy > 0.01
f_a ←a 2
- 3
f_b ← b 2
- 3
if f_a * f_c < 0
b ←c
else
a←c
c ← (a + b) / 2
f_c ← c - 3 2
accuracy ← |f_c|
answer ← c
58
Successful Pseudocoding
© Code for Schools, [Link]
x ← a
while x < b
f_x ← 9 - 0.1x2
x ← x + step
—------------ OR —---------
a ←1
b ← 6
n ← 10
step ← (b - a) / n
sum ← 0
x ← a
while x < b
← 9 - 0.1x
f_x 2
x ← x + step
59
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Requirements Pseudocode
1. loge2 =
2. 3
● Calculate 4
using the
bisection method
60
Successful Pseudocoding
© Code for Schools, [Link]
Chapter 6 - Functions
6A - Functions Analysis
Functions are chunks of pseudocode which have been grouped together under a name and
perform a specific task or calculation, given some information.
For example, let’s imagine we would like to add 1 to the value in a variable. We might write a
function like the one below:
def addOne(x)
result ←x + 1
return result
When we write a function like the one above, the pseudocode does not run… yet. In order to
use the function, we must call it.
For example:
z ← addOne(10)
This is the name of the function we are calling.
This is the argument given to the function.
61
Successful Pseudocoding
© Code for Schools, [Link]
Examples:
Complete a trace table for each of the pseudocode functions below.
a. def add(x, y)
z ← add(10, 20) y 20
total 30
z 30
b. def add(x, y)
return x + y
Variable Value
z ← add(10, 20) x 10
y 20
z 30
c. def square(x)
sq ← x * x Variable Value
return sq
x 5
z ← square(5) sq 25
z 25
d. def cube(x)
cb ← x * x * x Variable Value
return cb
side 4
side ←4 x 4
z ← cube(side) cb 64
62
Successful Pseudocoding
© Code for Schools, [Link]
z 64
e. def quadruple(x)
return x * x * x * x
Variable Value
a ←3 a 3
z ← quadruple(a) z 81
f. def hypotenuse(a, b)
c ← 𝑎 +𝑏
2 2 Variable Value
return c side1 3
side1 ←3 side2 4
a 3
side2 ← side1 + 1
b 4
z ← hypotenuse(side1, side2)
c 5
z 5
63
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Complete a trace table for each of the pseudocode functions below.
Code Trace Table
1. def sub(x, y)
diff ←x - y
return diff
z ← sub(100, 90)
2. def sub(x, y)
return x - y
z ← sub(10, 9)
3. def double(x)
db ← x + x
return db
z ← double(9)
4. def halve(x)
return x / 2
num← 10
z ← halve(num)
64
Successful Pseudocoding
© Code for Schools, [Link]
5. def energy(m)
return m * 3000000002
mass ←5
z ← energy(mass)
6. def area(l, w)
a ← l * w
return a
x ←3
y ← 4
z ← area(x, y)
7. def perimeter(l, w)
return 2 * (l + w)
x ←3
y ← 4
z ← perimeter(x, y)
65
Successful Pseudocoding
© Code for Schools, [Link]
8. def min(x, y)
result ←x
if y < x
result y←
return result
z ← min(10, 5)
66
Successful Pseudocoding
© Code for Schools, [Link]
6B - Writing Functions
For the exercises below, write the pseudocode to fulfill the requirements
Examples:
Requirements Pseudocode
Then: z ← average(4, 6)
● call the function
● supply it arguments of 4 and 6
● and assign the returned value to the
variable z
67
Successful Pseudocoding
© Code for Schools, [Link]
Then:
p ← isPositive(10)
● call the function
● supply it an argument of 10 -----Alternative-----
● and assign the returned value to the
variable p def isPositive(x)
result← True
if x < 0
result ←False
return result
p ← isPositive(10)
e. Write a function: def max(a, b)
● named: max
● with 2 parameters: a, b result←a
● which returns a if a is larger than b, b if b > a
otherwise
result ←b
Then: return result
● call the function
● supply it arguments of -3, -4
● and assign the returned value to the c ← max(-3, -4)
variable c
-----Alternative-----
def max(a, b)
if a > b
return a
else
return b
c ← max(-3, -4)
68
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Write the pseudocode to fulfill the requirements
Requirements Pseudocode
Then:
● call the function
● supply it an argument of 2,
● and assign the returned value to the
variable c
Then:
● call the function
● supply it arguments of 1, 2, and 3
● and assign the returned value to the
variable d
69
Successful Pseudocoding
© Code for Schools, [Link]
Then:
d ← canDrive(17)
● call the function
● supply it arguments of 17 -----Alternative-----
● and assign the returned value to the
variable d def canDrive(age)
result← True
if x < 8
result ←False
return result
d ← canDrive(17)
5. Write a function: def max(x, y, z)
● named: max if x >= y and x >= z
● with 2 parameters: x, y, z return x
● which returns the largest of the values else if y >= x and y >= z
in the parameters return y
else
Then: return z
● call the function
● supply it arguments of -3, -4, 0
biggest ← max(-3, -4, 0)
● and assign the returned value to the
variable biggest
70
Successful Pseudocoding
© Code for Schools, [Link]
In some cases, we may also pseudocode within one function, call another function.
Examples:
a. def sub(x, y)
b. def sub(x, y)
71
Successful Pseudocoding
© Code for Schools, [Link]
d. def area(l, w)
return l * w
Variable Value
def perim(l, w)
return 2 * (l + w) length 4
result ←x l 4
if y < x w 3
result y← a 12
return result
p 14
length←4 z 12
width ← 3
a ← area(length, width)
p ← perim(length, width)
z ← min(a, p)
e. def area(l, w)
return l * w
Variable Value
def perim(l, w)
return 2 * (l + w) a 4
def min(x, y) b 5
result ←x l 4
if y < x w 5
result y← z 18
return result
a ←4
b ← 5
72
Successful Pseudocoding
© Code for Schools, [Link]
f. def square(x)
s ← x * x Variable Value
return s
r 2
def area_circle(r)
x 2
a ← 3.1416 * square(r)
s 4
return a
a 12.5664
z ← area_circle(2) z 12.5664
73
Successful Pseudocoding
© Code for Schools, [Link]
7A - Initialising Lists
Lists are collections of values referenced by a single variable name.
For example, if we wanted to store 3 numbers: -45.2, 0.25, and 2.333, we could initialise 3
variables as follows:
number1 ← -45.2
number2 ← 0.25
number3 ← 2.333
Or we could use a list called names as follows:
We use square brackets [] to contain the values inside a list, and we use commas to separate
them.
integer1 ← 52
integer2 ← 21
integer3 ←1
Or we could use a list called integers as follows:
74
Successful Pseudocoding
© Code for Schools, [Link]
Examples:
Write code that would initialise the following lists:
75
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Write code that would initialise the following lists:
3. A list of integers called scores with the values: 150, 0, 100, 200
4. A list of floating point numbers called temperatures with the values: 15.3, 42.0, 32.1
5. A list of booleans called registered with the values: False, True, True, False
6. A list of: 2 lists of: numbers called invoices where each of the inner list of numbers
contains the values: 100.0, 16.5
Each inner list contains 3 floating point numbers recording the marks of a student.
76
Successful Pseudocoding
© Code for Schools, [Link]
7B - List Elements
Examples:
Consider the following lists:
77
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Identify the elements in the following lists:
Question
78
Successful Pseudocoding
© Code for Schools, [Link]
7C - Appending to Lists
Lists can be expanded after they are initialised by ‘appending’ elements to them.
Appending an element will add that element to the end of the list.
areas ← [24, 4, 6]
We can add the element 18 to the end of it by using the following pseudocode instruction:
[Link](18)
79
Successful Pseudocoding
© Code for Schools, [Link]
Examples:
Write down the code which will fulfil the requirements.
Then write down the final values in the list.
80
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Write down the code which will fulfil the requirements.
Then write down the final values in the list.
81
Successful Pseudocoding
© Code for Schools, [Link]
7D - List Indices
Because lists now combine all the values inside it into just one variable (with one name), we
need a way to refer to individual elements inside the list.
Definition: An index refers to the position of the element in the list. In our pseudocode
convention, the first position has an index of 0.
Examples:
Consider the list and questions in the previous exercise
plots ← [4, 3, 1, 5, 0, 2]
Exercises:
Question
82
Successful Pseudocoding
© Code for Schools, [Link]
7E - Indexing Lists
To read an element from a list, we need to refer to it using its index. The index is contained
between the [] brackets after the name of the list.
As with any variable, if we wish to read from it, it must be on the RHS of the assignment
operator ( ←).
Remember that the first element has the index of 0. This will trip you up, guaranteed! It even
trips up the most experienced of programmers sometimes!
Note that the [] is being used for a different purpose here than when it was used to initialise the
list (see above section).
volumes [2]
In the above code the square brackets are used to index the list. You can tell this is the case
because the value inside the brackets is an integer and it does not appear to the RHS of the
assignment operator.
Examples:
Consider the following list:
Write down the expression to index the following elements in the list:
1) 100 scores[0]
2) 67 scores[4]
3) 76 scores[2]
83
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Question
1.
marks ← [78, 100, 65, 59, 98, 45]
Write down the values of the following elements:
1) marks[2]
2) marks[0]
3) marks[5]
2.
marks ← [78, 100, 65, 59, 98, 45]
Write down the expression to index the following elements in the list:
1) 100
2) 59
3) 78
3.
diameters ← [ 3.4, 0, 1.2, 2.1, 3.5, 0.33 ]
Write down the values of the following elements:
1) members [1]
2) members [5]
3) members [0]
4.
diameters ← [ 3.4, 0, 1.2, 2.1, 3.5, 0.33 ]
Write down the expression to index the following elements in the list:
1) 0.33
2) 0
3) 1.2
84
Successful Pseudocoding
© Code for Schools, [Link]
points ← [2, 0, 3, 4, 1, 5]
x ←5
points[x]
Will also be the same as points[5]
y←6
x←2
points[y / x]
Will evaluate to points[3]
In terms of order of operations, the index operator [] is at the same level as the parentheses ().
points ← [2, 0, 3, 4, 1, 5]
points[points[2]]
85
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
Evaluate the index and write down the value of the element indexed
Question
1.
ids ← [2, 4, 1, 5, 3, 0]
n=2
ids[n]
2.
ids ← [2, 4, 1, 5, 3, 0]
n = 10
ids[n/2]
3.
ids ← [2, 4, 1, 5, 3, 0]
ids[ids[0]]
4.
ids ← [2, 4, 1, 5, 3, 0]
ids[ids[1] / 2]
5.
ids ← [2, 4, 1, 5, 3, 0]
attempts ← [3, 0, 1, 1, 1, 2]
id ← ids[5]
attempts [id]
6.
ids ← [2, 4, 1, 5, 3, 0]
attempts = [3, 0, 1, 1, 1, 2]
attempts [ids[3]]
86
Successful Pseudocoding
© Code for Schools, [Link]
We can use loops to inspect elements in a list by using the loop variable as the index into the
list.
Examples:
In the exercises below, walk through the code, fill in the trace table and attempt to determine
what the code is doing.
Note:
All questions below apply to the following list:
False,
found True
87
Successful Pseudocoding
© Code for Schools, [Link]
0, 17, 23,
total 44, 109,
115
17, 21, 65
v
88
Successful Pseudocoding
© Code for Schools, [Link]
89
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
In the exercises below, walk through the code, fill in the trace table and attempt to determine
what the code is doing.
Note:
All questions below apply to the following list:
ages ← [17, 6, 21, 65, 6]
1. n←4
while n >= 0:
print(ages[n])
n←n-1
2. n←0
found ← False
while n < 5:
if ages[n] == -32
found ← True
n←n+1
print(found)
3. i←0
a←0
while i < 5
a ← a+ ages[i]
i←i+1
a←a/5
print(a)
4. i←0
v ← ages[0]
while i < 5
if ages[i] < v
v ← ages[i]
i←i+1
print(v)
90
Successful Pseudocoding
© Code for Schools, [Link]
5. n←0
v1 ← ages[0]
while n < 5
if ages[i] < v1:
v1 ← ages[i]
n←n+1
n←0
v2 ← ages[0]
while n < 5:
if ages[i] < v2 and ages[i] > v1
v2 ← ages[i]
n←n+1
print(v2)
6. r ← []
n←4
while n >= 0
[Link](ages[n])
n←n-1
7. i←0
b ← False
while i < 5
j←i+1
while j < 5:
if ages[i] + ages[j] == 86
b ← True
j←j+1
i←i+1
print(b)
91
Successful Pseudocoding
© Code for Schools, [Link]
In the exercises below, write down the code to fulfill the requirements. You may wish to draw up
your own trace table if you wish to help.
Note:
All questions below apply to the following list:
Requirement Code
2. Determine whether or not an integer stored in the number variable exists in the list
(use a loop).
3. Create a new list, third, which contains only the first 3 elements.
Use a loop to do this.
5. Determine whether or not all the elements in the list are in ascending order (smallest to
largest).
6. Check whether the elements in the list is a palindrome - ie. the list is the same when
the elements are reversed
I.e. in this case, it is.
92
Successful Pseudocoding
© Code for Schools, [Link]
To change specific elements in a list, we can assign to an indexed element in the list
Remember this means that the indexed element must appear on the LHS of the assignment
operator (=).
marks[2] = 23
marks[0] =10 + 3 + 4
temperatures[3] = temperatures[5]
_ = Read
_ = Write
temperatures[3] = temperatures[5]
temperatures[3] = 19
Examples:
In the exercises below, walk through the code, fill in the trace table and attempt to determine
what the code is doing.
Note:
All questions below apply to the following list:
IDs ← [4, 3, 1, 2]
IDs [4,3,1,2]
[0,3,1,2]
[0,0,1,2]
[0,0,0,2]
[0,0,0,0]
93
Successful Pseudocoding
© Code for Schools, [Link]
IDs [4,3,1,2]
[2,3,1,2]
[2,2,1,2]
[2,2,2,2]
IDs [4,3,1,2]
[2,3,1,4]
[2,1,3,4]
94
Successful Pseudocoding
© Code for Schools, [Link]
Exercises:
In the exercises below, walk through the code, fill in the trace table and attempt to determine
what the code is doing.
Note:
All questions below apply to the following list:
IDs ← [4, 3, 1, 2]
1. i←3
while i >= 0
IDs[i] ← 1
i←i-1
2. n←0
while n < 4
IDs[n] ← - IDs[n]
n←n+1
3. i=0
temp ← IDs[0]
while i < :
IDs[i] ← IDs[i + 1]
i←i+1
IDs[3] ← temp
4. i←3
temp ← IDs[3]
while i > 0
IDs[i] ← IDs[i - 1]
i←i-1
IDs[0] ← temp
5. x←0
while x < 3
y←x+1
while y < 4
if IDs[y] > IDs[x]:
temp ← IDs[x]
IDs[x] ← IDs[y]
IDs[y] ← temp
y←y+1
x←x+1
95
Successful Pseudocoding
© Code for Schools, [Link]
6. x←0
swapped ← True
while swapped == True
x←0
swapped ← False
while x < 3
if IDs[x] > IDs[x + 1]:
temp ← IDs[x]
IDs[x] ← IDs[x + 1]
IDs[x + 1] ← temp
swapped ← True
x←x+1
96
Successful Pseudocoding
© Code for Schools, [Link]
In the exercises below, write down the code to fulfill the requirements. You may wish to draw up
your own trace table if you wish to help.
Note:
All questions below apply to the following list:
Requirement Code
3. Extension: Update the list so that all the negative numbers occur before the positive
ones. There is no need to do a complete sort.
4. Extension:
Write code to sort a list of numbers consisting only of 0s, 1s, and 2s without using a
nested loop.
97
Successful Pseudocoding
© Code for Schools, [Link]
98
Successful Pseudocoding
© Code for Schools, [Link]
Solutions to Exercises
2A
1.
d ←0 True
2.
x ← 30.0 - 2 True
3. 0→x False
(the assignment operator is in the
wrong direction)
4. (20 - 8) / 3 → y False
(the assignment operator is in the
wrong direction)
5.
x + 2 ←4 False
(LHS can only contain a single variable)
6.
z + x ← (10 + 2) + 3
False
(LHS can only contain a single variable)
7.
g ← ((10 / 2) ✕ (3 + 5)) - 3.3
True
8. a / 3 False
(No assignment operator)
9. x + z + 3 False
(No assignment operator)
10.
perimeter ← 2 * 10 + 2 * 5 True
99
Successful Pseudocoding
© Code for Schools, [Link]
2B
1.
y ← -3 x: -6
y: -3
x ← -6
2.
y ← 0.4 x: 0.2
y: 0.4
x ← y / 2
3.
y ← -3 + 2 x: 1
y: -1
x ← y + 2
2C
Pseudocode Trace-table
1.
a ← 10
Variable Value
b ← 20
a 10, 20
a ← b
b 20
b ← a
2.
x ← 10
Variable Value
y ← 20
x 10, 20
z ← x
y 20, 10
x ← y z 10
y ← z
100
Successful Pseudocoding
© Code for Schools, [Link]
3.
x ←8
Variable Value
y ← x - x / 2
x 8, 12
x ← y + x
y 4, 144
y ← x * x
4.
x ←1
Variable Value
y ← 1
x 1, 3
z ← x + y
y 1, 5
x ← y + z z 2, 8
y ← x + z
z ← x + y
2D
Pseudocode Trace-table
1.
a ← 10
Variable Value
a ← a / 2
a 10, 5
2.
x ←8
Variable Value
y ← (x + 4) / 3
x 8
y ← y * 3
y 4, 12, 8
y ← y - 4
101
Successful Pseudocoding
© Code for Schools, [Link]
3.
x ←4
Variable Value
y ← 8
x 4, 8
y ← x + y
y 8, 12, 4
x ← y - x
y ← y - x
2E
Goal Code
3. ●
●
Assign the value 2 to the variable l
Assign the value 3 to the variable w l ←2
● Assign the value 4 to the variable d
● Calculate the volume of a rectangle with w ← 3
a length given by l, a width given by w,
and a depth given by d d ← 4
● Assign the result to the variable v
v ← l * w * d
x ← x * 3
102
Successful Pseudocoding
© Code for Schools, [Link]
3A
Expression Evaluation
1. 5 == 2 False
2. 10 < 10 False
3. 2 != 2 False
4. 32 >= 32 True
5. (5 + 3) < 8 False
6. 7 + 2 * 2 == (11 - 0) True
9. False False
3B
Expression Evaluation
103
Successful Pseudocoding
© Code for Schools, [Link]
3C
Pseudocode Trace-table
1.
age ← 18
Variable Value
result ← x < 18
age 18
result False
2.
date ← 31/01/2001
Variable Value
return_by ← 28/02/2001
date 31/01/2001
late ← date > return_by
return_by 28/02/2001
late False
3.
height ← 180
Variable Value
height ← height / 100
height 180, 1.80
six_feet ← height >= 1.83
six_feet False
4.
t ← 200.0
Variable Value
t ← (t - 32.0) * 5/9
t 200.0,
boiling ← t >= 100 93.33
boiling False
104
Successful Pseudocoding
© Code for Schools, [Link]
5.
hours ← 150
Variable Value
age ← 19
hours 150
test_passed ← True
age 19
licensed ← hours > 120 and age > 18 and test_passed True
6.
distance ← 1500
Variable Value
distance ← distance / 1000
distance 1500, 1.5
fast_food ← True
fast_food True
rating ← 4 rating 4
order ← distance < 2.0 and rating >= 4 order False
and not fast_food
105
Successful Pseudocoding
© Code for Schools, [Link]
3D
Requirements Pseudocode
4A
1.
y ← -3
if y <= 0 Variable Value
y ←y*2 y -3, -6
2.
x ← 1
if x >= 1 Variable Value
x ←x+2 x 1, 3, -7
x ← x - 10
106
Successful Pseudocoding
© Code for Schools, [Link]
3.
x ←4
if x > 4 and x < 6 Variable Value
←x-1
x x 4, 2
x ← x / 2
4.
z ←10
if z > 0 and z < 50 Variable Value
←z/2
z z 10, 5, 3
5.
t ← 21
Variable Value
fan_on ← True
t 21
if t >= 20 and fan_on == False
6.
t ← 24
Variable Value
fan_on ← False
t 24
if t >= 20 and fan_on == False
fan_on ←
True fan_on False, True
if t <= 16 and fan_on == True
fan_on ← False
7.
y ←1
if y >= 0 Variable Value
y ←
-y y 1, -1, 10
if y <= 0
y ← y * -10
107
Successful Pseudocoding
© Code for Schools, [Link]
8.
x ←10
if x > 5 and x < 15 Variable Value
x ←
-x x -10, -10, 0, 3
if x <= 0
← x + 10
x
x ← x + 3
9.
day_hours ← 10
Variable Value
night_hours ← 2
day_hours 10, 11
is_night ← False
night_hours 2
if is_night and night_hours < 120
night_hours ←
night_hours + 1
is_night False
day_hours ← day_hours + 1
10.
x ←8
Variable Value
y ← 10
x 8
z ← 2
y 10
min ← x z 2
if y < x and y < z
min 8, 2
min y ←
if z < x and z < y
min ←z
108
Successful Pseudocoding
© Code for Schools, [Link]
11.
x ←8
Variable Value
y ← 10
x 8
z ← 2
y 10
max ← x z 2
if y > x and y > z
max 8, 10
max y←
if z > x and z > y
max ←z
4B
Requirements Pseudocode
credit ← True
2. ● Assign False to the variable, in_WA
● Assign 12 to the variable, time in_WA← True
● If in_WA then subtract 1 from the
variable time time ← 12
if in_WA
time ← time - 1
109
Successful Pseudocoding
© Code for Schools, [Link]
3. ●
●
Assign 18 to john_age
Assign 20 to jane_age john_age ← 18
● Assign 22 to jack_age
● Calculate the middle age and assign it jane_age ← 20
to the variable mid_age
jack_age ← 22
mid_age ← john_age
if jane_age >= jack_age and
jane_age <= john_age
mid_age ←jane_age
if jane_age >= john_age and
jane_age <= jack_age
mid_age ←jane_age
if jack_age >= jane_age
and jack_age <= john_age
mid_age ←jack_age
if jack_age >= john_age
and jack_age <= jane_age
mid_age ← jack_age
4C
1.
y← -2
if y > 0 Variable Value
y ←y+1 y -2, -4
else
y ←y-2
2.
z← 20
if z >= 30 Variable Value
z ← z - 20 z 20, 40, 10
else
z ← z + 20
110
Successful Pseudocoding
© Code for Schools, [Link]
z ←z/4
3.
x ←2
if x >= 2 Variable Value
x ←
x + 2 x 2, 4, 8
else if x < 0
x ←x-2
else
←4
x
x ← x * 2
4.
y ←0
if y > 10 Variable Value
y ←y+2 y 0, 4, 6, 3
y ← y + 4
else if y > 5
y ←y-2
y ← y + 4
else
←y+4
y
y ← y + 2
y ← y * 0.5
111
Successful Pseudocoding
© Code for Schools, [Link]
5.
a ← 16
Variable Value
b ← 20
a 16, 17
if a > 20 and b < 30
a ←b+2 b 20, 15
b ← a + 4
else if a > 10 and b < 30
a ←b-3
b ← a - 2
else if a > 0 and b < 50
a ←b+4
b ← a + 2
else
a ←b-4
b ← a - 2
6.
x ←2
if x >= 1 Variable Value
x ←
x - 1 x 2, 3
else if x >= 2
x ←
x + 1
else if x >= 3
x ←x-2
else
x ←0
112
Successful Pseudocoding
© Code for Schools, [Link]
7.
z ← 2
if z >= 2 Variable Value
z ←z + 1 z 2, 3, 2, 20
if z >= 3
z ←z - 1
if z >= 4
z ←z-2
else
z ← z * 10
4D
Requirements Pseudocode
GPA ←1
else
GPA ←0
113
Successful Pseudocoding
© Code for Schools, [Link]
y ←0
3. ●
●
Assign 2 to the variable, x
Assign 6 to the variable, y x ←2
● Assign 3 to the variable, z
● Determine the which value is the y ← 6
product of the other 2 values
and assign that value into the z ← 3
variable, multiple
● If no values are a multiple of the multiple ←
-1
other 2, then assign -1 to the if x == y * z
variable, multiple
multiple x←
else if y == x * z
multiple y←
else if z == x * y
multiple ←z
114
Successful Pseudocoding
© Code for Schools, [Link]
4. ●
●
Assign 4 to the variable, w
Assign 2 to the variable, x w ←4
● Assign 6 to the variable, y
● Assign 3 to the variable, z x ← 2
● Determine the which is the
largest value and assign that y ← 6
value into the variable, max
z ← 3
max ← w
if x > w and x > y and x > z
max x←
else if y > w and y > x and y > z
max y←
else if z > w and z > x and z > y
max ←z
5A
1.
x← 3
while x > 0 Variable Value
x ←x-1 x 3, 2, 1, 0
2.
x← 0
while x <= 6 Variable Value
x ←x+2 x 0, 2, 4, 6, 8
115
Successful Pseudocoding
© Code for Schools, [Link]
3.
i ← 10
Variable Value
t ← 0
i 10, 11, 12, 13, 14,
while i <= 15
15, 16
t ←t+i t 10, 21, 33, 46, 60,
i ← i + 1
75
4.
a ←1
Variable Value
b ← 2
a 0, 1, 3, 8, 21, 55
while a <= 21
b ← a + b
5.
x ←0
Variable Value
sum ← 0
x 0, 0.5, 1, 1.5, 2, 2.5
while x <= 2
sum ← sum + y
sum 0, 0.75, 1.75, 2.5
x ← x + 0.5
6.
x ←0
Variable Value
min ← ∞
x 0, 0.5, 1, 1.5, 2, 2.5
while x <= 2
116
Successful Pseudocoding
© Code for Schools, [Link]
7.
x ←0
Variable Value
max ← -∞
x 0, 0.5, 1, 1.5, 2, 2.0
while x <= 2
L ←b
else
L ←a+b
a ← b
b ← L
n ← n + 1
117
Successful Pseudocoding
© Code for Schools, [Link]
5B
Requirements Pseudocode
sum← sum + i
i ← i + 2
sum← sum + i * i * i
i ← i + 1
factorial ← factorial * i
i ←i-1
4. ● Find the minimum value of
y = sin x + cos x for x ∈ [0, 4], x ∈ Z x ←0
min ← ∞
while x <= 4
y ←sin x + cos x
if y < min
min ←y
x ←x+1
118
Successful Pseudocoding
© Code for Schools, [Link]
y ← sin x + cos x
sum ← sum + y
x ← x + 1
t a←
else if n == 2
t b←
else if n == 3
t ←c
else
←a+b+c
t
a ← b
b ← c
c ← t
n ← n + 1
119
Successful Pseudocoding
© Code for Schools, [Link]
5C
Exercises:
Requirements Pseudocode
1. loge2 =
n ←1
sum ← 0
sign ← 1
● Calculate ln 2 using the above
series up to 200 terms
denom ← 1
while n <= 200
← sign * 1 / denom
term
sign ← -sign
denom ← denom + 1
n ← n + 1
ln2 ← sum
120
Successful Pseudocoding
© Code for Schools, [Link]
2.
● Calculate
3
4
using the a ← -2
b ← -1
bisection method
c ← (a + b) / 2
f_c ← c - 43
accuracy ← |f_c|
answer ← a
while accuracy > 0.01
f_a ←a 3
- 4
f_b ← b 3
- 4
if f_a * f_c < 0
b ←c
else
a←c
c ← (a + b) / 2
f_c ← c - 4 3
accuracy ← |f_c|
answer ← c
121
Successful Pseudocoding
© Code for Schools, [Link]
x ← a
while x < b
f_x ← 9 - 0.1x 2
x1 ← x + step
f_x1 ← 9 - 0.1x1 2
sum ← sum+(f_x+f_x1)/2*step
x ← x1
—--------------- OR —-------------
a ←2
b ← 5
f_a ← 9 - 0.1a 2
f_b ← 9 - 0.1b 2
n ← 6
step ← (b - a) / n
sum ← 0
x ← a + step
while x < b
← 9 - 0.1x
f_x 2
x ← x + step
122
Successful Pseudocoding
© Code for Schools, [Link]
6A
1. def sub(x, y)
z ← sub(100, 90) y 90
diff 10
z 10
2. def sub(x, y)
return x - y
Variable Value
z ← sub(10, 9) x 10
y 9
z 1
3. def double(x)
db ← x + x Variable Value
return db
x 9
z ← double(9) db 18
z 18
4. def halve(x)
return x / 2
Variable Value
num← 10 num 10
z ← halve(num) x 10
z 5
123
Successful Pseudocoding
© Code for Schools, [Link]
5. def energy(m)
return m * 3000000002
Variable Value
mass ←5 mass 5
z ← energy(mass) m 5
z 4.5 x 1017
6. def area(l, w)
a ← l * w Variable Value
return a
x 3
x ←3 y 4
y ← 4
l 3
z ← area(x, y)
w 4
a 12
z 12
7. def perimeter(l, w)
return 2 * (l + w)
Variable Value
x ←3 x 3
y ← 4 y 4
z ← perimeter(x, y)
l 3
w 4
z 14
124
Successful Pseudocoding
© Code for Schools, [Link]
8. def min(x, y)
6B
Requirements Pseudocode
Then:
● call the function
● supply it an argument of 2,
● and assign the returned value to the
variable c
125
Successful Pseudocoding
© Code for Schools, [Link]
Then:
● call the function
● supply it arguments of 1, 2, and 3
● and assign the returned value to the
variable d
Then:
d ← canDrive(17)
● call the function
● supply it arguments of 17 -----Alternative-----
● and assign the returned value to the
variable d def canDrive(age)
result← True
if x < 8
result ←False
return result
d ← canDrive(17)
5. Write a function: def max(x, y, z)
● named: max if x >= y and x >= z
● with 2 parameters: x, y, z return x
● which returns the largest of the values else if y >= x and y >= z
in the parameters return y
else
Then: return z
● call the function
● supply it arguments of -3, -4, 0
biggest ← max(-3, -4, 0)
● and assign the returned value to the
variable biggest
126
Successful Pseudocoding
© Code for Schools, [Link]
7A
1.
prices ← []
2.
areas ← [25, 30, 4]
3.
scores ← [150, 0, 100, 200]
4.
temperatures ← [15.3, 42.0, 32.1]
5.
registered ← [False, True, True, False]
6.
invoices ← [ [100.0, 16.5], [100.0, 16.5] ]
7.
marks ← [ [100.0, 99.2, 95.7], [76.2, 70.5, 78.0], [85.0, 65.4, 72.2] ]
7B
1. 1) ages
2) 16
3) 11
4) 65
5) 16
2. 1) squares
2) [2, 4]
3) [3, 9]
4) 16
5) 2
6) 16
127
Successful Pseudocoding
© Code for Schools, [Link]
7C
1.
pi_digits ← []
[Link](3)
[Link](1)
[Link](4)
2.
scores ← []
[Link](87)
[Link](93)
[Link](67)
3.
coords ← []
[Link]([-2, -4])
[Link]([0, 0])
[Link]([3, 4])
4.
ages ← [20, 14]
[Link](30)
[Link](22)
5.
students ←[ [145, 25], [146, 20] ]
[Link]([147, 22])
6.
nums ← []
i ← 100
while i <= 150
[Link](i)
i ←i+1
7.
countdown ← [200, 198]
n ← 196
while n >= 0
[Link](n)
n ←n-2
128
Successful Pseudocoding
© Code for Schools, [Link]
7D
1. 1) 0
2) 5
3) 2
4) 4
5) 1
2. 1) 0
2) 5
3) 1
4) 3
5) 5
6) 4
7E
1. 1) 65
2) 78
3) 45
2. 1) marks[1]
2) marks[3]
3) marks[0]
3. 1) 0
2) 0.33
3) 3.4
4. 1) members[5]
2) members[0]
3) members[2]
7F
1. 1
2. 0
3. 1
4. 1
5. 3
7. 2
129
Successful Pseudocoding
© Code for Schools, [Link]
7G
1.
Variable Value Output
n 4, 3, 2, 1, 0, -1 6, 65, 21, 6, 17
2.
Variable Value Output
n 0, 1, 2, 3, 4, 5 False
found False
3.
Variable Value Output
i 0, 1, 2, 3, 4, 5 23
4.
Variable Value Output
i 0, 1, 2, 3, 4, 5 6
v 17, 6
130
Successful Pseudocoding
© Code for Schools, [Link]
5.
Variable Value Output
n 0, 1, 2, 3, 4, 5, 17
0, 1, 2, 3, 4, 5
v1 17, 6
v2 17
6.
Variable Value Output
n 4, 3, 2, 1, 0, -1
r [6]
[6, 65]
[6, 65, 21]
[6, 65, 21, 6]
[6, 65, 21, 6, 17]
Creates a new list, r, which contains the elements in ages in reversed order.
131
Successful Pseudocoding
© Code for Schools, [Link]
7.
Variable Value Output
i 0, 1, 2, 3, 4 True
j 1, 2, 3, 4, 5,
2, 3, 4, 5,
3, 4, 5,
4, 5,
5
b False, True
7H
1. n←0
while n < 7
print(scores[n])
n←n+2
2. i←0
found ← False
while i < 7
if number == scores[i]
found ← True
i←i+1
print(found)
3. third ← []
n←0
while n < 3
[Link](scores[n])
n←n+1
132
Successful Pseudocoding
© Code for Schools, [Link]
4. i←0
product ← 1
while i < 7
product ← product * scores[i]
i←i+1
print(product)
5. ascending ← True
n←0
while n < 6
if scores[n + 1] < scores[n]
ascending ← False
n←n+1
print(ascending)
6. palindrome ← True
n←0
while n < 3
if scores[n] != scores[6 - n]
palindrome ← False
n←n+1
print(palindrome)
7. zero_sum ← False
x←0
while x < 6
y←x+1
while y < 7
if scores[x] + scores[y] == 0
zero_sum ← True
y←y+1
x←x+1
print(zero_sum)
133
Successful Pseudocoding
© Code for Schools, [Link]
134
Successful Pseudocoding
© Code for Schools, [Link]
7I
1.
Variable Value
i 3, 2, 1, 0, -1
IDs [4,3,1,2]
[4,3,1,1]
[4,3,1,1]
[4,1,1,1]
[1,1,1,1]
Overwrites all the list elements with the value 1, from the highest index down to the first
index.
2.
Variable Value
n 0, 1, 2, 3, 4
IDs [4,3,1,2]
[-4,3,1,2]
[-4,-3,1,2]
[-4,-3,-1,2]
[-4,-3,-1,-2]
3.
Variable Value
i 0, 1, 2, 3
temp 4
IDs [4,3,1,2]
[3,3,1,2]
[3,1,1,2]
[3,1,2,2]
[3,1,2,4]
“Rotates” all the elements in the list one position to the left.
135
Successful Pseudocoding
© Code for Schools, [Link]
4.
Variable Value
i 3, 2, 1, 0
temp 2
IDs [4,3,1,2]
[4,3,1,1]
[4,3,3,1]
[4,4,3,1]
[2,4,3,1]
“Rotates” all the elements in the list one position to the right.
5.
Variable Value
x 0, 1, 2, 3
y 1, 2, 3, 4,
2, 3, 4,
3, 4,
4
temp 1
IDs [4,3,1,2]
[4,3,2,1]
136
Successful Pseudocoding
© Code for Schools, [Link]
6.
Variable Value
x 0, 1, 2, 3, 4,
0, 1, 2, 3, 4,
0, 1, 2, 3, 4
temp 4,3
IDs [4,3,1,2]
[3,4,1,2]
[3,1,4,2]
[3,1,2,4]
[1,3,2,4]
[1,2,3,4]
137
Successful Pseudocoding
© Code for Schools, [Link]
7J
max_value ← scores[0]
i←1
while i < 7
if scores[i] > max_value
max_value ← scores[i]
i←i+1
i←0
while i < 7
scores[i] ← max_value:
i←i+1
x←0
while x < 6
y←x+1
while y < 7
if scores[y] > scores[x]
temp ← scores[x]
scores[x] ← scores[y]
scores[y] ← temp
y←y+1
x←x+1
print(scores[3])
138