CH-9 Problem Solving
CH-9 Problem Solving
1 Total = 100.00
2 PRINT ′Enter the height of each member of your class, one at a
time, when prompted′
3 FOR Count = 1 TO 30
4 PRINT ′Enter a height in metres′
5 INPUT Height
6 Total = Total + Height
7 PRINT Total / 30
8 Count = Count + 1
9 NEXT Count
State the line numbers that contain the errors and describe how to correct each error.
Error 1 .......................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
Error 2 .......................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
Error 3 .......................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
[3]
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[1]
3 Four programming concepts and four descriptions are shown.
Draw a line to connect each programming concept to the most appropriate description.
An overview of a program or
Function
subroutine. [3]
4 A programmer has written a routine to store the name, email address and password of a contributor
to a website’s discussion group.
(a) The programmer has chosen to verify the name, email address and password.
Explain why verification was chosen and describe how the programmer would verify this data.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[4]
(b) The programmer has also decided to validate the email address and the password.
Describe validation checks that could be used.
...................................................................................................................................................
...................................................................................................................................................
Password ..................................................................................................................................
...................................................................................................................................................
................................................................................................................................................[2]
5 A program checks that the weight of a basket of fruit is over 1.00 kilograms and under
1.10 kilograms. Weights are recorded to an accuracy of two decimal places and any weight not in
this form has already been rejected.
Give three weights as test data and for each weight state a reason for choosing it. All your reasons
must be different.
Weight 1 ............................................................................................................................................
Reason ..............................................................................................................................................
..........................................................................................................................................................
Weight 2 ............................................................................................................................................
Reason ..............................................................................................................................................
..........................................................................................................................................................
Weight 3 ............................................................................................................................................
Reason ..............................................................................................................................................
..........................................................................................................................................................
[3]
4 A programmer has written a routine to check that prices are below $10.00. These values are used
as test data.
..........................................................................................................................................................
..........................................................................................................................................................
9.99 ..................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
ten ....................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
[3]
2 This section of program code asks for 80 numbers between 100 and 1000 to be entered. It checks
that the numbers are in the correct range, and stores them in an array. It counts how many of the
numbers are larger than 500 and then outputs the result when the program is finished.
1 Count = 0
2 FOR Index = 1 TO 80
3 INPUT 'Enter a number between 100 and 1000', Number
4 WHILE Number = 99 AND Number = 1001
5 INPUT 'This is incorrect, please try again', Number
6 ENDWHILE
7 Num[80] = Number
8 IF Number > 500 THEN Count = Count + 1
9 UNTIL Index = 80
10 PRINT Index
11 PRINT ' numbers were larger than 500'
There are four lines of code that contain errors.
State the line number for each error and write the correct code for that line.
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[2]
(b) Describe, using an example, how data could be verified on data entry.
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[2]
(c) Explain what is meant by the term library routine.
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[2]
4 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 ...........................................................................................................................................
Correction .................................................................................................................................
...................................................................................................................................................
[2]
(b) Rewrite the correct algorithm using a more suitable loop structure.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[3]
2 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
Input destination
Map List
[2]
4 For each of the four statements in the table, place a tick in the correct column to show whether it
is an example of validation or verification.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[2]
(b) Identify, using pseudocode, another loop structure that the algorithm in part (a) could have
used.
...................................................................................................................................................
...............................................................................................................................................[1]
(c) 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.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[3]
2 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
Error 1 ........................................................................................................................................
Correction ..................................................................................................................................
Error 2 ........................................................................................................................................
Correction ..................................................................................................................................
Error 3 ........................................................................................................................................
Correction ..................................................................................................................................
Error 4 ........................................................................................................................................
Correction ..................................................................................................................................
...............................................................................................................................................[4]
(b) Decide, with reasons, whether the numbers 10 and 20 are within or outside the range.
Within Outside
Number Reason
range (✓) range (✓)
10
……………………………………………………………………..
……………………………………………………………………..
20
……………………………………………………………………..
……………………………………………………………………..
[4]
4 Four validation checks and four descriptions are shown below.
Draw a line to link each validation check to the correct description.
Data is of a particular
Range check
specified type
Error 1 ..............................................................................................................................................
Correction .........................................................................................................................................
Error 2 ..............................................................................................................................................
Correction .........................................................................................................................................
Error 3 ..............................................................................................................................................
Correction .........................................................................................................................................
Error 4 ..............................................................................................................................................
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
1 .................................................................................................................................................
...................................................................................................................................................
2 .................................................................................................................................................
...................................................................................................................................................
3 .................................................................................................................................................
...............................................................................................................................................[3]
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[3]
2 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
(i) Identify three changes you would need to make to find the largest number input instead
of the smallest number.
1 .........................................................................................................................................
...........................................................................................................................................
2 .........................................................................................................................................
...........................................................................................................................................
3 .........................................................................................................................................
.......................................................................................................................................[3]
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
...........................................................................................................................................
.......................................................................................................................................[3]
2 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
7 Average = Total/PosCount
8 NEXT Counter
9 PRINT Num
Locate these errors and suggest code corrections to remove each error.
1 .......................................................................................................................................................
..........................................................................................................................................................
2 .......................................................................................................................................................
..........................................................................................................................................................
3 .......................................................................................................................................................
..........................................................................................................................................................
4 .......................................................................................................................................................
......................................................................................................................................................[4]
4 A routine checks the age and height of children who are allowed to enter a play area. The children
must be less than 5 years of age and under 1 metre in height.
(a) The first set of test data used is age 3 and height 0.82 metres.
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[2]
(b) Provide two additional sets of test data. For each, give
Each type of test data and reason for use must be different.
Set 1 .........................................................................................................................................
Type ..........................................................................................................................................
Reason .....................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
Set 2 .........................................................................................................................................
Type ..........................................................................................................................................
Reason .....................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[6]
2 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
Locate these errors and suggest code corrections to remove each error.
1 .......................................................................................................................................................
..........................................................................................................................................................
2 .......................................................................................................................................................
..........................................................................................................................................................
3 .......................................................................................................................................................
..........................................................................................................................................................
4 .......................................................................................................................................................
......................................................................................................................................................[4]
4 A routine checks the weight of melons to be sold in a supermarket. Melons weighing under
0.5 kilograms are rejected and melons weighing over 2 kilograms are also rejected.
Normal .............................................................................................................................................
Extreme ............................................................................................................................................
Abnormal ......................................................................................................................................[3]
2 Read this section of program code that should input 10 positive numbers and then output the
smallest number input.
1 Small = 0
2 Counter = 0
3 REPEAT
4 INPUT Num
6 Counter = Counter + 1
7 PRINT Small
Locate these errors and suggest a corrected piece of code for each error.
1 .......................................................................................................................................................
..........................................................................................................................................................
2 .......................................................................................................................................................
..........................................................................................................................................................
3 .......................................................................................................................................................
..........................................................................................................................................................
4 .......................................................................................................................................................
......................................................................................................................................................[4]
2 Read this section of program code that should input 30 positive numbers and then output the
largest number input.
1 Large = 9999
2 Counter = 0
4 DO
5 INPUT Num
7 Counter = Counter - 1
8 ENDWHILE
9 PRINT Large
Locate these errors and suggest a corrected piece of code for each error.
1 .......................................................................................................................................................
..........................................................................................................................................................
2 .......................................................................................................................................................
..........................................................................................................................................................
3 .......................................................................................................................................................
..........................................................................................................................................................
4 .......................................................................................................................................................
......................................................................................................................................................[4]
4 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
[3]