0% found this document useful (0 votes)
8 views

GR CHECK - Module 9 Textbook Answers CUP

Uploaded by

medaaaf1
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)
8 views

GR CHECK - Module 9 Textbook Answers CUP

Uploaded by

medaaaf1
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

© Cambridge University Press 2019


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

© Cambridge University Press 2019


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
Average  RunningTotal / Count
OUTPUT Average

© Cambridge University Press 2019


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

© Cambridge University Press 2019

You might also like