Homework #07 - Answers: Jatinder Uses Internet Banking. This Pseudocode Checks Her PIN
Homework #07 - Answers: Jatinder Uses Internet Banking. This Pseudocode Checks Her PIN
Question 1
c ← 0
INPUT PIN
x ← PIN
REPEAT
x ← INT(x/10)
c ← c + 1
UNTIL x < 1
IF c <> 5
THEN
PRINT "Error in PIN entered"
ELSE
PRINT "PIN OK"
ENDIF
What value of c and what message would be output if the following PINs were entered? [2]
51020 Value of c: 5
Message: PIN OK
5120 Value of c: 4
Start
c ← 0
INPUT PIN
x ← PIN
x ← x / 10
c ← c + 1
N Y
x < 1
N Y
c <> 5
Stop
Question 2
The flowchart inputs the size of a number of car engines; a value of –1 stops the input. This
information is output: average engine size and number of engines with size > 1.5
Complete the trace table for the input data. 1.8, 2.0, 1.0, 1.3, 1.0, 2.5, 2.0, 1.3, 1.8, 1.3, –1
Read this section of program code that inputs twenty (20) numbers and then outputs the largest
number input.
1 h = 0
2 c = 0
3 REPEAT
4 READ x
5 IF x > h THEN x = h
6 c = c + 1
7 PRINT h
8 UNTIL c < 20
line 7: PRINT h should come after the end of the repeat loop
Question 4
INPUT a, b, c
IF a > b AND a > c THEN PRINT a
ELSE IF b > c THEN PRINT b
ELSE PRINT c
Question 5
(You may use INT(x) in your answer, e.g. y = INT(3.8) gives the value y = 3) [4]
CountIntegers ← 0
FOR x ← 1 TO 1000
INPUT Number
Difference ← INT(Number) – Number
IF Difference = 0 THEN
CountIntegers ← CountIntegers + 1
ENDIF
NEXT x
PRINT CountIntegers
(NOTE: alternative Solution)
CountIntegers ← 0
FOR x ← 1 TO 1000
INPUT Number
IF INT(Number) = Number THEN
CountIntegers ← CountIntegers + 1
ENDIF
NEXT x
PRINT CountIntegers
Describe, with examples, two sets of test data you would use to test your algorithm. [2]
Read this section of program code that should input 10 positive numbers and then output the
smallest number input. [4]
1 Small = 0
2 Counter = 0
3 REPEAT
4 INPUT Num
5 IF Num < Small THEN Num = Small
6 Counter = Counter + 1
7 PRINT Small
8 UNTIL Counter < 10
There are four errors in this code.
Locate these errors and suggest a corrected piece of code for each error.
The flowchart below inputs the weight of a number of parcels in kilograms. Parcels weighing more
than 25 kilograms are rejected. A value of –1 stops the input.
The following information is output: the total weight of the parcels accepted and number of parcels
rejected. [4]
Complete the trace table for the input data:
1.8, 26.0, 7.0, 11.3, 10.0, 2.5, 25.2, 5.0, 19.8, 29.3, –1
Question 8
a variable is used to store data that can change during the running of a program
a constant is used to store data that will not be changed during the running of a
program
Question 9
Identify three different loop structures that you can use when writing pseudocode.
FOR … TO … NEXT
REPEAT … UNTIL
WHILE … DO … ENDWHILE
Question 10
Read this section of program code that should input 30 positive numbers and then output the largest
number input. [4]
1 Large = 9999
2 Counter = 0
3 WHILE Counter > 30
4 DO
5 INPUT Num
6 IF Num < Large THEN Large = Num
7 Counter = Counter - 1
8 ENDWHILE
9 PRINT Large
There are four errors in this code.
Locate these errors and suggest a corrected piece of code for each error.
Line 1 or Large =9999: this should read Large = 0
Line 3 or WHILE: this should read WHILE Counter < 30
line 6 or IF: this should read IF Num > Large THEN Large = Num
line 7 or Counter =…: this should read Counter = Counter + 1
Question 11
The flowchart below inputs six single digit numbers. The predefined function MOD gives the value
of the remainder, for example, Y ← 10 MOD 3 gives the value Y = 1
Complete a trace table for each of the two sets of input data. [4]
Set 1
5, 2, 4, 3, 1, 5
Set 2
3, 2, 1, 0, 7, 3
Identify a problem with this flowchart and explain how to correct it. [3]
Read this section of program code that should input 50 numbers and then output the average.
1 Total = 0
2 For Counter = 1 TO 50
3 INPUT Num
4 Total = Total + 1
5 Counter = Counter + 1
6 Average = Total/Counter
7 NEXT Counter
8 PRINT Average
The flowchart inputs an integer. The predefined function DIV gives the integer result of the
division, e.g. Y = 10 DIV 3 gives the value Y = 3. The predefined function MOD gives the
value of the remainder, e.g. Y = 10 MOD 3 gives the value Y = 1.
Complete a trace table for each of the two input values 5 and 12.
Trace table for input value 5
Identify two different conditional statements that you can use when writing pseudocode.
Read this section of program code that should input 50 numbers and then output the average of the
positive numbers only.
1 Total = 0
2 PosCount = 0
3 FOR Counter = 1 TO 50
4 INPUT Num
5 IF Num < 0 THEN Total = Total + Num
6 IF Num > 0 THEN Counter = Counter + 1
7 Average = Total/PosCount
8 NEXT Counter
9 PRINT Num
line 5 or IF Num < 0: this should read IF Num > 0 (THEN Total = Total + Num)
line 6 or (IF Num > 0 ) THEN Counter = Counter + 1:
this should read (IF Num > 0 THEN)Poscount = Poscount + 1
line 7 Average = Total/Poscount: this should come after the end of the repeat loop
line 9 or PRINT Num: this should read PRINT Average
Question 16
This pseudocode inputs an integer. The predefined function DIV gives the value of the division, e.g.
Y = 10 DIV 3 gives the value Y = 3. The predefined function MOD gives the value of the
remainder, e.g. Y = 10 MOD 3 gives the value Y = 1.
INPUT X
WHILE X > 15
DO
T1 ← X DIV 16
T2 ← X MOD 16
CASE T2 OF
10:OUTPUT A
11:OUTPUT B
12:OUTPUT C
13:OUTPUT D
14:OUTPUT E
15:OUTPUT F
OTHERWISE OUTPUT T2
ENDCASE
X ← T1
ENDWHILE
CASE X OF
10:OUTPUT A
11:OUTPUT B
12:OUTPUT C
13:OUTPUT D
14:OUTPUT E
15:OUTPUT F
OTHERWISE OUTPUT X
ENDCASE
Complete a trace table for each of the two input values 37 and 191.
Trace table for input value 37
Read this section of program code that inputs 10 positive numbers and then outputs the smallest
number input.
1 Small = 1000
2 Counter = 0
3 REPEAT
4 INPUT Num
5 IF Num < Small THEN Small = Num
6 Counter = Counter + 1
7 UNTIL Counter = 10
8 PRINT Small
Identify three changes you would need to make to find the largest number input instead of the
smallest number. [3]
1 Large = 0
2 Counter = 0
3 REPEAT
4 INPUT Num
5 IF Num > Large THEN Large = Num
6 Counter = Counter + 1
7 UNTIL Counter = 10
8 PRINT Large
Question 18
The flowchart below inputs the height of children who want to ride on a rollercoaster. Children
under 1.2 metres are rejected. The ride starts when eight children have been accepted. [4]
Complete the trace table for the input data:
1.4, 1.3, 1.1, 1.3, 1.0, 1.5, 1.2, 1.3, 1.4, 1.3, 0.9, 1.5, 1.6, 1.0
Question 19
FOR … TO … NEXT
a set number of iterations
WHILE … DO … ENDWHILE
used where the loop may never be executed/whilst a specified condition exists
Question 20
Read this section of program code that inputs 10 positive numbers and then outputs the total.
1 Total = 0
2 Counter = 0
3 REPEAT
4 INPUT Num
5 Total = Total + Num
6 PRINT Total
7 Counter = Counter + 1
8 UNTIL Counter = 10
This code works, but it is inefficient. Suggest three improvements that could be made. [3]
1 Total = 0
2 FOR Counter = 1 To 10
3 REPEAT
4 INPUT Num
5 UNTIL Num >0
6 Total = Total + Num
7 NEXT Counter
8 PRINT Total
Question 21
The flowchart below calculates the number of tins of paint required to paint walls. The flowchart
inputs the height and width of a wall in metres, the number of doors and the number of windows. A
value of –1 for the height stops the input.
Complete the trace table for the input data:
3, 5, 1, 0, 3, 7, 0, 0, 3, 5, 0, 3, 3, 7, 1, 1, –1, 0, 0, 0
Question 22
1 InRange = 0
2 OutRange = 1000
3 FOR Count = 1 TO 10
4 INPUT Num
5 IF Num > 10 AND Num < 20 THEN InRange = InRange + 1
6 ELSE OutRange = OutRange - 1
7 Count = Count + 1
8 NEXT X
9 PRINT InRange, OutRange
Line 2: OutRange = 0
Line 6: should be OutRange = OutRange + 1
Line 7: not needed
Line 8: NEXT X should be NEXT Count / Line 3: FOR Count = 1 TO 10 should be
FOR X = 1 TO 10
Decide, with reasons, whether the numbers 10 and 20 are within or outside the range. [4]
Question 23
The flowchart below inputs the price of an item under $10. The change from a $10 note is output.
Any amount less than 5 cents is rounded up to 5 cents.
The predefined function INT rounds a number down to the nearest whole number; for example
Z ← INT(5.7) gives the value Z = 5
Complete the trace table for the input data: 6.29
Question 24
REPEAT ... UNTIL and WHILE ... DO ... ENDWHILE are two different loop
structures you can use when writing pseudocode. Explain, using examples, why you would choose
to use each type of loop.
REPEAT
INPUT Number
Total ← Total + Number
UNTIL Number = 0
at least one repeat is required
WHILE Number <> -1 DO
INPUT Number
Total ← Total + Number
ENDWHILE
The loop may never be executed
Question 25
Read this section of program code that inputs positive numbers, discards any negative numbers and
then outputs the average. An input of zero ends the process.
1 Total = 0
2 Counter = 100
3 REPEAT
4 REPEAT
5 INPUT Num
6 UNTIL Num < 0
7 Total = Total + 1
8 Counter = Counter + Num
9 UNTIL Num = 0
10 Average = Total / (Counter - 1)
11 Print Average
The flowchart below inputs an integer. The predefined function DIV gives the value of the division,
for example Z ← 11 DIV 3 gives the value Z = 3. The predefined function MOD gives the value
of the remainder, for example Z ← 11 MOD 3 gives the value Z = 2.
Complete a trace table for each of the two input values 33 and 75.
Trace table for input value 33
Question 27
CASE X OF
1 : PRINT 'Option 1'
2 : PRINT 'Option 2'
3 : PRINT 'Option 3'
OTHERWISE PRINT 'Incorrect choice'
ENDCASE
– e.g. checking for discrete/large number/more than 2 of values
Question 28
Describe each of the following data types used in programming. In each case, give an example of
a piece of data to illustrate your answer. Each example must be different.
Char
Description:
A single character (from the keyboard)
Example:
A/#/2
String
Description:
An (ordered) sequence of characters
Example:
Hello world / #123?Y / 234 78963
Boolean
Description
A data type with two possible values
Example:
TRUE / FALSE
Question 29
OR
CASE X OF
1: PRINT ("ONE")
2: PRINT ("TWO")
OTHERWISE PRINT ("Less than ONE or more than TWO")
ENDCASE
An algorithm has been written in pseudocode to input 100 numbers, select and print the
largest number and smallest number.
Count ← 1
INPUT Number
High ← Number
Low ← Count
REPEAT
INPUT Number
IF Number > High THEN
High Number
ENDIF
IF Number > Low THEN
Low ← Number
ENDIF
Count ← Count + 1
UNTIL Count = 99
PRINT "Largest Number is ", Number
PRINT "Smallest Number is ", Low
Find the four errors in the pseudocode and suggest a correction for each error.
Low ← Count should be Low ← Number
Number > Low should be Number < Low
UNTIL Count = 99 should be UNTIL Count > 99 or UNTIL Count = 100 or UNTIL
Count >= 100 // Count ← 1 should be Count ← 0
PRINT "Largest Number is ", Number should be PRINT "Largest Number is ", High
Show how you would change the corrected algorithm to total the numbers and print the total.
Use a variable Total.
Count ← 1
INPUT Number
High ← Number
Low ← Number
Total ← Number
REPEAT
INPUT Number
Total ← Total + Number
IF Number > High THEN
High ← Number
ENDIF
IF Number < Low THEN
Low ← Number
ENDIF
Count ← Count + 1
UNTIL Count > 99
PRINT "Largest Number is ", High
PRINT "Smallest Number is ", Low
PRINT "Total is ", Total
Question 31
This flowchart inputs the marks gained in an examination. An input of –1 ends the routine.
Complete the trace table for the mark input data: 50, 70, 65, 30, 95, 50, 55, 85, 65, 35, –1, 45
Question 32
Describe the purpose of variables and constants. Use an example of each in your answer.
A value that can change during the execution of a program
A named value that cannot change during the execution of a program
Variable example - using a counter for example counter ← counter + 1
Constant example – a static value that can be used for checking for example maxAge
Question 33
This pseudocode algorithm calculates the weight and number of bags in a load of firewood. The
weight in kilograms of each bag is input. The algorithm finishes when either 50 bags have been
weighed, or as soon as the total weight exceeds 1000 kilograms. Only then are the total weight and
the number of bags in the load output.
01 TotalWeight ← 1000
02 BagCount ← 0
03 MaxBag ← 50
04 MaxWeight ← 1000
05 REPEAT
06 OUTPUT "Please Enter weight of bag"
07 INPUT Weight
08 TotalWeight ← TotalWeight + Weight
09 BagCount ← BagCount + 1
10 OUTPUT "Number of bags in the load is ", BagCount
11 UNTIL TotalWeight > MaxWeight AND BagCount >= MaxBag
12 OUTPUT "Total weight of the load is ", MaxWeight
Line 10 move OUTPUT "Number of bags in the load is ", BagCount to end /
after line 11
Line 11 UNTIL TotalWeight > MaxWeight OR BagCount >= MaxBag
An algorithm has been written in pseudocode to check the temperature readings taken from a
freezer are within the range –18 degrees to –25 degrees inclusive.
The algorithm counts the number of times that the temperature reading is below –25 degrees and the
number of times that the temperature reading is above –18 degrees.
An engineer is called if there are more than 10 temperature readings below –25 degrees.
An alarm sounds if there are more than 5 temperature readings above –18 degrees.
01 TooHot ← 0
02 TooCold ← 1000
03 REPEAT
04 OUTPUT "Please enter temperature"
05 INPUT Temperature
06 IF Temperature < -25
07 THEN
08 TooCold ← TooCold – 1
09 ENDIF
10 IF Temperature > -18
11 THEN
12 TooHot ← TooHot + 1
13 ENDIF
14 UNTIL TooHot > 5 OR TooCold > 10
15 IF TooHot < 5
16 THEN
17 INPUT "Alarm!!"
18 ENDIF
19 IF TooCold > 10
20 THEN
21 OUTPUT "Call the Engineer"
22 ENDIF
This flowchart represents an algorithm that allows the input of two numbers and performs a
calculation. The predefined function MOD finds the remainder from integer division for example
X ← 8 MOD 5 gives X a value of 3.
Complete a trace table for this set of input data:
11, 4, 6, 2, 3, 9, 3, 2, 2, 6, 0, 0, 1, 1
Name and describe the most appropriate programming data type for each of the examples of data
given. Each data type must be different.
Data: 37
Read this section of code that inputs the ages of people entering an event. The input sequence is
ended by inputting a negative value for age. The code outputs the number of people at the event
over the age of 18.
01 Num18 = 0
02 INPUT Age
03 WHILE Age >= 0 DO
04 IF Age >= 18 THEN
05 Num18 = Num18 + Age
06 ENDIF
07 ENDWHILE
08 PRINT Num18 – Age
Explain the differences between a WHILE … DO … ENDWHILE and a REPEAT … UNTIL loop.
This section of program code asks for 50 numbers to be entered. The total and average of the
numbers are calculated.
1 Total = 0
2 Counter = 50
3 PRINT ′When prompted, enter 50 numbers, one at a time′
4 REPEAT
5 PRINT ′Enter a number′
6 INPUT Number
7 Total + Number = Total
8 Number = Number + 1
9 UNTIL Counter = 50
10 Average = Number * Counter
11 PRINT ′The average of the numbers you entered is ′, Average
Write an algorithm, using pseudocode, to input a number between 0 and 100 inclusive. The
algorithm should prompt for the input and output an error message if the number is outside this
range.
OUTPUT "Enter a number between 0 and 100 "
INPUT Number
IF Number < 0 OR Number > 100 THEN
OUTPUT "The number you have entered is outside the specified range"
ENDIF
Question 45
Write an algorithm to input three different numbers, and then output the largest number. Use either
pseudocode or a flowchart.
INPUT Num1, Num2, Num3
IF (Num1 > Num2) AND (Num1 > Num3) THEN PRINT Num1 ENDIF
IF (Num2 > Num1) AND (Num2 > Num3) THEN PRINT Num2 ENDIF
IF (Num3 > Num1) AND (Num3 > Num2) THEN PRINT Num3 ENDIF
or
INPUT Num1
Big ← Num1
INPUT Num2, Num3
IF Num2 > Big THEN Big ← Num2 ENDIF
IF Num3 > Big THEN Big ← Num3 ENDIF
PRINT Big
Give two sets of test data to use with your algorithm in previous question and explain why you
chose each set
Test data set 1:
30, 29, 28
Reason:
first number is the largest
Test data set 2:
x, y, z
Reason:
abnormal data, should be rejected
Question 47
This flowchart inputs the weight of items in kilograms to be loaded on a trailer. Any item over
25 kilograms is rejected. The trailer can take up to 100 kilograms.
Complete the trace table for the input data:
13, 17, 26, 25, 5, 10, 15, 35, 20, 15
Question 48
An algorithm has been written in pseudocode to input 100 numbers and print out the sum.
A REPEAT … UNTIL loop has been used.
Count ← 0
Sum ← 0
REPEAT
INPUT Number
Sum ← Sum + Number
Count ← Count + 1
UNTIL Count > 100
PRINT Sum
Error - Count ← 0
Correction - Count ← 1
or
Error - UNTIL Count > 100
Correction - UNTIL Count >= 100 or UNTIL Count = 100 Or UNTIL Count > 99
Rewrite the correct algorithm using a more suitable loop structure.
Sum ← 0
FOR Count 1 TO 100
INPUT Number
Sum Sum + Number
NEXT
PRINT Sum
Question 49
Question 50
IF … THEN … ELSE … ENDIF is one type of conditional statement used when writing
pseudocode. Identify and describe another type of conditional statement that you could use when
writing pseudocode. Give a reason why you would use this type of conditional statement.
Identification:
CASE …
… OF … OTHERWISE … (ENDCASE) or
… OF … (OTHERWISE) … ENDCASE
Description:
a statement that allows for multiple selections
Reason:
to simplify pseudocode/ make pseudocode more understandable etc.
Question 51
This flowchart checks a batch of 10 rice sacks for weight. Sacks should weigh 50 kilograms each.
Sacks weighing over 50.5 kilograms or less than 49.5 kilograms are rejected. The number of sacks
accepted and the number of sacks rejected is output.
An algorithm has been written in pseudocode to input some numbers and print out any numbers that
are greater than or equal to 100. The number 999 stops the algorithm.
INPUT Number
WHILE NUMBERS <> 999 DO
IF Number > 100 THEN PRINT Number ENDIF
ENDWHILE
PRINT Number
This flowchart inputs the weight in kilograms of a passenger stepping into a lift. The lift can take a
maximum of eight passengers or a maximum weight of 640 kilograms.
Complete the trace table for the passenger input data:
50, 70, 65, 100, 95, 50, 55, 85, 70, 75
Question 54
Value 1
2 boundary should be accepted as weight OK
Value 2
two erroneous/abnormal should be rejected
Question 55
Explain the difference between the programming concepts of sequence and selection. Include an
example of a programming statement for each concept in your explanation.
Sequence is the concept of one statement being executed after another whereas selection
decides which statement(s) are to be executed depending upon the result of a question
Sequence example
PRINT X
PRINT Y
Selection example
IF X > Y THEN PRINT X ELSE PRINT Y
Question 56
Write an algorithm to input 1000 numbers. Count how many numbers are positive and how many
numbers are zero. Then output the results. Use either pseudocode or a flowchart.
zero ← 0
posCount ← 0
FOR count ← 1 TO 1000
INPUT number
IF number > 0
THEN posCount ← posCount + 1
ENDIF
IF number = 0
THEN zero ← zero + 1
ENDIF
NEXT
OUTPUT posCount, " positive numbers"
OUTPUT zero, " zeros"
Give one change you could make to your algorithm to ensure initial testing is more manageable.
Reduce the number of iterations to a manageable amount
OR Simulate the input (e.g. random generation)
Question 57
A programmer has written a routine to check that prices are below $10.00. These values are used as
test data.
10.00 9.99 ten
Explain why each value was chosen.
10.00 boundary/erroneous data // the price should be rejected // value is out of range
9.99 boundary/extreme/normal data // the prices should be accepted // value is within normal
range
ten erroneous/abnormal data // input should be rejected // value is wrong type
Question 58
Explain the difference between the programming concepts of counting and totalling. Include an
example of a programming statement for each concept in your explanation.
Totalling is used to sum a list of numbers
Counting is used to find how many numbers/items there are in a list.
Totalling example
Total = Total + Number
Counting example
Counter = Counter + 1
Question 59
Draw a flowchart for an algorithm to input numbers. Reject any numbers that are negative and
count how many numbers are positive. When the number zero is input, the process ends and the
count of positive numbers is output.
Explain the changes you will make to your algorithm to also count the negative numbers.
Use another counter/variable
Update this counter/variable when the number is less than zero/count all numbers and
subtract the positive numbers
Output this counter/variable at the end // Output both counters at the end
Question 60
This pseudocode algorithm inputs two non-zero numbers and a sign, and then performs the
calculation shown by the sign. An input of zero for the first number terminates the process.
INPUT Number1, Number2, Sign
WHILE Number1 <> 0
IF Sign = '+' THEN Answer ← Number1 + Number2 ENDIF
IF Sign = '-' THEN Answer ← Number1 - Number2 ENDIF
IF Sign = '*' THEN Answer ← Number1 * Number2 ENDIF
IF Sign = '/' THEN Answer ← Number1 / Number2 ENDIF
IF Sign <> '/' AND Sign <> '*' AND Sign <> '-' AND Sign <> '+'
THEN Answer ← 0
ENDIF
IF Answer <> 0 THEN OUTPUT Answer ENDIF
INPUT Number1, Number2, Sign
ENDWHILE
Show how you could improve the algorithm written in pseudocode by writing an alternative type of
conditional statement in pseudocode.
CASE Sign OF
ꞌ+ꞌ : Answer ← Number1 + Number2
ꞌ-ꞌ : Answer ← Number1 - Number2
ꞌ*ꞌ : Answer ← Number1 * Number2
ꞌ/ꞌ : Answer ← Number1 / Number2
OTHERWISE Answer ← 0
ENDCASE
Question 61
An algorithm has been written in pseudocode to select a random number using the function
RandInt(n), which returns a whole number between 1 and the argument n. The algorithm then
Find the four errors in the pseudocode and suggest a correction to remove each error.
IF TotalTry > Number should be IF Guess > Number
IF Guess > Number should be IF Guess < Number
TotalTry ← Guess + 1 should be TotalTry ← TotalTry + 1
UNTIL Guess <> Number should be UNTIL Guess = Number
Question 63
The flowchart checks the lengths of a batch of 10 ropes. For the batch to be accepted 90% of the
lengths need to be between 24.9 and 25.1 metres.
Complete the trace table for the input data:
24.88, 25.01, 24.98, 25.00, 25.05, 24.99, 24.97, 25.04, 25.19, 25.07
It has been decided to only reject batches of rope that contain ropes that are too short.
State the change required to the algorithm.
Remove Length < 25.1 AND
Explain how the algorithm to reject batches could be improved to make it more effective.
Check the reject counter after each incrementation/remove reject check after counter
= 10
as soon as Reject = 2 / >1 ...
reject batch and end
Question 64
A programmer writes a program to weigh baskets of fruit in grams, keeping a total of the weight
and counting the number of baskets. The total weight is stored in a variable Total and the number
of baskets is stored in a variable BasketCount.
Explain, including examples of programming statements, how totalling and counting could be used
in this program.
Totalling:
Adding the weight of each basket to the total weight as each weight is entered
Total = Total + Weight
Counting:
Adding one to/incrementing the number of baskets as each weight is entered
BasketCount = BasketCount + 1
Question 65
Name the type of statement demonstrated by the use of IF … THEN … ELSE … ENDIF
An algorithm has been written in pseudocode to input 50 numbers and total only the positive
numbers.
Count ←
Total ← Count
REPEAT
INPUT Number
IF Number <> 0 THEN
Total ← Total + Count
ENDIF
Count ← Count + 1
UNTIL Count < 50
PRINT Total
Find the four errors in the pseudocode and suggest a correction for each error
Total ← Count should be Total ← 0
Number <> 0 should be Number > 0
Total ← Total + Count should be Total ← Total + Number
UNTIL Count < 50 should be
UNTIL Count > 50, UNTIL Count >= 51, UNTIL Count = 51
Show how you would change the corrected algorithm to only total numbers greater than 0 and less
than 20.
The test should be IF Number > 0 AND Number <20
Question 67
This flowchart inputs the type of passenger for a survey: S for Senior, A for Adult, C for Child. All
other values are ignored, apart from Z which ends the process.
Complete the trace table for the passenger input data:
S, S, S, A, C, C, C, A, A, A, A, W, S, S, D, C, Z, D, S
Question 68
An algorithm has been written in pseudocode to input the weight of 500 items and reject any that
are over-weight or under-weight, then print the percentage rejected.
Count ← 1
Reject ← 0
Over ← 62
Under ← 58
REPEAT
INPUT ItemWeight
IF ItemWeight > Over AND ItemWeight < Under THEN
Reject ← Reject – 1
ENDIF
Count ← Count + 1
UNTIL Count > = 500
Reject ← Reject / 100
PRINT "Percentage rejected is ", Reject
Find the four errors in the pseudocode and suggest a correction for each error.
Count ← 1 should be Count ← 0 or Count >= 500 should be Count > 500
AND should be OR
Reject ← Reject – 1 should be Reject ← Reject + 1
Reject ← Reject/100 should be Reject ← Reject/5 or Reject * 100 / 500
Describe how you would change the corrected algorithm to calculate the number accepted
instead of rejected, using a variable Accept, and print a warning if fewer than 50% are
accepted.
Add Accept ← 0 at start
Add ELSE Accept ← Accept + 1 after THEN AND Over and
Under defined/position described
OR
Add Accept ← Accept + 1 after THEN AND Replace IF
statement with …<= Over AND …>= UNDER … /position described
Add Accept ← Accept/5 after UNTIL AND correct loop/position described
ADD IF Accept < 50 THEN PRINT "Less than 50% accepted" at end
Accept ← 0
Count ← 1 // 0
Reject ← 0
Over ← 62
Under ← 58
REPEAT
INPUT ItemWeight
IF ItemWeight > Over OR ItemWeight < Under
//IF ItemWeight <= Over ANDItemWeight >= Under THEN
Reject ← Reject + 1
ELSE
Accept ← Accept + 1//ELSE not required
ENDIF
Count ← Count + 1
UNTIL Count > 500 // >= 500
Accept ← Accept / 5
IF Accept < 50 THEN
PRINT "Less than 50% accepted"
ENDIF
Question 69
This flowchart inputs the tread depth of five tyres, four on the car and a spare tyre. Any tread
depth of 1.6mm or less is rejected. To be potentially roadworthy, a car must have four tyres with a
tread depth greater than 1.6mm.
Complete Trace table 1 for the tread depth input data:
1.7, 1.9, 1.4, 1.8, 2.0
Identify and describe three loop structures that are available in pseudocode.
FOR … TO … NEXT
fixed number of repetitions
REPEAT … UNTIL
always executed // condition tested at end
WHILE … DO … ENDWHILE
may not be executed // condition tested at beginning
Question 71
This flowchart inputs student percentage marks for three examinations. If the average of these
marks is 80% or over then a distinction grade is awarded. If the average of these marks is less than
40% then a fail grade is awarded. Otherwise a pass grade is awarded.
Complete a trace table for each set of input data:
Set 1: 88, 74, 60
It has been decided to include an extra grade of Merit when the average of the marks is 60% or
more, and less than 80%. Describe the changes that will need to be made to the flowchart.
Add extra decision box …
… in an appropriate position between the average calculation and the output
Check for average greater than or equal to 60 and less than 80
Output Merit if average greater than or equal to 60 (and less than 80) …
… otherwise continue
Question 72
This algorithm accepts weights of bags of cookies. Any cookie bag weighing between 0.9 and 1.1
kilograms inclusive is acceptable. Underweight bags weigh less than 0.9 kilograms and overweight
bags weigh more than 1.1 kilograms. An input of a negative number stops the process. Then the
total number of bags, the number of overweight bags and the number of underweight bags weighed
are output.
Accept ← 0
Over ← 0
Under ← 0
OUTPUT "Enter weight of first cookie bag"
INPUT BagWeight
WHILE BagWeight > 0
IF BagWeight > 1.1 THEN
Error ← 1
ELSE
IF BagWeight < 0.9 THEN
Error ← 2
ELSE
Error ← 0
ENDIF
ENDIF
CASE Error OF
0 : Accept ← Accept + 1
1 : Over ← Over + 1
2 : Under ← Under + 1
ENDCASE
OUTPUT "Weight of next bag?"
INPUT BagWeight
ENDWHILE
Total ← Accept – Over – Under
OUTPUT "Number of bags weighed ", Total
OUTPUT "Number overweight ", Over
OUTPUT "Number underweight ", Under
Complete a trace table for the given algorithm using this input data:
1.05, 0.99, 1.2, 0.85, 1.1, 0.9, 1.5, 0.95, 1.05, 1.00, 1.07, 0.89, –10
Write an algorithm in pseudocode to input 500 positive whole numbers. Each number input must be
less than 1000. Find and output the largest number input, the smallest number input and the range
(difference between the largest number and smallest number).
Large ← 0
Small ← 1000
FOR Count ← 1 TO 500
REPEAT
OUTPUT "Enter a whole number between 1 and 999"
INPUT Number
UNTIL Number >= 1 AND Number < 1000 AND Number = Number DIV 1
IF Number < Small THEN
Small ← Number
ENDIF
IF Number > Large THEN
Large ← Number
ENDIF
NEXT
Range ← Large – Small
OUTPUT "Largest number is ", Large, " Smallest number is ", Small,
Describe how the algorithm could be changed to make testing less time-consuming.
Reduce the amount of numbers entered
By decreasing the final value of the loop
or
Remove the need to input values
By using random numbers / a previously populated array
Question 75
Complete the trace table for the algorithm using this input data:
Secret, Secret, VerySecret, VerySecret, Pa55word, Pa55word, 999, 888
Explain how the algorithm could be extended to allow three attempts at inputting the matching
password. Any pseudocode statements used in your answer must be fully explained.
Position: before INPUT PasswordRepeat // at start
…use a (variable) counter (for number of tries) or flag
… initialise variable counter or flag
Position after IF Length(Password) >= 8 THEN or after INPUT
PasswordRepeat
… insert REPEAT/WHILE/(conditional) loop
Position after OUTPUT "Reject"
… add one to counter (for number of tries)
… output a message "Try again"
… add INPUT PasswordRepeat
Position after OUTPUT "Accept"
… reset flag to show password matched
Position after ENDIF
… (insert UNTIL/ENDWHILE)to exit the loop after three tries or if the repeated
password matches the original
Question 76
Write the extra pseudocode statements that are needed to count and output the number of students
under the age of 7. Use the variable CountUnder7; assume CountUnder7 has already been set
to zero. [4]
IF Age < 7
THEN
CountUnder7 ←CountUnder7 + 1
ENDIF
OUTPUT "There are ", CountUnder7, " students aged under 7."
Question 77
This flowchart inputs a whole number. The function INT returns the integer value of a number. For
example, INT (7.5) is 7
The algorithm shown by this flowchart allows the input of examination marks for a class of
students. A mark of –1 ends the process. If a mark is 80 or over then a distinction grade is awarded.
The number of distinctions for the whole class is calculated. If this is over 50% of the class, the
class is awarded a highly commended certificate. [5]
Complete a trace table for the algorithm using this input data:
88, 74, 60, 90, 84, 87, 95, 72, 84, 66, –1
Question 79
1 Continue 1
2 WHILE Continue = 0
3 OUTPUT "Enter 1 for +, 2 for -, 3 for * or 4 for /"
4 INPUT Operator
5 OUTPUT "Enter the first value"
6 INPUT Value1
7 OUTPUT "Enter the second value"
8 OUTPUT Value2
9 IF Operator
10 1: Answer Value1 + Value2
11 2: Answer Value1 - Value2
12 3: Answer Value1 * Value2
13 4: Answer Value1 / Value2
14 ENDCASE
15 OUTPUT "The answer is ", Value1
16 OUTPUT "Do you wish to enter more values (Yes or No)?"
17 INPUT MoreValues
18 IF MoreValues = "No"
19 THEN
20 Continue 1
21 ENDIF
22 UNTIL Continue = 0
Find the five errors in the pseudocode and suggest a correction for each error.
Line 8 OUTPUT Value2 – should be INPUT Value2
Method 2
Line 2 WHILE Continue = 0 should be REPEAT
Method 3
Line 2 WHILE Continue = 0 should be WHILE Continue = 1
Alternative answer
REPEAT
IF Operator < 1 OR Operator > 4 THEN
OUTPUT "Enter 1, 2, 3 or 4"
INPUT Operator
ENDIF
UNTIL Operator >= 1 AND Operator <= 4
Location in algorithm
After line 4 / between lines 2 and 5
Question 80
An algorithm has been written in pseudocode to generate 50 positive random integers with values
less than or equal to 100. These numbers are stored in the array NumRand[]
The function RandUp(X,Y) generates a random integer greater than X and less than or equal to
Y For example, RandUp(1,4) generates 2 or 3 or 4
1 Count 0
2 WHILE Counter > 50 DO
3 NumRand[Counter] RandUp(1,100)
4 Counter Counter – 2
5 ENDWHILE
Find the four errors in the pseudocode and write a correction for each error.
Line 1 should be Counter
1 Counter ← 0
2 WHILE Counter < 50 DO
3 NumRand[Counter] ← RandUp(0, 100)
4 Counter ← Counter + 1
5 ENDWHILE
The pseudocode for this algorithm could be shortened by the use of a FOR … NEXT loop.
The algorithm, shown by this flowchart, allows the input of examination marks for a class of
students. A mark of 999 ends the process. If a mark is 40 or over then a pass grade is awarded. The
number of pass grades is calculated for the whole class. If this is under 50% of the class, the class is
offered extra help
Complete a trace table for the algorithm using this input data:
88, 24, 60, 30, 44, 17, 25, 22, 54, 6, 999, −1
Question 83
The pre-defined function MOD gives the value of the remainder of integer division.
For example, R = 9 MOD 4 gives the value R = 1
First ← 0
Last ← 0
INPUT Limit
FOR Counter ← 1 TO Limit
INPUT Value
IF Value >= 100 THEN
IF Value < 1000 THEN
First ← Value DIV 100
Last ← Value MOD 10
IF First = Last THEN
OUTPUT Value
ENDIF
ENDIF
ENDIF
NEXT Counter
Complete the trace table for the algorithm using this input data:
8, 66, 606, 6226, 8448, 642, 747, 77, 121
Describe the purpose of the algorithm.
checks for / outputs 3-digit numbers
… where the first and last digit are the same
Question 84
An algorithm allows a user to input their password and checks that there are at least eight characters
in the password. Then, the user is asked to re-input the password to check that both inputs are the
same. The user is allowed three attempts at inputting a password of the correct length and a
matching pair of passwords. The pre-defined function LEN(X) returns the number of characters in
the string, X
01 Attempt ← 0
02 REPEAT
03 PassCheck ← TRUE
04 OUTPUT "Please enter your password "
05 INPUT Password
06 IF LEN(Password) < 8
07 THEN
08 PassCheck ← TRUE
09 ELSE
10 OUTPUT "Please re-enter your password "
11 INPUT Password2
12 IF Password <> Password
13 THEN
14 PassCheck ← FALSE
15 ENDIF
16 ENDIF
17 Attempt ← Attempt + 1
18 UNTIL PassCheck OR Attempt <> 3
19 IF PassCheck
20 THEN
21 OUTPUT "Password success"
22 ELSE
23 OUTPUT "Password fail"
24 ENDIF
Identify the three errors in the pseudocode and suggest a correction to remove each error.
line 8 / PassCheck TRUE
correction PassCheck FALSE
line 12 / IF Password <> Password
correction IF Password2 <> Password // IF Password <> Password2
line 18 / UNTIL PassCheck OR Attempt <> 3
correction UNTIL PassCheck OR Attempt = 3 / UNTIL PassCheck OR Attempt >= 3
The algorithm includes two types of check on the data input.
Identify and describe each type of check.
Check: validation // length check
Description length check // checks number of characters in password
Check: verification // double entry
Description double entry // comparison that two inputs are the same
Give two sets of test data for this algorithm and a reason for choosing each set.
Each set of test data and its reason must be different.
Set 1 – any appropriate example e.g. “small”
Reason must follow through from the password given e.g. abnormal data will be
rejected
Set 2 – any different appropriate example e.g. “password” and “password”
Reason must be different and follow through from the password given e.g. normal data
will be accepted
Question 85
This algorithm checks the temperature of hot food being served to customers.
Complete the trace table for the algorithm using this input data:
75, 78, 84, 87, 91, 80, 75, 70, 65, 62, –1, 20
State how the final output from the algorithm could be improved.
include a message to explain the value output / e. g. “The percentage of meals not
served” // outputting Hot, Cold and Serve
Identify the process in the algorithm that is not required.
updating the Serve variable // Serve ← Serve + 1
Question 86
Describe what is meant by the terms variable and constant and give an example of each in your
answer.
variables are used to represent values that can change during the execution of a
program // variables can be used to
store the results of calculations / counting / totalling // can store values entered by the
user
variable example – any data that is input into a program such as a date
constants represent values that must stay the same throughout the execution of a
program
constant example – any value that does not change, such as Pi in mathematical
formulae
Question 87
The energy efficiency of an electrical appliance is the percentage of useful energy out compared
with the total energy in.
An algorithm has been written in pseudocode to calculate the energy efficiency of an appliance.
Values for total energy in and useful energy out are input. The efficiency is calculated and output as
a percentage. The entry of the number –1 for either value stops the algorithm.
01 REPEAT
02 OUTPUT "Enter total energy in "
03 INPUT TotalEnergyIn
04 OUTPUT "Enter useful energy out "
05 OUTPUT UsefulEnergyOut
06 IF TotalEnergyIn <> -1 AND UsefulEnergy <> -1
07 THEN
08 Efficiency (UsefulEnergyOut / TotalEnergyIn) * 100
09 OUTPUT "Efficiency is ", Efficiency, "%"
10 ENDIF
11 UNTIL TotalEnergyIn <> -1 OR UsefulEnergyOut <> -1
Write pseudocode to check for an efficiency of 92% or over for this appliance and to output
“A-rated” if the efficiency is 92% or over.
IF Efficiency >= 92
THEN
OUTPUT "A-rated"
ENDIF
Question 89
This flowchart represents an algorithm to find the average value of a number of sales.