0% found this document useful (0 votes)
623 views7 pages

Chapter 12 Answers

This document provides tasks and questions related to algorithm design and problem solving. It includes examples of pseudocode for tasks such as making a sandwich or converting between inches and centimeters. It also provides exam-style questions testing skills like calculating checksums, counting positive/negative numbers, and calculating averages. The overall document provides material for students to practice and demonstrate their understanding of algorithmic problem solving and design.

Uploaded by

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

Chapter 12 Answers

This document provides tasks and questions related to algorithm design and problem solving. It includes examples of pseudocode for tasks such as making a sandwich or converting between inches and centimeters. It also provides exam-style questions testing skills like calculating checksums, counting positive/negative numbers, and calculating averages. The overall document provides material for students to practice and demonstrate their understanding of algorithmic problem solving and design.

Uploaded by

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

Chapter 12: Algorithm design and

problem solving: Answers to coursebook


questions and tasks
Syllabus sections covered: 9
It is suggested that Chapter 12 and 13 are worked through in parallel with Chapter 14.

Task 12.01

Task 12.02
These examples show the sort of detail students should show in their answers.

To make a sandwich To walk from college to shop To log on to computer


Cut two slices of bread. Exit college through the main Power up computer.
Spread butter on one side entrance. Wait for log-on screen.
of each slice of bread. Turn right and walk to the Enter username in the first
Lay a slice of cheese on the T-junction. text box.
buttered side of one slice Turn left and walk 50 metres. Enter password in the second
of bread. Cross the road: you are now text box.
Cover the cheese with the standing in front of the shop. Press Enter.
second slice of buttered
bread, with the buttered
side facing the cheese.

© Cambridge University Press 2019


Task 12.03
Identifier Explanation
Inches Length as a whole number of inches
Cm The result from using the given formula: Cm = Inches * 2.54
OUTPUT "Enter inches: "
INPUT Inches
Cm  Inches * 2.54
OUTPUT Cm, " cm"

Task 12.04
IF Age < 12 OR Age > 60 THEN fare is free

Question 12.01
The greater than signs (>) have to be replaced by smaller than signs (<):
IF Number1 < Number2
THEN // Number1 is smaller
IF Number1 < Number3
THEN
OUTPUT Number1
ELSE
OUTPUT Number3
ENDIF
ELSE // Number2 is smaller
IF Number2 < Number3
THEN
OUTPUT Number2
ELSE
OUTPUT Number3
ENDIF
ENDIF

Question 12.02
First part
The logic statement for Until has to be changed:
INPUT BiggestSoFar
Counter  1
REPEAT
INPUT NextNumber
Counter  Counter + 1
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar  NextNumber

© Cambridge University Press 2019


ENDIF
UNTIL Counter = 100
OUTPUT BiggestSoFar

Second part
An extra input is required and again the logic statement for Until has to be changed:
INPUT MaxNumbers
INPUT BiggestSoFar
Counter  1
REPEAT
INPUT NextNumber
Counter  Counter + 1
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar  NextNumber
ENDIF
UNTIL Counter = MaxNumbers
OUTPUT BiggestSoFar

Task 12.05
If it is not known how many times the loop will need to be executed, a REPEAT loop (or a WHILE loop) is more
appropriate. To be able to calculate the average, a count must be kept of how many numbers have been added
together. Note that when using a REPEAT loop the rogue value is counted as a number, so calculating the average
needs to use (Count – 1)
Using a REPEAT loop:
RunningTotal  0
Count  0
REPEAT
INPUT NextNumber
RunningTotal  RunningTotal + NextNumber
Count  Count + 1
UNTIL NextNumber = 0
OUTPUT RunningTotal
Average  RunningTotal / (Count – 1)
OUTPUT Average

Using a WHILE loop (note that rogue value is not counted as a number):
RunningTotal  0
Count  0
INPUT NextNumber
WHILE NextNumber <> 0 DO
Count  Count + 1
RunningTotal  RunningTotal + NextNumber
INPUT NextNumber
ENDWHILE
OUTPUT RunningTotal

© Cambridge University Press 2019


Average  RunningTotal / Count
OUTPUT Average

