0% found this document useful (0 votes)
847 views100 pages

Homework #07 - Answers: Jatinder Uses Internet Banking. This Pseudocode Checks Her PIN

Uploaded by

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

Homework #07 - Answers: Jatinder Uses Internet Banking. This Pseudocode Checks Her PIN

Uploaded by

Amira Okasha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Homework #07 – Answers

Question 1

Jatinder uses Internet banking. This pseudocode checks her PIN.

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

Message: Error in PIN entered


What type of validation check is being carried out here?
Length check
Flowchart

Start

c ← 0

INPUT PIN

x ← PIN

x ← x / 10

c ← c + 1

N Y
x < 1

N Y
c <> 5

OUTPUT "PIN OK" OUTPUT "Error in PIN entered"

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

Engine Count Number Size Average OUTPUT


0 0 0
1.8 1 1 1.8
3.8 2 2 2.0
4.8 3 1.0
6.1 4 1.3
7.1 5 1.0
9.6 3 6 2.5
11.6 4 7 2.0
12.9 8 1.3
14.7 5 9 1.8
16.0 10 1.3
-1 1.6 1.6 5
Question 3

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

There are three errors in this code.


Locate these errors and suggest a corrected piece of code.[3]

line 5: this should read IF x > h THEN h = x

line 7: PRINT h should come after the end of the repeat loop

line 8: this should read UNTIL c = 20 or UNTIL c >= 20 or UNTIL c > 19

Question 4

Write an algorithm, using pseudocode or flowchart only, which:

 inputs three numbers


 outputs the largest of the three numbers [3]

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

Write an algorithm, using pseudocode or flowchart only, which:

• inputs 1000 numbers


• outputs how many of these numbers were whole numbers (integers)

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

 1000 whole numbers to ensure that loop works properly


 900 whole numbers and 100 numbers with decimal places to ensure that the routine
distinguishes correctly
Question 6

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.

Line 1 or Small = 0: this should read Small = 999


line 5 or IF…: this should read IF Num < Small THEN Small = Num
line 8 or UNTIL: this should read UNTIL Counter = 10 or
UNTIL Counter > = 10 or
UNTIL Counter > 9
line 7 or PRINT…: PRINT Small should come after the end of the repeat loop
or
line 8 or UNTIL: this should come before line 7
Question 7

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

Explain the difference between a variable and a constant in a program.

 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

State the purpose of the flowchart [1]

(modulo 11) check digit calculation

Identify a problem with this flowchart and explain how to correct it. [3]

Problem – doesn’t deal correctly with remainder 10/a check digit of X

Solution – check Z for X as a final digit

– have a special case where check = 10

– accept where Check = 10 and F = X


Question 12

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

There are four errors in this code.


Locate these errors and suggest code corrections to remove each error.

line 4 or (Total =) Total + 1: this should read (Total =) Total + Num


line 5 or Counter = Counter + 1: delete this line
line 6 or (Average = )Total / Counter: swap lines 6 and 7
line 6 or (Average = )Total / Counter : this should read (Average =) Total / 50
Question 13

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

State the purpose of the flowchart


Converts a (denary) number to binary
Question 14

Identify two different conditional statements that you can use when writing pseudocode.

 IF … THEN … ELSE … ENDIF


 CASE … OF … OTHERWISE … ENDCASE
Question 15

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

There are four errors in this code.


Locate these errors and suggest code corrections to remove each error.

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

State the purpose of the pseudocode [1]

convert a denary number to hexadecimal and output it in reverse order


Question 17

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]

 Change variable name in every instance as needs to be meaningful e.g. Large


 Set this variable to a low value
 line 5: change comparison from < to >
