0% found this document useful (0 votes)
4 views36 pages

Algorithms Topic (Handout)

The document provides a series of programming exercises related to pseudocode and flowcharting, including identifying errors in code, suggesting improvements, and explaining programming concepts such as variables, constants, and loop structures. It also includes tasks for connecting statement types to examples, selecting appropriate data structures, and completing trace tables for given inputs. Overall, the document serves as a comprehensive assessment of programming knowledge and skills.

Uploaded by

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

Algorithms Topic (Handout)

The document provides a series of programming exercises related to pseudocode and flowcharting, including identifying errors in code, suggesting improvements, and explaining programming concepts such as variables, constants, and loop structures. It also includes tasks for connecting statement types to examples, selecting appropriate data structures, and completing trace tables for given inputs. Overall, the document serves as a comprehensive assessment of programming knowledge and skills.

Uploaded by

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

1(a) Four statement types and four examples are shown below.

Draw a line to connect each statement type to the correct example.

Statement type Example

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.

Data structure ...................................................................................................................................

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.

Statement type example

Assignment FOR X ← 1 TO 10

Iteration READ X

Input PRINT X

Output X ← Y + Z

[3]

(b) – data structure (one—dimensional) array ……….


– ……… reason to simplify programming/ make programs shorter, etc. [2]

(c) – IF (… THEN … ELSE … ENDIF)

– CASE (… OF … OTHERWISE … ENDCASE) [2]


1. 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]

(ii) Rewrite the program code with your changes.

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

...........................................................................................................................................

.......................................................................................................................................[3]
1 (i) 1 mark for each change

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 > [3]

(ii) 3 marks maximum, 1 mark for each change correctly included.

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

This code works, but it is inefficient.

(i) Suggest three improvements that could be made.

1 .................................................................................................................................................

...................................................................................................................................................

2 .................................................................................................................................................

...................................................................................................................................................

3 .................................................................................................................................................

...............................................................................................................................................[3]

(ii) Rewrite the program code with your improvements.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................[3]
1 (i) 1 mark for each improvement

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 [3]

(ii) 3 marks maximum, 1 mark for each improvement correctly included.

Sample answer below


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 [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

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.

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]

© Cambridge International Examinations 2015


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

..........................................................................................................................................................

..........................................................................................................................................................

..........................................................................................................................................................

