Book 2 · Chapter 9
Algorithm Design and Problem-solving
B2·9This 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.
For the whole chapter
- Topic pageAlgorithm Design and Problem-Solving
B2·9·T - QuizAlgorithm Design & Problem-Solving
B2·9·Q1 - LabFlowchart Studio
B2·9·X1 - LabSorting & searching visualiser
B2·9·X2 - LabTrace tables
B2·9·X3 - LabIdentifier table (Playground)
B2·9·X4
§9.1 Computational thinking skills
Short code B2·9·1
- FlashcardsFlashcards §9.1: Computational thinking & design tools
B2·9·F1 - Past-paper questionsPast-paper questions §9.1 (computational-thinking-skills)
B2·9·1
§9.2 Algorithms · Documenting and converting
Short code B2·9·2
- FlashcardsFlashcards §9.2: Algorithms — pseudocode, flowcharts & refinement
B2·9·F2 - Past-paper questionsPast-paper questions §9.2 (algorithms)
- Past-paper questionsPast-paper questions §9.2 (documenting-and-converting)
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 StudentOutput
› 72 Student 1: 72 P › 105 Marks are 0 to 100: enter it again › 45 Student 2: 45 F › 38 Student 3: 38 F
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: ", StepsOutput
› 6 Steps to reach 1: 8
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: ", BestDayOutput
› 8200 › 11400 › 9600 › 12500 › 7300 › 10000 › 11000 Total steps: 70000 Mean per day: 10000 Days on target: 4 Best day: 4
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: ", Under60Output
› 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
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)Output
17, alone: TRUE 13, with an adult: TRUE 13, alone: FALSE 10, with an adult: FALSE
DECLARE Age : INTEGER
Age ← 14
IF Age >= 12 AND <= 15 THEN
OUTPUT "Junior"
ENDIFOutput
ERROR: Line 3: Unexpected '<=' — expected a value here
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"
ENDIFOutput
› 64 › 45 › 80 › 50 › -1 75
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
ENDWHILEOutput
› 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
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 BinaryOutput
› 13 1101
DECLARE Number : INTEGER
DECLARE Digits : INTEGER
INPUT Number
Digits ← 0
WHILE Number > 0
Digits ← Digits + 1
Number ← Number DIV 10
ENDWHILE
OUTPUT DigitsOutput
› 4096 4
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 CountOutput
› 7 7 14 21 28
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"
ENDIFSunfl0wer.Output
› Sunfl0wer Accepted
Output
› garden12 Rejected
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: ", EvenTotalOutput
› 7 › 4 › 10 › 3 › 0 Odd numbers: 2 Total of even numbers: 14
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.