Rewrite the program code with your changes. [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

REPEAT ... UNTIL is one type of loop structure.


Identify and describe two other types of loop structure that you could use when writing pseudocode.

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

 use FOR … NEXT instead of REPEAT … UNTIL


 Move PRINT to after the end of the loop
 Add error checking to check that the value input is positive
Rewrite the program code with your improvements.[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

Read this section of program code that:


 inputs 10 numbers
 checks whether each number is within a specified range
 totals the numbers within the range and outside the range

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

There are four errors in this code.


Locate these errors and suggest a correction to remove each error.

 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

There are four errors in this code.


Locate these errors and suggest a correction to remove each error.

 line 2 or Counter = 100


 Counter = 0
 line 6 or UNTIL Num < 0
 UNTIL Num >= 0
 line 7 or Total = Total + 1
 Total = Total + Num
 line 8 or Counter = Counter + Num
 Counter = Counter + 1
Question 26

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

IF...THEN...ELSE...ENDIF and CASE...OF...OTHERWISE...ENDCASE are two


different conditional statements that you can use when writing pseudocode. Explain, using
examples, why you would choose to use each conditional statement.

IF X > 0 AND X <= 10 THEN


PRINT 'In Range'
ELSE PRINT 'Out of Range'
ENDIF
E.g. checking a condition that may be complex
// uses relational operators // checking for a range of values
// only 2 options

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

Give an example of a conditional statement using pseudocode.


IF X < 0 THEN
PRINT "Negative"
ELSE
PRINT "Not negative"
ENDIF

OR
CASE X OF
1: PRINT ("ONE")
2: PRINT ("TWO")
OTHERWISE PRINT ("Less than ONE or more than TWO")
ENDCASE

Describe the purpose of a conditional statement


To allow different routes through a program dependent on meeting certain criteria
Question 30

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.

 Add Total ← 0 // Total ← Number


 Add Total ← Total + Number
 Add PRINT "Total is ", 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

Give the line number(s) from the algorithm of:


an assignment statement 1/2/3/4/8/9
a loop 5–11
a counting statement 9
a totalling statement 8
Give the line numbers of the four errors in this pseudocode. Suggest a correction for each error.
Line 1 TotalWeight ← 0

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

Line 12 OUTPUT "Total weight of the load is ", TotalWeight


Explain how you could extend the algorithm to calculate and display the average weight of a bag of
firewood in the load.
 After line 11
 Divide TotalWeight by
 … BagCount
 Assign a new variable AverageWeight ← TotalWeight / BagCount
 Output the result OUTPUT AverageWeight
 With a message "Average weight of a bag of firewood is "
Question 34

This flowchart inputs five numbers and performs a calculation.


The predefined function MOD finds the remainder from integer division for example

R ← 25 MOD 11 gives R a value of 3


Complete the trace table for this set of input data:
5, 4, 6, 2, 1, 9, 3, 2, 1, 6, 7, 6, 1, 5, 1, 0, 0, 0, 0, 0

Describe the purpose of this flowchart


 (Use first four digits input to) calculate a check digit
 Check if the check digit input is valid
Question 35

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

Give the line number(s) from the algorithm of:


an assignment statement 1/2/8/12
a loop 3 and/or 14
a counting statement 8/12
a selection statement 6/10/15/19
Give line numbers where the four errors are to be found in the pseudocode. Suggest a correction
for each error.
Line 02 TooCold ← 0
Line 08 TooCold ← TooCold + 1
Line 15 IF TooHot > 5
Line 17 OUTPUT "Alarm!!"
Explain how you could extend the algorithm to count the number of times the temperature readings
are within the range –18 degrees to –25 degrees inclusive.
 Add a new variable inRange …
 … set to zero at start of algorithm
 Add extra IF statement
 IF temperature >= -25 AND temperature <= -18
 Update inRange by 1 if true
Question 36

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

Explain the purpose of this algorithm.


 Checking if the remainder, when the larger number is divided by the smaller number,
is zero
 To see if the larger number is a multiple of the smaller number
 To see if the smaller number is a factor of the larger number
Question 37

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

Data type name


Integer
Data type description
(Any) whole number
Data: Cambridge2021
Data type name
String
Data type description
A group of characters/text
Data: 47.86
Data type name
Real
Data type description
(Any real) number that could be a whole number or a fraction
Question 38

The flowchart represents an algorithm.


The algorithm will terminate if –1 is entered.
Complete the trace table for the input data:
50, 75, 99, 28, 82, 150, –1, 672, 80

Describe the purpose of the algorithm.


 To output the type of test data
 … by performing a range check //… by checking if numbers are within the range 50
and 100 (inclusive) (or not).
Question 39

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

There are four errors in this code.


Locate these errors and suggest code correction to remove each error.
Error – Line 04 or IF Age >= 18 and Correction – IF Age >18
Error – Line 05 or Num18 =Num18 + Age and Correction – Num18 = Num18 + 1
Error – Line 08 or PRINT Num18 – Age and Correction – PRINT Num18
Error – INPUT Age missing inside loop and Correction – Include INPUT Age after test and
before exiting loop
Question 40

There is a program that stores the following data:


 EmployeeID, an employee ID which must be two letters followed by 4 numbers, e.g.
TY4587
 Manager, whether the employee is a manager or not
 AnnualHoliday, number of whole days’ annual holiday
 PayGrade, the employee’s pay grade which must be a single letter A–F
Complete the following table to identify:
 The most appropriate data type for each variable

Variable Data type


EmployeeID String
Manager Boolean
AnnualHoliday Integer
PayGrade Char
Question 41

Study the flowchart.

Complete the trace table for the input values 4, 3, −1:


Question 42

Explain the differences between a WHILE … DO … ENDWHILE and a REPEAT … UNTIL loop.

 WHILE has criteria check at start / pre-test


 May never run
 REPEAT UNTIL has criteria check at end / post-test
 Will always run at least once
Question 43

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

There are four errors in this code.


State the line number for each error and write the correct code for that line.

Line 2 Correct code Counter = 0


Line 7 Correct code Total = Total + Number // Number + Total
Line 8 Correct code Counter = Counter + 1 // 1 + Counter
Line 10 Correct code Average = Total / Counter // Average = Total / 50
Question 44

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

This flowchart inputs a range of temperatures in degrees Fahrenheit.


As each temperature is input, it is compared with the previous highest temperature. If it is higher
than the current highest, it replaces the previous highest temperature and then it is converted to
degrees Celsius. For ease of calculation, the final step of the Fahrenheit to Celsius conversion has
been approximated as division by 2. When –1 is entered, the input process stops and the highest
temperature (in both Fahrenheit and Celsius) is output

Complete the trace table for the input data:


68, 46, 50, 86, 65, 50, 40, 30, –1
Question 46

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

Find the error in the pseudocode and suggest a correction.

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

Write an algorithm using either pseudocode or a flowchart, to:


 input a positive integer
 use this value to set up how many other numbers are to be input
 input these numbers
 calculate and output the total and the average of these numbers.
INPUT NumberCount
Total ← 0
FOR Count ← 1 TO NumberCount
INPUT Number
Total ← Total + Number
NEXT
Average ← Total/NumberCount
PRINT Total, Average

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.

Complete the trace table for the input data:


50.4, 50.3, 49.1, 50.3, 50.0, 49.5, 50.2, 50.3, 50.5, 50.6
The size of the batch has increased to 50 sacks. It has been decided to only reject sacks that are
underweight. State the changes that need to be made to the flowchart.
 change to Is Count = 50?
 remove IS Sack > 50.5?
Question 52

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

Find the four errors in the pseudocode and suggest corrections.

NUMBERS should be Number


IF Number > 100 should be IF Number >= 100
INPUT Number is missing from inside the loop insert INPUT Number after the IF statement
The final PRINT Number is not needed remove it
Show, using pseudocode, how you would change the corrected algorithm to print out any numbers
between 100 and 200 inclusive
The test should be IF Number >= 100 AND Number <= 200
Question 53

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

A program checks if the weight of a baby is at least 2 kilograms.


Give, with reasons, two different values of test data that could be used for the baby’s weight.
Each reason must be different.

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

Complete the trace table for the input data:


5, 7, +, 6, 2, –, 4, 3, *, 7, 8, ?, 0, 0, /

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

Examine the following pseudocode:


INPUT A
INPUT B
INPUT C
INPUT D
INPUT E
INPUT F
INPUT G
INPUT H
INPUT I
INPUT J
INPUT K
INPUT L
T ← A + B + C + D + E + F + G + H + I + J + K + L
OUTPUT "The average equals ", T / 12

Describe what happens in this pseudocode.


 Inputs a series of values
 Finds the total
 Prints out the average
Describe how this pseudocode could be altered to allow any number of values to be input.
 Use of loop structure
 Allow input to define the limit of the loop / use sentinel value
 Keeping a count of the number of values
 It could use a totalling process to keep a running total
Re-write the given pseudocode to allow any number of values to be input.
Total ← 0
INPUT CounterLimit
FOR LoopCounter ← 1 To CounterLimit
INPUT Number
Total ← Total + Number
NEXT LoopCounter
OUTPUT ″The average equals ″, Total / CounterLimit
Question 62

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

allows the user to guess the number.


Number ← RandInt(100)
TotalTry ← 1
REPEAT
PRINT "Enter your guess now, it must be a whole number"
INPUT Guess
IF TotalTry > Number THEN
PRINT "Too large try again"
ENDIF
IF Guess > Number THEN
PRINT "Too small try again"
ENDIF
TotalTry ← Guess + 1
UNTIL Guess <> Number
TotalTry ← TotalTry - 1
PRINT "Number of guesses ", TotalTry

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

The following pseudocode algorithm uses nested IF statements


IF Response = 1 THEN
X ← X + Y
ELSE
IF Response = 2 THEN
X ← X – Y
ELSE
IF Response = 3 THEN
X ← X * Y
ELSE
IF Response = 4 THEN
X ← X / Y
ELSE
OUTPUT "No response"
ENDIF
ENDIF
ENDIF
ENDIF

Name the type of statement demonstrated by the use of IF … THEN … ELSE … ENDIF

Conditional / selection statement


Re-write the pseudocode algorithm using a CASE statement
CASE OF Response // CASE Response OF
1 : X ← X + Y
2 : X ← X - Y
3 : X ← X * Y
4 : X ← X / Y
OTHERWISE OUTPUT ″No response″
ENDCASE
Question 66

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

Complete Trace table 2 for the tread depth input data:


1.2, 1.9, 1.4, 1.8, 2.4
Question 70

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

Set 2: 20, 33, 67

Set 3: 79, 91, 70

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

An algorithm has been written in pseudocode to:


 input 25 positive whole numbers less than 100
 find and output the largest number
 find and output the average of all the numbers
01 A ← 0
O2 B ← 0
03 C ← 0
04 REPEAT
05 REPEAT
06 INPUT D
07 UNTIL D > 0 AND D < 100 AND D = INT(D)
08 IF D > B
09 THEN
10 B ← D
11 ENDIF
12 C ← C + D
13 A ← A + 1
14 UNTIL A >= 25
15 E ← C / A
16 OUTPUT "Largest number is ", B
17 OUTPUT "Average is ", E

Give the line number for the statements showing:


Totalling 12
Counting 13
Range check 07
Calculating the average 15
State an example for each type of test data needed to test the input of the number:
Normal test data example
27 (example many correct answers any whole number 1-99 inclusive)
Erroneous/abnormal test data example
106 (example many correct answers)
Extreme test data example
99/1
The algorithm needs to be changed to include finding and outputting the smallest number input.
Describe how you would change the algorithm.
 new variable for minimum…
 … set to first value/high value
 … at start of program / before line 4
 test input / D for less than minimum
 … replace value minimum if input less than
 … after line 7 and before line 14
 new output for minimum (with appropriate message)
 … at end of program // after line 14
Question 73

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

There is an error in this algorithm.


Identify the error and write the corrected pseudocode statement.
 Error Total ← Accept – Over – Under // line 26
 Correction Total ← Accept + Over + Under
Question 74

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,

" Range of numbers is ", Range

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

This algorithm checks passwords.


 Each password must be 8 or more characters in length; the predefined function Length
returns the number of characters.
 Each password is entered twice, and the two entries must match.
 Either Accept or Reject is output.
 An input of 999 stops the process
REPEAT
OUTPUT "Please enter password"
INPUT Password
IF Length(Password) >= 8 THEN
INPUT PasswordRepeat
IF Password <> PasswordRepeat THEN
OUTPUT "Reject"
ELSE
OUTPUT "Accept"
ENDIF
ELSE
OUTPUT "Reject"
ENDIF
UNTIL Password = 999

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

An algorithm has been written to:


 input the ages of 100 students
 count and output the number of students aged 7 and under 12
 count and output the number of students aged 12 and under 18
 count and output the number of students aged 18 and over.
Complete the pseudocode algorithm: [4]
01 Count7to12 ← 0
02 Count12to18 ← 0
03 CountOver18 ← 0
04 FOR Student ← 1 TO 100
05 OUTPUT "Please enter student's age in years "
06 INPUT Age
07 IF Age >= 7 AND Age < 12
08 THEN
09 Count7to12 ← Count7to12 + 1
10 ENDIF
11 IF Age >= 12 AND Age < 18
12 THEN
13 Count12to18 ← Count12to18 + 1
14 ENDIF
15 IF Age >= 18
16 THEN
17 CountOver18 ← CountOver18 + 1
18 ENDIF
19 NEXT Student
20 OUTPUT "There are ", Count7to12, " students aged 7 and under 12."
21 OUTPUT "There are ", Count12to18, " students aged 12 and under 18."
22 OUTPUT "There are ", CountOver18, " students aged 18 and over."

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

An input of -1 ends the routine


Complete the trace table for the given algorithm using this input data:
7, 6, 5, 4, –1, 12, 34

Describe the purpose of this algorithm


 to count the factors / the numbers that go into (other than 1 or itself) of a number
 to output the number of factors
Describe the problem that occurs if a whole number smaller than 4 and not equal to –1 is input.
 the value of D becomes zero
 division by zero error
 endless loop
Explain how to change the flowchart to prevent this problem occurring.
 after the decision box to test if the number is -1
 insert another decision box to test if the number is less than 4 / less than or equal to 3
 return to INPUT Number if true
Question 78

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

Line 9 IF Operator – should be CASE OF Operator

Line 15 OUTPUT "The answer is ", Value1 – should be Answer

The loop may be corrected using a number of alternative methods:


Method 1
Line 1 Continue ← 1 should be Continue ← 0

Line 22 UNTIL Continue = 0 should be ENDWHILE // Line 2 WHILE Continue = 0


should be REPEAT and Line 22 UNTIL Continue = 0 should be Until Continue = 1

Method 2
Line 2 WHILE Continue = 0 should be REPEAT

Line 20 Continue ← 1 should be Continue ← 0 // Line 1 Continue ← 1 should be


Continue ← 0 and Line

22 UNTIL Continue = 0 should be Until Continue = 1

Method 3
Line 2 WHILE Continue = 0 should be WHILE Continue = 1

Line 20 Continue ← 1 should be Continue ← 0 and Line 22 UNTIL Continue = 0


should be ENDWHILE
Corrected algorithm example 1
1 Continue ← 1
2 REPEAT
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 INPUT Value2
9 CASE OF 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 ", Answer
16 OUTPUT "Do you wish to enter more values (Yes or No)?"
17 INPUT MoreValues
18 IF MoreValues = "No"
19 THEN
20 Continue ← 0
21 ENDIF
22 UNTIL Continue = 0

Corrected algorithm example 2


1 Continue ← 0
2 WHILE Continue = 0 (DO)
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 INPUT Value2
9 CASE OF 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 ", Answer
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 ENDWHILE
The algorithm needs changing to allow only the numbers 1, 2, 3, or 4 to be entered for the input
variable Operator. Write the pseudocode to perform this task and state where in the algorithm it
would be located.
Pseudocode
WHILE Operator < 1 OR Operator > 4 (DO)
OUTPUT "Enter 1, 2, 3 or 4"
INPUT Operator
ENDWHILE

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

The flowchart represents an algorithm.


The algorithm will terminate if –1 is entered at the List input.
Complete the trace table for the algorithm using this input data:
2, 77, 2, 16, 1, 35, 2, –7, 5, 18, 1, 11, 1, 12, 2, 20, –1, 18
Question 81

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

Line 2 should be < 50

Line 3 should be RandUp(0,100) // RandUp(-1,100)

Line 4 should be Counter ← Counter + 1

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.

Rewrite the algorithm using a FOR … NEXT loop.


FOR Counter ← 0 TO 49 // FOR Counter ← 1 TO 50
NumRand[Counter] ← RandUp(0,100) / RandUp(-1,100)
NEXT // NEXT Counter
Question 82

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 pseudocode represents an algorithm.


The pre-defined function DIV gives the value of the result of integer division.
For example, Y = 9 DIV 4 gives the value Y = 2

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 flowchart shows an algorithm that should:


• allow 100 numbers to be entered into the variable Number
• total the numbers as they are entered
• output the total and average of the numbers after they have all been entered.
Complete this flowchart:
Question 88

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

Identify the three errors in the pseudocode and suggest corrections.


 Line 05 OUTPUT UsefulEnergyOut should be INPUT UsefulEnergyOut
 Line 06 IF TotalEnergyIn <> -1 AND UsefulEnergy <> -1 should be:
IF TotalEnergyIn <> -1 AND UsefulEnergyOut <> -1
 Line 11 UNTIL TotalEnergyIn <> -1 OR UsefulEnergyOut <> -1 should be:
 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.

Complete the trace table using this data:


5.50, 3.40, 6.25, 3.85, –11.00, 0
Identify the error in the algorithm and describe how to correct it.
Error
for example including negative numbers / not differentiation between negative and positive
values
Correction
For example – after the input box insert a decision box to reject negative numbers

You might also like