Algorithms Topic (Handout)
Algorithms Topic (Handout)
Assignment FOR X 1 TO 10
Iteration READ X
Input PRINT X
Output X Y + Z
[3]
(b) A programmer writes a program to store a patient’s temperature every hour for a day.
State the data structure that would be most suitable to use and give the reason for your choice.
Reason ..............................................................................................................................................
......................................................................................................................................................[2]
(c) Identify two different selection statements that you can use when writing pseudocode.
1 ........................................................................................................................................................
..........................................................................................................................................................
2 ........................................................................................................................................................
......................................................................................................................................................[2]
1(a) 1 mark for each correct line, maximum 3 (zero correct 0, one correct 1, two correct 2, three or
four correct 3), each box must have only one connection.
Assignment FOR X ← 1 TO 10
Iteration READ X
Input PRINT X
Output X ← Y + Z
[3]
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]
1 (i) 1 mark for each change
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
[3]
1. 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
1 .................................................................................................................................................
...................................................................................................................................................
2 .................................................................................................................................................
...................................................................................................................................................
3 .................................................................................................................................................
...............................................................................................................................................[3]
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[3]
1 (i) 1 mark for each improvement
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 1 mark for each error identified + suggested correction
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 [4]
..........................................................................................................................................................
..........................................................................................................................................................
..........................................................................................................................................................
......................................................................................................................................................[2]
6 Identify three different loop structures that you can use when writing pseudocode.
1 .......................................................................................................................................................
..........................................................................................................................................................
2 .......................................................................................................................................................
..........................................................................................................................................................
3 .......................................................................................................................................................
......................................................................................................................................................[3]
Number of Price
Garden Garage
Bathrooms in $
Bungalow B17 7 4 Yes Yes 750,000
Apartment A09 2 1 No No 100,000
House H10 4 2 Yes No 450,000
House H13 3 2 Yes No 399,000
Apartment A01 2 2 No Yes 95,000
Apartment A16 1 1 No No 150,000
House H23 3 1 No Yes 250,000
House H46 2 1 Yes Yes 175,000
Page 6 Mark Scheme Syllabus Paper
Cambridge IGCSE – May/June 2015 0478 21
Integer 'a'
Real 2
Char 2.0
String True
Boolean 'Twelve'
[4]
6 – FOR (… TO … NEXT)
– REPEAT (… UNTIL)
– WHILE (… DO … ENDWHILE) [3]
7 (a) – 7 [1]
(b) – Brochure No
– Uniquely identifies each property [2]
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]
[Turn over
2 1 mark for each error identified + suggested correction
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 [4]
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]
3. 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.
START
Posn @ 1
New @ 0
INPUT X
T1 @ X DIV 2
T2 @ X MOD 2
New @New+T2 * Posn
Posn @Posn * 10
X @T1
Yes is T1
>= 2?
No
OUTPUT New
END
Complete a trace table for each of the two input values 5 and 12.
[6]
...............................................................................................................................................[1]
[Turn over
2 One mark for each error identified + suggested correction
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
[4]
3 (a)
Number 1 Trace table
X Posn New T1 T2 Output
5 1 0
10 1 2 1
2 100 1 1 0
101
101
12 1 0
10 0 6 0
6 100 0 3 0
3 1000 100 1 1
1100
1100
67$57
5LGHUV
5HMHFW
,1387
+HLJKW
,V <HV
+HLJKW 5HMHFW5HMHFW
"
1R
5LGHUV5LGHUV
(1'
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
[4]
[Turn over
5 REPEAT ... UNTIL is one type of loop structure.
Identify and describe two other types of loop structure that you could use when writing
pseudocode.
Description .................................................................................................................................
...................................................................................................................................................
Description .................................................................................................................................
...............................................................................................................................................[4]
4
Riders Reject Height Output
0 0
1 1.4
2 1.3
1 1.1
3 1.3
2 1.0
4 1.5
5 1.2
6 1.3
7 1.4
8 1.3
Ready to go 2
(1 mark) (1 mark) (1 mark) (1 mark)
[4]
5 – FOR (… TO … NEXT)…
– … a set number of iterations
– WHILE (… DO … ENDWHILE) …
– … used where the loop may never be executed/whilst a specified condition exists
[4]
6 The flowchart on the opposite page shows what happens when the barcode on a product is
scanned at the checkout in a supermarket. The barcodes are used in an automatic stock control
system.
Using item number only from the list below, complete the flowchart.
Item
Statement
number
4 Has the re-order flag already been added to the product record?
Barcode on product
is scanned END
No
No
Yes
Yes
No Any more
barcodes
to scan? No
Yes
Yes
END
No
[4]
[Turn over
6
[4]
START
barcode on product is
scanned END
No
Yes
barcode is looked up
in stock database
2
1 mark
No
3 7
Yes
1 mark
6
Yes
No any more
5 barcodes
No
to scan?
1 mark Yes
Yes
4 END
No
1/8
1 mark
8/1
[4]
7. Regular customers at a supermarket use a rewards card at the point-of-sale.
Points are calculated from every transaction and added to the points total stored on the card.
When the points total exceeds 500, the customer can either:
• pay the full amount due and increase their points total
• get $1 deducted from the amount due in exchange for 500 reward points
The new points total and amount to be paid is printed on the receipt.
A user-defined function CalculatePoints has already been coded to calculate the new points
earned from the amount spent.
INPUT AmountDue
NewPoints ←
CalculatePoints(AmountDue)
PointsTotal ←
PointsTotal + NewPoints
[6]
[Turn over
7
INPUT
Amount(Due)
A. CALCULATE
CALL points total
CalculatePoints(AmountDue)
In general – accept
descriptive assignment
PointsTotal = statements
PointsTotal + NewPoints
OUTPUT
“Exchange points?”
INPUT
Response
Response = “YES”?
Allow yes/Yes/missing
YES quotes
PointsTotal =
PointsTotal - 500
AmountDue = AmountDue - 1
OUTPUT
AmountsDue, PointsTotal
STOP
[Max 6]
8 The standard pack of playing cards has four suits – called Clubs, Diamonds, Hearts and Spades.
Each card has a value shown by its number or a name: 1 (Ace), 2, 3, … 10, 11 (Jack), 12 (Queen),
13 (King). The pack of cards has one combination for each suit and value.
A program is to be written which simulates a magician dealing all 52 cards from the card pack.
(a) Explain why the generation of 52 (4 suits x 13 card values) pairs of random numbers will not
simulate the dealing of the complete pack.
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[2]
Card value
Suit
number 1 2 3 4 5 6 7 8 9 10 11 12 13
1 (Clubs) F F F F F F F F F F T F F
2 (Diamonds) F F F F F F F F F F F F F
3 (Hearts) F F T F F F F F F F F F F
4 (Spades) F F F F F F F F F F F F F
The table shows two cards have been dealt so far; the 3 of Hearts and the Jack of Clubs.
The program will output the suit and the card value in the order in which the cards are dealt.
The program design in pseudocode is produced as follows:
.......................................................................................................................................[1]
.......................................................................................................................................[1]
(iii) The initialisation of a variable used as a counter, but not to control a ‘count controlled’
loop.
.......................................................................................................................................[1]
.......................................................................................................................................[1]
...............................................................................................................................................[1]
...............................................................................................................................................[1]
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[2]
[Turn over
(g) Line 18 in the design shows which new card is dealt each time.
When an Ace, Jack, Queen or King is dealt, the output displays the number for that card, not
the name of the card.
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...............................................................................................................................................[4]
8 (a) The combination of suit and card number // the ‘pair ‘ of numbers // the pair of random
numbers …. [1]
There will be duplicates/repeats//not all cards will be drawn [1]
(ii) 27 // 28 [1]
(iii) 08 [1]
(iv) 12 // 13 [1]
(c) 1 [1]
(e) Test has the card has already been drawn? [1]
Set value TRUE for this card entry (in the array)/this card [1]
Flags that this is the first time this card has been drawn // decides if another card
must be generated [1]
Outputs the new card value [1]
[Max 2]
(g) Pseudocode …
(SELECT) CASE (OF) CardValue + ENDCASE [1]
(CASE) 1: CardName ← "Ace" 1 mark for any one correct [1]
(CASE) 11: CardName ← "Jack"
(CASE) 12: CardName ← "Queen"
(CASE) 13: CardName ← "King" (final three cases …) [1]
OTHERWISE (/ELSE) CardName ← CardValue //
(CASE) 2 TO 10: CardName ← CardValue) [1]
ENDCASE // ENDSELECT
The integers are stored in an array, Num. The first N elements are to be processed.
FOR i ← 1 TO (N - 1)
j ←1
REPEAT
IF Num[j] > Num[j + 1]
THEN
Temp ←
Num[j]
Num[j] ←
Num[j + 1]
Num[j + 1] Temp←
ENDIF
j ←
j + 1
UNTIL j = (N – i + 1)
ENDFOR
(a) (i) Trace the execution of the pseudocode for the value N = 5 and the given array of integers.
Num
N i j Temp 1 2 3 4 5
5 11 16 13 7 8
[8]
[Turn over
(ii) State the purpose of the algorithm.
...........................................................................................................................................
.......................................................................................................................................[1]
(iii) Describe what evidence from the trace table suggests that the given pseudocode is
inefficient.
...........................................................................................................................................
.......................................................................................................................................[1]
(b) Complete the identifier table documenting the use of each of the variables.
Temp
[5]
9 (a) (i)
N i j Temp 1 2 3 4 5
11 16 13 7 8
5
1 1
2 13 16
3 7 16
4 8 16
Terminates at 5 and
4 respectively 5
2 1
2 7 13
3 8 13
3 1 7 11
2 8 11
2 7 8 11 13 16
[8]
(ii) To sort / to order/put in ascending order the items (in the array) [1]
Num
Mark as follows:
INTEGER × 4 [1]
One mark per description [4]