Problem solving, algorithms and programming – Edexcel IGCSE Computer Science (4CP0)
Question text
6 Algorithms are used to solve problems.
(a) Figure 5 shows an algorithm written in pseudocode.
01
SET total TO 0
02
SET counter TO 1
03
SEND 'Input a number' TO DISPLAY
04
RECEIVE number FROM (INTEGER) KEYBOARD
05
WHILE counter < number DO
06
SET total TO total + counter
07
SET counter TO counter + 1
08
END WHILE
09
SEND 'The sum is ' & total TO DISPLAY
Figure 5
(i) Give two benefits of writing algorithms in pseudocode.
(2)
1
2
(ii) Explain why (INTEGER) is required in line 04 of the algorithm.
(2)
When the number 5 is entered, the algorithm should display The sum is 15.
There is an error in the algorithm.
(iii) Complete the table to identify the line that contains the error and show the
corrected code.
(2)
Line
Correct code
(b) Trace tables are used when developing algorithms.
Describe the purpose of a trace table.
(2)
(c) Figure 6 shows an algorithm.
Start
Animal [0] = “Dog”
Animal [1] = “Cat”
Animal [2] = “Bee”
Animal [3] = “Emu”
flag = True
No
flag = True ?
Yes
Stop
flag = False
index = 0
No
Animal [index] >
Animal [index + 1] ?
Yes
temp = Animal [index]
Animal [index] = Animal [index + 1]
Animal [index + 1] = temp
flag = True
index = index + 1
No
index = 3 ?
Yes
Figure 6
(i) Complete the trace table for the algorithm in Figure 6.
You may not need to use all the rows.
(6)
Animal array
flag
index
temp
[0]
[1]
[2]
[3]
(ii) Give the purpose of the algorithm in Figure 6.
(1)
(iii) Explain why the variable temp is needed in the algorithm.
(2)
(Total for Question 6 = 17 marks)
Question text
6 Algorithms can be represented using flowcharts or pseudocode.
(a) An algorithm has been designed to perform these steps:
A user is asked to input a number n
•
If the number n is 0 the program stops
•
If the number n is positive a message to display ‘Positive’ is output
•
If the number n is negative a message to display ‘Negative’ is output
•
•
The program repeats until a value of 0 has been input.
Draw a flowchart to represent this algorithm.
(6)
(b) Figure 3 shows an algorithm written in pseudocode that performs a
mathematical process.
01
02
RECEIVE number FROM (INTEGER) KEYBOARD
03
04
SET flag TO FALSE
05
06
IF number = 1 THEN
07
SET flag TO TRUE
08
ELSE
09
SET testNum TO 1
10
REPEAT
11
SET testNum TO testNum + 1
12
IF (number MOD testNum) = 0 THEN
13
SET flag TO TRUE
14
END IF
15
UNTIL (testNum = number) OR (flag = TRUE)
16
END IF
17
18
IF NOT (flag = TRUE)
19
SEND number TO DISPLAY
20
END IF
21
Figure 3
Trace tables are used to show the outputs from algorithms.
(i) Complete the trace table for an input of 7
(2)
number
flag
testNum
DISPLAY
(ii) Complete the trace table for an input of 9
(2)
number
flag
testNum
DISPLAY
(iii) State the purpose of the algorithm.
(1)
(iv) Lines 10 to 15 of the pseudocode in Figure 3 use a post‑conditioned loop.
Explain one reason why it is better for the algorithm to use a post‑conditioned
loop rather than a count controlled loop.
(2)
(Total for Question 6 = 13 marks)
Question text
6 Pseudocode and flowcharts are used to create algorithms.
(a) 6. The
Pseudocode
number of and
flowcharts
a train are
used or
to late
create
are algorithms.
minutes
is early
recorded each day for a week.
• (a)
A The
value number
of 0 is of
recorded
minutes if a the
train
is is
on time.
train
early
or late are recorded each day for a week.
•
A negative
value
of 0 is is recorded
recorded if if the
the train
train is is early.
• A value
on time.
• A negative
value
is recorded
if the is train
late. is early.
•
A positive
value is
recorded
if the train
•
A positive value is recorded if the train is late.
The algorithm in Figure 2 has been created to calculate and output the total
The algorithm in Figure 1 has been created to calculate and output the total
number
of trains that were early, on time or late.
number of trains that were early, on time or late.
01
02
SET arrival TO [-2, 1, 5, 0, -3, 4, 1]
03
04
SET early TO 0
05
SET late TO 0
06
SET index TO 0
07
08
WHILE NOT (index > LENGTH(arrival)) DO
09
10
IF arrival[index] >= 0 THEN
11
SET late TO arrival[index]
12
ELSE
13
IF arrival[index] < 0 THEN
14
SET early TO early + 1
15
END IF
16
END IF
17
18
SET index TO index + 1
19
20
END WHILE
21
22
SEND "Trains early:
" & early TO DISPLAY
23
SEND "Trains on time: " & (late + early) TO DISPLAY
24
SEND "Trains late:
" & late TO DISPLAY
25
Figure 1
Figure
2
Identify the type of iteration construct used in Figure 2.
(1)
Identify the type of iteration construct used in Figure 1.
A Post‑conditioned loop
(1)
B Count
A controlled
loop
Post-conditioned
loop
Count controlled loop
B
C Array loop
Array loop
C
Pre-conditioned
loop
D
D Pre‑conditioned
loop
(b) There are four errors in the pseudocode in Figure 2.
Write the correct code for lines 08, 10, 11 and 23
(i) Line 08
(1)
(ii) Line 10
(1)
(iii) Line 11
(1)
(iv) Line 23
(1)
(c) Give one reason for the variable index being incremented in line 18.
(1)
(d) In the game FizzBuzz a user enters a number.
These conditions apply:
•
if the number is divisible by 3 the program displays Fizz
•
if the number is divisible by 5 the program displays Buzz
•
if the number is divisible by both 3 and 5 the program displays FizzBuzz
•
if the number is not divisible by either 3 or 5, the program displays the
number entered.
Hint: If one number is divisible by another number there is no remainder.
These are the components needed for the flowchart.
Start
Stop
Get
number
Is number
Is number
Is number
divisible by 3?
divisible by 5?
divisible by 5?
Display
Display
number
Fizz
Display
Display
Buzz
FizzBuzz
Draw the flowchart for the algorithm used in the game in the box on the
next page.
Use each component once.
Do not add any additional components.
Use as many arrows and yes/no labels as you need.
(6)
Draw your flowchart here.
(Total for Question 6 = 12 marks)
Question text
6 Programmers share algorithms with different people and write algorithms for
different reasons.
(a) A programmer is showing a new algorithm to a group of non-technical managers.
State an appropriate method for writing the algorithm.
Justify your answer.
(2)
Method
Justification
(b) Figure 1 shows an algorithm that displays a string based on the number input by
the user.
SEND ("Enter a number: ") TO DISPLAY
1
RECEIVE inNum FROM (INTEGER) KEYBOARD
2
IF ((inNum = 1) OR (inNum = 2)) THEN
3
IF (inNum = 1) THEN
4
SEND ("First") TO DISPLAY
5
ELSE
6
IF (inNum = 2) THEN
7
SEND ("Second") TO DISPLAY
8
END IF
9
END IF
10
ELSE
11
SEND ("Invalid input") TO DISPLAY
12
END IF
13
Figure 1
Give one reason why the selection statement on line 7 is not required.
(1)
(c) Figure 2 shows an algorithm that manipulates arrays.
The algorithm works with any number of scores.
SET oldScores TO [ 10, 20, 30, 40, 50]
1
SET newScores TO [ 0, 0, 0, 0, 0]
2
SET newIndex TO 0
3
4
FOR oldIndex FROM (LENGTH (oldScores) - 1) TO 0 STEP -1 DO
5
SET newScores[newIndex] TO oldScores[oldIndex]
6
SET newIndex TO newIndex + 1
7
END FOR
8
Figure 2
(i) Describe what happens to the variable oldIndex when line 5 is executed.
(2)
(ii) State the purpose of the algorithm in Figure 2.
(1)
(d) Cloud storage has become an increasingly popular service.
Discuss the benefits and drawbacks of using cloud storage.
(6)
(Total for Question 6 = 12 marks)
Question text
4 Reba likes writing programs.
(a) She is writing a guessing game.
She needs a flowchart to show the logic of the game.
(i) These are the components needed to draw the flowchart.
Display "No match.
Get guess
Guess again"
Display "Hooray
number = 10
they match"
guess = number?
Stop
Start
Draw the flowchart for the algorithm in the box on the next page.
Use each component once.
Do not add any additional components.
Use as many arrows and yes/no labels as you need.
(5)
Draw your flowchart here.
(ii) Identify an alternative method for writing the algorithm.
(1)
A Simulation
B Cipher
C Program code
D Truth table
(b) Reba wants to develop a program that will convert a temperature in Fahrenheit
to Celsius.
Here are four steps in the algorithm.
The steps are not in the correct order.
Step
A
Change the temperature to Celsius
B
Get the temperature in Fahrenheit
C
Show the temperature in Celsius
D
Set the temperature to 0
(i) Give the letter of the step that initialises a variable.
(1)
(ii) Give the letter of the step that inputs a value.
(1)
(c) Figure 2 shows the pseudocode for an early version of an algorithm that Reba
has written for another game.
The algorithm:
•
asks the user to input a colour or input –1 to end the game
•
awards 1 point for red
•
awards 8 points for orange
•
generates the score for the game
•
displays the results of the game.
1 SET Colour TO ""
2 SET Score TO 0
3 SET RedPoints TO 0
4 SET OrangePoints TO 0
5 SET NumOranges TO 0
6
7 WHILE Colour <> "-1" DO
8 RECEIVE Colour FROM (STRING) KEYBOARD
9 IF Colour = "red" THEN
10 SET RedPoints TO RedPoints + 1
11 ELSE
12 IF Colour = "orange" THEN
13 SET OrangePoints TO OrangePoints + 8
14 SET NumOranges TO NumOranges + 1
15 END IF
16 END IF
17 END WHILE
18
19 SET Score TO RedPoints + OrangePoints
20
21 SEND ("Score: "& Score) TO DISPLAY
22 SEND ("Number of reds: "& RedPoints) TO DISPLAY
23 SEND ("Number of oranges: "& OrangePoints) TO DISPLAY
Figure 2
Reba inputs: red, orange, red, red, orange, –1
The outputs are not as she expects.
(i) Complete the trace table to show the outputs.
(4)
Colour
Score
RedPoints
OrangePoints
NumOranges
Outputs
0
0
0
0
red
orange
red
red
orange
–1
(ii) Give the line number of the pseudocode that contains the error.
(1)
(iii) Write a replacement line of pseudocode to correct the error.
(1)
(Total for Question 4 = 14 marks)
Question text
4 Algorithms can be used to perform calculations and to process data.
(a) State what is meant by the term algorithm.
(1)
(b) Figure 3 shows an algorithm.
Start
Number1 = 1
Number2 = Number1
Output
Number1 Number2
Count = 0
Yes
Stop
Count = 2?
No
Number3 = Number2 + Number1
Output
Number3
Number1 = Number2
Number2 = Number3
Count = Count + 1
Figure 3
(i) Complete this trace table for the algorithm.
You may not need to use all the rows.
(5)
Number1
Number2
Number3
Count
Output
Count = 2?
1
1
–
0
1 1
False
(ii) The benefits of using a trace table include that they allow variable states,
outputs and decisions to be recorded.
Give one other benefit of using a trace table to test an algorithm.
(1)
(c) An algorithm is needed to count and display the number of vowels in a word.
The vowels are a, e, i, o, and u.
The completed algorithm must:
•
count the number of vowels
create a message as a single string (e.g. there are number vowels)
•
•
print the message to the display.
Here is a partially completed algorithm written in pseudocode.
Complete the algorithm in the spaces provided.
(5)
SET word TO 'elephant'
FOR EACH letter FROM word DO
END FOREACH
(Total for Question 4 = 12 marks)
Question text
5 Isaac is a program developer.
(a) Figure 3 shows an algorithm Isaac has written.
Start
Numbers[0] = 10
Numbers[1] = 6
Numbers[2] = 1
Numbers[3] = 9
Numbers[4] = 3
Count = 0
Length = 4
Midpoint = 2
Temp = 0
No
Count < Midpoint?
Stop
Yes
Temp = Numbers[Count]
Numbers[Count] = Numbers[Length − Count]
Numbers[Length − Count] = Temp
Count = Count + 1
Figure 3
(i) Complete the trace table. You may not need to use all of the rows.
(5)
Numbers array
Count
Length
Midpoint
Temp
[0]
[1]
[2]
[3]
[4]
0
4
2
0
10
6
1
9
3
(ii) Give the purpose of the algorithm.
(1)
(iii) Explain why the variable Temp is needed.
(2)
(b) Figure 4 shows an algorithm Isaac has written using pseudocode.
The algorithm should display the average of the numbers that have been input.
1
SET total TO 0
2
SET number TO 0
3
SET count TO 0
4
WHILE number <> −1 DO
5 SEND 'Input a number or −1 to end the program' TO DISPLAY
6 RECEIVE number FROM (INTEGER) KEYBOARD
7 SET total TO total + number
8 SET count TO count + 1
9
END WHILE
10 SET average TO total / count
11 SEND 'The average is ' & average TO DISPLAY
Figure 4
Isaac uses the input 2, 3, 5, 2, −1 to test the algorithm. He discovers an error.
Expected result
Actual result
The average is 2.75
The average is 2.2
(i) Explain why the Actual result is not the same as the Expected result.
(2)
(ii) Give the number of the line that contains the error.
(1)
(iii) Amend a single line of pseudocode to correct the error.
(1)
(c) Isaac is going to sell one of his programs and distribute it on a DVD.
Give two advantages of choosing a programming language that uses a compiler,
rather than an interpreter, for a program that will be distributed on a DVD.
(2)
1
2
(Total for Question 5 = 14 marks)
Question text
6 A software engineer is working with some algorithms.
(a) An algorithm needs to be developed.
The algorithm must:
•
check for a change of hour every minute
•
use the library subprogram getHour() to get the hour part of the current time
in the 24-hour clock (0 to 23)
•
output “Good morning” when the hour is between 3 and 12, inclusive
•
output “Good afternoon” when the hour is between 13 and 19, inclusive
•
output “Good night” at all other times.
Complete the flowchart to represent this algorithm, in the space provided on the
next page.
The call to the library subprogram getHour() has already been included.
Use this subprogram symbol to show waiting for 1 minute.
wait
1 minute
(6)
Draw your flowchart here.
START
getHour()
(b) Another algorithm determines when to change the flavoured syrups for a drinks
dispensing machine.
Figure 4 shows this algorithm written in pseudocode.
11 SET flavours TO [“anise”, “mango”, “cola”, “apple”, “papaya”, “strawberry”,
“lychee”, “banana”]
12
13 SET volume TO [0.7, 0.2, 0.6, 0.1, 0.05, 0.8, 0.4, 0.6]
14
15 SET i TO 0
16
17 WHILE i < 8 DO
18
19 IF (volume[i] < 0.1) THEN
10 SEND (flavours[i] & “ needs changing”) TO DISPLAY
11
12 ELSE
13 IF (volume[i] >= 0.3) AND (volume[i] <= 0.5) THEN
14 SEND (flavours[i] & “ needs ordering”) TO DISPLAY
15 END IF
16 END IF
17
18 SET i TO i + 1
19
20 END WHILE
Figure 4
(i) Give the output produced by the algorithm.
(2)
The algorithm works with a fixed number of flavours.
(ii) Give the number of the line in the algorithm that would need to be amended
to allow for any number of flavours.
(1)
(iii) State how the pseudocode needs to be changed to make this amendment.
(1)
QUESTION 6(c) BEGINS ON THE NEXT PAGE.
(c) The algorithms will be coded in a high-level language.
Programs written in high-level languages must be translated in order to be
executed by a computer.
Discuss the similarities and differences between a compiler and an interpreter.
(6)
(Total for Question 6 = 16 marks)
Question text
5 Ships carry cargo around the world in containers.
(a) Containers come in two sizes.
Figure 2 shows an algorithm written using flowchart symbols.
Start
volume1 = 24.00
Yes
reqVolume
<= volume1
volume2 = 48.00
No
print “Requires
minVolume = volume1 / 2
small container”
input width
No
reqVolume
<= volume2
input height
Yes
input length
print “Requires
large container”
reqVolume = width * length * height
print “Too large for
cargo container”
No
reqVolume <
minVolume
Yes
print “Too small for
cargo container”
Stop
Figure 2
(i) Complete the table to show the output for each cargo item.
(3)
Cargo item
Output
width
length
height
4
4
2
2
2
2
3
8
5
(ii) State the purpose of the algorithm in Figure 2.
(1)
(b) Each ship is registered to a state.
Figure 3 shows a partially completed algorithm written in pseudocode.
The completed algorithm must:
•
print each state to the display on a new line
•
count the number of states
•
create a message as a single string (e.g. there are number states)
•
print the message to the display.
Complete the algorithm in the space provided in Figure 3.
(4)
SET numStates TO 0
SET states TO [“France”, “Singapore”, “Malta”, “Panama”, “Greece”, “Italy”]
FOR EACH state FROM states DO
END FOREACH
Figure 3
(c) Cargo ships have maximum weight loads.
Figure 4 shows an algorithm written using pseudocode.
The algorithm should identify the size of cargo ship required for any load.
There is an error on line 9.
2
SET loadWeight TO [20000, 28000, 40000, 50000]
3
SET index TO 0
4
SET found TO FALSE
5
6
SEND “Enter cargo weight” TO DISPLAY
7
RECEIVE target FROM (INTEGER) KEYBOARD
8
9
WHILE (NOT found) DO
10
IF (loadWeight [index] >= target) THEN
11
SEND loadWeight [index] TO DISPLAY
12
SET found TO TRUE
13
ELSE
14
SET index TO index + 1
15
END IF
16
END WHILE
17
18
IF (NOT found) THEN
19
SEND “No ship available” TO DISPLAY
20
END IF
Figure 4
(i) Trace tables are used to identify errors in algorithms.
Complete the trace table for an input of 50500 to show what happens due to
the error on line 9 in the pseudocode in Figure 4.
You may not need to fill in all the rows in the table.
(2)
target
found
index
loadWeight[index]
50500
FALSE
0
(ii) Construct a single line of pseudocode to correct line 9.
(2)
(Total for Question 5 = 12 marks)