Task 12.06
01 // Set up initial values
01.1 INPUT symbol
01.2 // Input Max Number Of Symbols
01.2.1 REPEAT
01.2.2 INPUT MaxNumberOfSymbols
01.2.3 UNTIL MaxNumberOfSymbols MOD 2 = 1
01.3 NumberOfLeadingSpaces  (MaxNumberOfSymbols - 1) / 2
01.4 NumberOfSymbols  1
01.5 NumberOfMiddleSpaces  -1
02 REPEAT
03 // Output number of leading spaces
03.1 FOR i  1 TO NumberOfLeadingSpaces
03.2 OUTPUT Space // without moving to next line
03.3 NEXT i
04 // Output symbol, middle spaces, symbol
04.01 IF NumberOfSymbols = 1 // top of pyramid
04.02 THEN
04.03 OUTPUT Symbol
04.04 ELSE
04.05 IF NumberOfSymbols < MaxNumberOfSymbols
04.06 THEN
04.07 OUTPUT Symbol
04.08 FOR i  1 TO NumberOfMiddleSpaces
04.09 OUTPUT Space // no new line
04.10 NEXT i
04.11 OUTPUT Symbol
04.12 ELSE // output the final line
04.13 FOR i  1 TO NumberOfSymbols
04.14 OUTPUT Symbol // no new line
04.15 NEXT i
04.16 ENDIF
04.17 ENDIF
04.18 OUTPUT Newline // move to next line
05 // Adjust values for next row
05.1 NumberOfLeadingSpaces  NumberOfLeadingSpaces - 1
05.2 NumberOfMiddleSpaces  NumberOfMiddleSpaces + 2
05.3 NumberOfSymbols  NumberOfSymbols + 2
06 UNTIL NumberOfSymbols > MaxNumberOfSymbols

A better solution is to treat the tip and the base of the triangle separately and use the REPEAT loop for
the other lines.

© Cambridge University Press 2019


Task 12.07
CALL SetValues
CALL OutputTopRow
CALL AdjustValuesForNextRow
REPEAT
CALL OutputLeadingSpaces
CALL OutputRow
CALL AdjustValuesForNextRow
UNTIL NumberOfSymbols = MaxNumberOfSymbols
CALL OutputBaseRow

PROCEDURE SetValues
INPUT symbol
// Input Max Number Of Symbols
REPEAT
INPUT MaxNumberOfSymbols
UNTIL MaxNumberOfSymbols MOD 2 = 1
NumberOfLeadingSpaces  (MaxNumberOfSymbols - 1) / 2
NumberOfSymbols  1
NumberOfMiddleSpaces  -1
ENDPROCEDURE

PROCEDURE OutputTopRow
CALL OutputLeadingSpaces
OUTPUT Symbol
OUTPUT Newline
ENDPROCEDURE

PROCEDURE AdjustValuesForNextRow
NumberOfLeadingSpaces  NumberOfLeadingSpaces - 1
NumberOfMiddleSpaces  NumberOfMiddleSpaces + 2
NumberOfSymbols  NumberOfSymbols + 2
ENDPROCEDURE

PROCEDURE OutputLeadingSpaces
FOR i  1 TO NumberOfLeadingSpaces
OUTPUT Space // without moving to next line
NEXT i
ENDPROCEDURE

PROCEDURE OutputRow
OUTPUT Symbol
FOR i  1 TO NumberOfMiddleSpaces
OUTPUT Space // don’t move to next line
NEXT i

© Cambridge University Press 2019


OUTPUT Symbol
OUTPUT Newline // move to the next line
ENDPROCEDURE

PROCEDURE OutputBaseRow
FOR i  1 TO NumberOfSymbols
OUTPUT Symbol
NEXT i
OUTPUT Newline
ENDPROCEDURE

Exam-style Questions
1 Weighting  10
Total  0
FOR Count  1 TO 9
INPUT Digit
Value  Digit * Weighting
Total  Total + Value
Weighting  Weighting – 1
Count  Count + 1
NEXT Count
Remainder  Total MOD 11
CheckDigit  11 – Remainder
IF CheckDigit = 10
THEN
CheckDigit  X
ENDIF

Marking guidance:
1 mark for initialising Weighting and Total correctly
1 mark for correct loop structure
1 mark for correct input statement within loop
1 mark for correct assignment to Value within loop
1 mark for correct assignment to Total within loop
1 mark for correct updating of Weighting and Count
1 mark for correct assignment to Remainder
1 mark for correct assignment to CheckDigit
1 mark for correct IF statement structure and correct Boolean expression

© Cambridge University Press 2019


2 Positive  0
Negative  0
REPEAT
INPUT Number
IF Number > 0
THEN
Positive  Positive + 1
ELSE
Negative  Negative + 1
UNTIL Number = 0

Marking guidance:
1 mark for initialising Positive correctly
1 mark for initialising Negative correctly
1 mark for correct REPEAT loop structure and correct Boolean expression
1 mark for correct input statement within the loop
1 mark for correct IF THEN ELSE structure within loop
1 mark for correct Boolean expression
1 mark for correctly incrementing Positive and Negative

3 RogueValue  -1
Total  0
Count  0
INPUT Number
WHILE Number <>RogueValue DO
Count  Count + 1
Total  Total + Number
INPUT Number
ENDWHILE
If Count > 0
THEN
Average  Total / Count
OUTPUT Average
ENDIF

Marking guidance:
1 mark for initialising RogueValue
1 mark for initialising Total and Count correctly
1 mark for INPUT statement before loop
1 mark for correct loop structure and correct Boolean expression
1 mark for correct assignments to Count and Total within loop
1 mark for correct input statement as last statement within loop
1 mark for correct IF statement structure and correct Boolean expression
1 mark for correct assignment to Average and output Average

© Cambridge University Press 2019

You might also like