......................................................................................................................................................[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

4 1 mark for each correct link, up to maximum of 4 marks

Integer 'a'

Real 2

Char 2.0

String True

Boolean 'Twelve'

[4]

5 Any two points from


– 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 [2]

6 – FOR (… TO … NEXT)
– REPEAT (… UNTIL)
– WHILE (… DO … ENDWHILE) [3]

7 (a) – 7 [1]

(b) – Brochure No
– Uniquely identifies each property [2]

(c) Garage – Boolean


Number of Bedrooms – Number/Integer/Single
Price in $ – Number/Single/Real/Currency [3]

(d) 399000 H13


450000 H10 [2]

© Cambridge International Examinations 2015


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

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.

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

There are four errors in this code.

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

New @New+T1 * Posn

OUTPUT New

END
Complete a trace table for each of the two input values 5 and 12.

Trace table for input value 5

X Posn New T1 T2 OUTPUT

Trace table for input value 12

X Posn New T1 T2 OUTPUT

[6]

(b) State the purpose of the flowchart in part (a).

...............................................................................................................................................[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

(1 mark) (1 mark) (1 mark)

Number 2 Trace table


X Posn New T1 T2 Output

12 1 0

10 0 6 0

6 100 0 3 0

3 1000 100 1 1

1100

1100

(1 mark) (1 mark) (1 mark)


[6]

(b) Converts a (denary) number to binary [1]


4 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.

67$57

5LGHUV
5HMHFW

,1387
+HLJKW

,V <HV
+HLJKW 5HMHFW5HMHFW
"

1R

5LGHUV5LGHUV

1R 5LGHUV <HV 287387


" 5HDG\WRJR 5HMHFW

(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

Riders Reject Height OUTPUT

[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.

Loop structure 1 .........................................................................................................................

Description .................................................................................................................................

...................................................................................................................................................

Loop structure 2 .........................................................................................................................

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.

Several of the statements in the flowchart are missing.

Using item number only from the list below, complete the flowchart.

Item
Statement
number

1 Add flag to product record to indicate re-order made

2 Any more barcodes to scan?

3 Has the scanned barcode been found in the file?

4 Has the re-order flag already been added to the product record?

5 Is number of product in stock <= re-order level?

6 Number of product in stock is reduced by 1

7 Output an error message

8 Automatically send out order for new product


START

Barcode on product
is scanned END

No

Barcode is looked up Yes


in stock database

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.

One reward point is given for every $1 spent.

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 program is to be written with the following specification:

• read the points total from the card


• process the amount spent
• output the amount to be paid and the new points total

A user-defined function CalculatePoints has already been coded to calculate the new points
earned from the amount spent.

Study the following pseudocode:

INPUT AmountDue
NewPoints ←
CalculatePoints(AmountDue)
PointsTotal ←
PointsTotal + NewPoints

IF PointsTotal > 500


THEN
OUTPUT "Exchange points?"
INPUT Response
IF Response = "YES"
THEN
PointsTotal ←
PointsTotal – 500
AmountDue ←
AmountDue – 1
ENDIF
ENDIF

OUTPUT AmountDue, PointsTotal

The algorithm is also to be documented with a program flowchart.

Complete the flowchart by:

• filling in the flowchart boxes


• labelling, where appropriate, lines of the flowchart
CALL
CalculatePoints(AmountDue)

[6]
[Turn over
7

START START and STOP/END


to score

INPUT
Amount(Due)
A. CALCULATE
CALL points total
CalculatePoints(AmountDue)
In general – accept
descriptive assignment
PointsTotal = statements
PointsTotal + NewPoints

Condition + at least one


PointsTotal label to score
> 500
YES A. True/Tick

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.

The program generates pairs of random numbers:

• the first, in the range 1 to 4, to represent the suit


• the second, in the range 1 to 13, to represent the card value

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

(b) A representation of dealing out the cards is shown below:

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.

When each card is dealt, the appropriate cell changes from F to T.

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:

01 DECLARE SuitNum : INTEGER


02 DECLARE CardValue : INTEGER
03 DECLARE DealCount : INTEGER
04 DECLARE NewCard : BOOLEAN
05 DECLARE CardPack ...........................................................................................................
06
07 CALL InitialiseCardPack
08 DealCount ← 0
09 WHILE DealCount <> 52
10 NewCard ← FALSE
11 WHILE NewCard = FALSE
12 SuitNum ← RANDOM(1,4) // generates a random number
13 CardValue ← RANDOM(1,13) // in the range given
14 IF CardPack[SuitNum, CardValue] = FALSE
15 THEN
16 CardPack[SuitNum, CardValue] ← TRUE
17 NewCard ← TRUE
18 OUTPUT SuitNum, CardValue
19 ENDIF
20 ENDWHILE
21 DealCount ← DealCount + 1
22 ENDWHILE
23
24 // end of main program
25
26 PROCEDURE InitialiseCardPack
27 DECLARE i : INTEGER
28 DECLARE j : INTEGER
29 FOR i ← 1 TO 4
30 FOR j ← 1 TO 13
31 CardPack[i, j] ← FALSE
32 ENDFOR
33 ENDFOR
34 ENDPROCEDURE
Study the pseudocode and answer the questions below:

Give the line number for:

(i) A statement which marks the end of a count controlled loop.

.......................................................................................................................................[1]

(ii) The declaration of a local variable.

.......................................................................................................................................[1]

(iii) The initialisation of a variable used as a counter, but not to control a ‘count controlled’
loop.

.......................................................................................................................................[1]

(iv) A statement which uses a built-in function of the programming language.

.......................................................................................................................................[1]

(c) Give the number of procedures used by the pseudocode.

...............................................................................................................................................[1]

(d) Copy the condition which is used to control a ‘pre-condition’ loop.

...............................................................................................................................................[1]

(e) Explain the purpose of lines 14 – 19 in the design.

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................[2]

(f) Complete the declaration of the global variable at line 05.

05 DECLARE CardPack ....................................................................................................[1]

[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.

Card value Card


name
1 Ace
11 Jack
12 Queen
13 King

A new requirement is to display the name of the card, where appropriate.

Write a CASE structure using variable CardValue.

Assign to a new variable CardName either:

• the card value (2, 3, 4, 5, 6, 7, 8, 9 or 10)


• or where appropriate, the card name Ace, Jack, Queen or King

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...................................................................................................................................................

...............................................................................................................................................[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]

(b) (i) 32 // 33 [1]

(ii) 27 // 28 [1]

(iii) 08 [1]

(iv) 12 // 13 [1]

(c) 1 [1]

(d) DealCount <> 52 // NewCard = FALSE


Allow: Inclusion of the WHILE [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]

(f) CardPack ARRAY[1:4 , 1:13] OF/:/AS BOOLEAN [1]


Allow: parentheses

(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

Note: Must be double quotes present and correct case


Visual Basic
Select Case CardValue
Case 1
CardName = "Ace"
Case 11
CardName = "Jack"
Case 12
CardName = "Queen"
Case 13
CardName = "King"
Case Else // Case 2 to 10
CardName = Str(CardValue) [4]
End Select
Allow: omission of Str
9 A program is to process a set of integers.

The integers are stored in an array, Num. The first N elements are to be processed.

The pseudocode for this program is shown below:

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.

Identifier Data type Description

Num ARRAY[1:100] OF INTEGER The array of numbers.

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

4 1 final values are:

2 7 8 11 13 16

Note these final values will not be


Must be:
shown on the same row …
• Preceded by other entries …
• nothing after the 1, 2 sequence

[8]

(ii) To sort / to order/put in ascending order the items (in the array) [1]

(iii) There were no swaps on the last pass / on pass 4 [1]


(b)
Identifier Data Type Description

Num

N INTEGER The number of numbers in the list

i INTEGER Loop counter // The number of ‘passes’


up through the list

j INTEGER The index // position in the array

Temp INTEGER Description must imply/states the


‘swapping’ operation

Mark as follows:
INTEGER × 4 [1]
One mark per description [4]

You might also like