Skip to content
Book 2: Fundamental Problem-solving and Programming Skills

Book 2 · Chapter 9

Algorithm Design and Problem-solving

§9.1§9.2Regular · ≈ 7 m · ↘B2·9

This chapter is in the ebook.

Read it in full on cswithzak.com. Every question has its mark scheme and every program is ready to run. The free practice for each section is below, with the short code the book prints beside it.

Open in the ebook

§9.1 Computational thinking skills

Short code B2·9·1

§9.2 Algorithms · Documenting and converting

Short code B2·9·2

Abstraction: keep only what matters

Short code B2·9·10

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Decomposition: one sub-problem, one module

Short code B2·9·11

FUNCTION GetValidMark() RETURNS INTEGER
   DECLARE Mark : INTEGER
   INPUT Mark
   WHILE Mark < 0 OR Mark > 100
      OUTPUT "Marks are 0 to 100: enter it again"
      INPUT Mark
   ENDWHILE
   RETURN Mark
ENDFUNCTION
FUNCTION GradeFor(Mark : INTEGER) RETURNS CHAR
   DECLARE Grade : CHAR
   IF Mark >= 50 THEN
      Grade ← 'P'
   ELSE
      Grade ← 'F'
   ENDIF
   RETURN Grade
ENDFUNCTION
PROCEDURE ShowResult(Number : INTEGER, Mark : INTEGER)
   OUTPUT "Student ", Number, ": ", Mark, " ", GradeFor(Mark)
ENDPROCEDURE
DECLARE Student : INTEGER
FOR Student ← 1 TO 3
   CALL ShowResult(Student, GetValidMark())
NEXT Student
One sub-problem, one module: two functions that each return one value, and a procedure that does a job. The main program is the last four lines. Marks typed: 72, 105 (refused), 45, 38.

Output

› 72
Student 1: 72 P
› 105
Marks are 0 to 100: enter it again
› 45
Student 2: 45 F
› 38
Student 3: 38 F
Run and checked before printing

What makes an algorithm

Short code B2·9·12

DECLARE Number : INTEGER
DECLARE Steps : INTEGER
INPUT Number
Steps ← 0
WHILE Number <> 1
   IF Number MOD 2 = 0 THEN
      Number ← Number DIV 2
   ELSE
      Number ← 3 * Number + 1
   ENDIF
   Steps ← Steps + 1
ENDWHILE
OUTPUT "Steps to reach 1: ", Steps
Predict: the user types 6. What is output? Write your answer down before you read on.
Run and checked before printing

Output

› 6
Steps to reach 1: 8
Run and checked before printing

Identifier tables

Short code B2·9·13

CONSTANT Days = 7
CONSTANT Target = 10000
DECLARE Day : INTEGER
DECLARE Steps : INTEGER
DECLARE Total : INTEGER
DECLARE OnTarget : INTEGER
DECLARE BestDay : INTEGER
DECLARE BestSteps : INTEGER
Total ← 0
OnTarget ← 0
BestSteps ← -1
FOR Day ← 1 TO Days
   INPUT Steps
   Total ← Total + Steps
   IF Steps >= Target THEN
      OnTarget ← OnTarget + 1
   ENDIF
   IF Steps > BestSteps THEN
      BestSteps ← Steps
      BestDay ← Day
   ENDIF
NEXT Day
OUTPUT "Total steps: ", Total
OUTPUT "Mean per day: ", Total / Days
OUTPUT "Days on target: ", OnTarget
OUTPUT "Best day: ", BestDay
Input, process, output. Every identifier in the table above is declared, and every one is given a value before it is read. The week's counts are 8200, 11400, 9600, 12500, 7300, 10000 and 11000.

Output

› 8200
› 11400
› 9600
› 12500
› 7300
› 10000
› 11000
Total steps: 70000
Mean per day: 10000
Days on target: 4
Best day: 4
Run and checked before printing

Writing pseudocode from a problem

Short code B2·9·14

DECLARE Time : REAL
DECLARE Fastest : REAL
DECLARE Under60 : INTEGER
DECLARE Swimmer : INTEGER
Under60 ← 0
FOR Swimmer ← 1 TO 12
   INPUT Time
   IF Swimmer = 1 THEN
      Fastest ← Time
   ELSE
      IF Time < Fastest THEN
         Fastest ← Time
      ENDIF
   ENDIF
   IF Time < 60 THEN
      Under60 ← Under60 + 1
   ENDIF
NEXT Swimmer
OUTPUT "Fastest time: ", Fastest
OUTPUT "Under 60 seconds: ", Under60
The Mark Ladder's model answer, run with twelve times. Notice 60 itself is not counted: the stem says under 60.

Output

› 62.5
› 58.25
› 71
› 59.5
› 60
› 57.75
› 64
› 66.5
› 59
› 70.25
› 61
› 63
Fastest time: 57.75
Under 60 seconds: 4
Run and checked before printing

Logic statements

Short code B2·9·15

FUNCTION CanEnter(Age : INTEGER, WithAdult : BOOLEAN) RETURNS BOOLEAN
   RETURN Age >= 16 OR (Age >= 12 AND WithAdult = TRUE)
ENDFUNCTION

OUTPUT "17, alone: ", CanEnter(17, FALSE)
OUTPUT "13, with an adult: ", CanEnter(13, TRUE)
OUTPUT "13, alone: ", CanEnter(13, FALSE)
OUTPUT "10, with an adult: ", CanEnter(10, TRUE)
The rule as one logic statement, tested with one value from each region of the rule. The brackets make the AND happen first.

Output

17, alone: TRUE
13, with an adult: TRUE
13, alone: FALSE
10, with an adult: FALSE
Run and checked before printing
DECLARE Age : INTEGER
Age ← 14
IF Age >= 12 AND <= 15 THEN
   OUTPUT "Junior"
