GR CHECK - Module 9 Textbook Answers CUP
GR CHECK - Module 9 Textbook Answers CUP
Task 12.01
Task 12.02
These examples show the sort of detail students should show in their answers.
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
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
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
A better solution is to treat the tip and the base of the triangle separately and use the REPEAT loop for
the other lines.
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
PROCEDURE OutputBaseRow
FOR i 1 TO NumberOfSymbols
OUTPUT Symbol
NEXT i
OUTPUT Newline
ENDPROCEDURE