ENDIF
Each side of AND must be a complete comparison. Written the way it is said aloud, the condition is not pseudocode at all.

Output

ERROR: Line 3: Unexpected '<=' — expected a value here
Run and checked before printing

Structured English

Short code B2·9·16

DECLARE Score : INTEGER
DECLARE Count : INTEGER
DECLARE Passes : INTEGER
Count ← 0
Passes ← 0
INPUT Score
WHILE Score <> -1
   Count ← Count + 1
   IF Score >= 50 THEN
      Passes ← Passes + 1
   ENDIF
   INPUT Score
ENDWHILE
IF Count > 0 THEN
   OUTPUT Passes * 100 / Count
ELSE
   OUTPUT "No scores"
ENDIF
The structured English above, step for step. Step 3 ('while') became WHILE … ENDWHILE, and step 6 is the second INPUT at the end of the loop body. Scores typed: 64, 45, 80, 50, then −1.

Output

› 64
› 45
› 80
› 50
› -1
75
Run and checked before printing
DECLARE Score : INTEGER
DECLARE Count : INTEGER
DECLARE Passes : INTEGER
Count ← 0
Passes ← 0
INPUT Score
WHILE Score <> -1
   Count ← Count + 1
   IF Score >= 50 THEN
      Passes ← Passes + 1
   ENDIF
   OUTPUT "Score ", Score, " | Count ", Count, " | Passes ", Passes
   INPUT Score
ENDWHILE
Trace proof: the values after each pass of the loop, scores 64, 45, 80, 50, −1.

Output

› 64
Score 64 | Count 1 | Passes 1
› 45
Score 45 | Count 2 | Passes 1
› 80
Score 80 | Count 3 | Passes 2
› 50
Score 50 | Count 4 | Passes 3
› -1
Run and checked before printing

Flowcharts

Short code B2·9·17

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Flowchart to pseudocode

Short code B2·9·18

DECLARE Number : INTEGER
DECLARE Binary : STRING
INPUT Number
Binary ← ""
REPEAT
   IF Number MOD 2 = 1 THEN
      Binary ← "1" & Binary
   ELSE
      Binary ← "0" & Binary
   ENDIF
   Number ← Number DIV 2
UNTIL Number = 0
OUTPUT Binary
The Mark Ladder's answer, run with 13: the remainders 1, 0, 1, 1 are put on the front of the string, so it reads 1101.

Output

› 13
1101
Run and checked before printing
DECLARE Number : INTEGER
DECLARE Digits : INTEGER
INPUT Number
Digits ← 0
WHILE Number > 0
   Digits ← Digits + 1
   Number ← Number DIV 10
ENDWHILE
OUTPUT Digits
The faded twin's answer, run with 4096.

Output

› 4096
4
Run and checked before printing

From pseudocode or structured English to a flowchart

Short code B2·9·19

DECLARE N : INTEGER
DECLARE Count : INTEGER
INPUT N
FOR Count ← 1 TO 4
   OUTPUT Count * N
NEXT Count
A FOR loop hides three steps: set the counter, test it, add 1 to it. The flowchart beside it shows all three. Input: 7.

Output

› 7
7
14
21
28
Run and checked before printing

Stepwise refinement

Short code B2·9·20

DECLARE Password : STRING
DECLARE Index : INTEGER
DECLARE Ch : STRING
DECLARE HasDigit : BOOLEAN
DECLARE HasUpper : BOOLEAN
INPUT Password
HasDigit ← FALSE
HasUpper ← FALSE
FOR Index ← 1 TO LENGTH(Password)
   Ch ← MID(Password, Index, 1)
   IF Ch >= "0" AND Ch <= "9" THEN
      HasDigit ← TRUE
   ENDIF
   IF Ch >= "A" AND Ch <= "Z" THEN
      HasUpper ← TRUE
   ENDIF
NEXT Index
IF LENGTH(Password) >= 8 AND HasDigit = TRUE AND HasUpper = TRUE THEN
   OUTPUT "Accepted"
ELSE
   OUTPUT "Rejected"
ENDIF
Level 3 written as pseudocode. Each refined step from the diagram is one or two lines here. Input: Sunfl0wer.

Output

› Sunfl0wer
Accepted
Run and checked before printing

Output

› garden12
Rejected
Run and checked before printing
DECLARE Number : INTEGER
DECLARE OddCount : INTEGER
DECLARE EvenTotal : INTEGER
OddCount ← 0
EvenTotal ← 0
INPUT Number
WHILE Number <> 0
   IF Number MOD 2 = 1 THEN
      OddCount ← OddCount + 1
   ELSE
      EvenTotal ← EvenTotal + Number
   ENDIF
   INPUT Number
ENDWHILE
OUTPUT "Odd numbers: ", OddCount
OUTPUT "Total of even numbers: ", EvenTotal
The five described steps, turned into pseudocode and run with 7, 4, 10, 3, 0: if the description were missing a step, this program would be missing a line.

Output

› 7
› 4
› 10
› 3
› 0
Odd numbers: 2
Total of even numbers: 14
Run and checked before printing

Command-word coach

Short code B2·9·21

Practice for this page of the book: the quiz, flashcards and past-paper questions for its section are listed above.

Workbook 2: Algorithm Design and Problem-solving

Short code W2·9 · Problem-solving and Programming Skills Workbook

The write-in workbook for this chapter has drills, exam-style practice, trace tables, fix-the-mistake and mark-it-yourself pages. Its full mark schemes and an interactive version arrive here with the chapter.

Ask Z about Algorithm Design and Problem-solving
Buy ZAK's books — WhatsApp the books desk: Buy the booksPrint or ebook