0% found this document useful (0 votes)
0 views

Pseudocode 2015

This document is an examination paper for Cambridge International Advanced Level Computer Science, covering fundamental problem-solving and programming skills. It includes various questions requiring pseudocode, program code, and algorithm design related to topics such as race time calculations, menu systems, insurance quotations, and sales data processing. The paper consists of 19 printed pages and is designed for candidates to demonstrate their programming knowledge and skills within a 2-hour time frame.

Uploaded by

Frederic Lafitte
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Pseudocode 2015

This document is an examination paper for Cambridge International Advanced Level Computer Science, covering fundamental problem-solving and programming skills. It includes various questions requiring pseudocode, program code, and algorithm design related to topics such as race time calculations, menu systems, insurance quotations, and sales data processing. The paper consists of 19 printed pages and is designed for candidates to demonstrate their programming knowledge and skills within a 2-hour time frame.

Uploaded by

Frederic Lafitte
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 112

Cambridge International Examinations

Cambridge International Advanced Subsidiary and Advanced Level


* 5 5 0 9 2 4 3 6 5 1 *

COMPUTER SCIENCE 9608/21


Paper 2 Fundamental Problem-solving and Programming Skills May/June 2015
2 hours
Candidates answer on the Question Paper.
No Additional Materials are required.
No calculators allowed.

READ THESE INSTRUCTIONS FIRST

Write your Centre number, candidate number and name in the spaces at the top of this page.
Write in dark blue or black pen.
You may use an HB pencil for any diagrams, graphs or rough working.
Do not use staples, paper clips, glue or correction fluid.
DO NOT WRITE IN ANY BARCODES.

Answer all questions.


No marks will be awarded for using brand names of software packages or hardware.

At the end of the examination, fasten all your work securely together.
The number of marks is given in brackets [ ] at the end of each question or part question.

The maximum number of marks is 75.

This document consists of 19 printed pages and 1 blank page.

DC (LK/JG) 95392/5
© UCLES 2015 [Turn over
2

Throughout the paper you will be asked to write either pseudocode or program code.

Complete the statement to indicate which high-level programming language you will use.

Programming language ...........................................................................................................................

1 A marathon runner records their time for a race in hours, minutes and seconds.

An algorithm is shown below in structured English.

INPUT race time as hours, minutes and seconds


CALCULATE race time in seconds
STORE race time in seconds
OUTPUT race time in seconds

(a) The identifier table needs to show the variables required to write a program for this algorithm.

Complete the table.

Identifier Data type Description


RaceHours INTEGER The hours part of the race time.

[3]

(b) Before the program is written, the design is amended.

The new design includes input of the runner’s current personal best marathon time (in
seconds).

The output will now also show one of the following messages:

• “Personal best time is unchanged”


• “New personal best time”
• “Equals personal best time”

(i) Show the additional variable needed for the new design.

Identifier Data type Description

[1]

© UCLES 2015 9608/21/M/J/15


3

(ii) Write program code for the new design.


Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.

Programming language ............................................................................................................

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

...............................................................................................................................................[7]

© UCLES 2015 9608/21/M/J/15 [Turn over


4

(c) The program code will be tested using white-box testing.

(i) Explain what is meant by white-box testing.

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

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

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

(ii) Complete the table heading.


Complete Test Number 1.
Add the data for Test Number 2 and Test Number 3.

Input values Output


Test Race Race Race Total time
number hours minutes seconds ....................... (seconds) Message
1 3 4 13 11053 11053

2 11053

3 11053

[6]

© UCLES 2015 9608/21/M/J/15


5

2 A program displays a menu with choices 1 to 4. The code to display the menu is written as the
procedure DisplayMenu.

(a) Pseudocode which uses this procedure is:

CALL DisplayMenu
REPEAT
OUTPUT "Enter choice (1..4)"
INPUT Choice
UNTIL Choice >= 1 AND Choice <= 4

(i) Describe what this pseudocode will do.

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

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

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

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

(ii) State why a loop is required.

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

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

(b) The following pseudocode is a revised design.

CONSTANT i 3
CALL DisplayMenu
NoOfAttempts 0
REPEAT
OUTPUT "Enter choice (1..4)"
INPUT Choice
NoOfAttempts NoOfAttempts + 1
UNTIL (Choice >= 1 AND Choice <= 4) OR NoOfAttempts = i

(i) Give the maximum number of inputs the user could be prompted to make.

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

(ii) State why this algorithm is an improvement on the one given in part (a).

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

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

© UCLES 2015 9608/21/M/J/15 [Turn over


6

(c) The pseudocode is in its initial stage of development.

The table below shows the action currently taken by the pseudocode following each menu
choice.

Menu
Description Program response
choice
Read data from the Calls a procedure ReadFile which for testing
1
customer file purposes outputs the message “Read file code”
2 Add a customer Outputs message “Add customer code”

3 Search for a customer Outputs message “Search customer code”

4 Terminates the program Ends

Complete the pseudocode for the design in part (b), shown again below, to respond to each
menu choice.

CONSTANT i 3
CALL DisplayMenu
NoOfAttempts 0
REPEAT
OUTPUT "Enter choice (1..4)"
INPUT Choice
NoOfAttempts NoOfAttempts + 1
UNTIL (Choice >= 1 AND Choice <= 4) OR NoOfAttempts = i

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

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

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

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

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

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

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

© UCLES 2015 9608/21/M/J/15


7

(d) The algorithm in part (c) is to be amended. The program will:


• repeatedly display the menu and respond to the user’s choice
• terminate when the user enters 4

Write program code for this final design which will be made up of:
• the main program
• procedure ReadFile
• procedure DisplayMenu

Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.

Programming language ............................................................................................................

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

...............................................................................................................................................[8]
© UCLES 2015 9608/21/M/J/15 [Turn over
8

3 When the guarantee on a computer runs out, the owner can take out insurance to cover breakdown
and repairs.

The price of the insurance is calculated from:

• the model of the computer


• the age of the computer
• the current insurance rates

Following an enquiry to the insurance company, the customer receives a quotation letter with the
price of the insurance.

A program is to be produced.

The structure chart below shows the modular design for this process:

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

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

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

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

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

.................................... ....................................
Premium rates

Premium rates

access premium rates


database ....................................

© UCLES 2015 9608/21/M/J/15


9

(a) Using the letters A to D, add the labelling to the chart boxes on the opposite page.

Modules
A Send quotation letter
B Calculate price
C Produce insurance quotation
D Input computer details
[2]

(b) Using the letters E to J, complete the labelling on the chart opposite.

Some of these letters will be used more than once.

Data items
E CustomerName
F CustomerEmail
G Model
H Age
I PolicyCharge
J PolicyNumber
[4]

© UCLES 2015 9608/21/M/J/15 [Turn over


10

4 A game is played between two players:

• they take turns at rolling a six-sided die (numbered 1 to 6) and record their throw
• a player scores 1 point if their throw is higher than their opponent
• they each roll the die 20 times
• if the player’s throw is the same as their opponent, the total points is unchanged
• the winner is the player with the larger number of points after 20 throws

The pseudocode will use variable NoOfThrows as shown.

Identifier Data type Description


NoOfThrows INTEGER Loop control variable

(i) Complete the pseudocode for the given algorithm.

FOR ....................................................................................................................................

INPUT Player1Throw

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

IF Player1Throw > Player2Throw


THEN

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

ENDIF
IF Player2Throw > Player1Throw
THEN
Player2Total Player2Total + 1
ENDIF

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

IF Player1Total > Player2Total


THEN
OUTPUT "Player1 is the winner"
ELSE
OUTPUT "Player2 is the winner"
ENDIF
[5]

(ii) Identify the game result which will produce incorrect output.

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

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

© UCLES 2015 9608/21/M/J/15


11

Question 5 begins on page 12.

© UCLES 2015 9608/21/M/J/15 [Turn over


12

5 A company creates two new websites, Site X and Site Y, for selling bicycles.

Various programs are to be written to process the sales data.

These programs will use data about daily sales made from Site X (using variable SalesX) and
Site Y (using variable SalesY).

Data for the first 28 days is shown below.

SalesDate SalesX SalesY


1 03/06/2015 0 1
2 04/06/2015 1 2
3 05/06/2015 3 8
4 06/06/2015 0 0
5 07/06/2015 4 6
6 08/06/2015 4 4
7 09/06/2015 5 9
8 10/06/2015 11 9
9 11/06/2015 4 1

28 01/07/2015 14 8

(a) Name the data structure to be used in a program for SalesX.

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

© UCLES 2015 9608/21/M/J/15


13

(b) The programmer writes a program from the following pseudocode design.

x 0
FOR DayNumber 1 TO 7
IF SalesX[DayNumber] + SalesY[DayNumber] >= 10
THEN
x x + 1
OUTPUT SalesDate[DayNumber]
ENDIF
ENDFOR
OUTPUT x

(i) Trace the execution of this pseudocode by completing the trace table below.

x DayNumber OUTPUT
0

[4]

(ii) Describe, in detail, what this algorithm does.

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

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

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

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

© UCLES 2015 9608/21/M/J/15 [Turn over


14

(c) The company wants a program to output the total monthly sales for one of the selected
websites.

The programmer codes a function with the following function header:

FUNCTION MonthlyWebSiteSales(ThisMonth : INTEGER, ThisSite : CHAR)


RETURNS INTEGER

The function returns the total number of bicycles sold for the given month and website.

The function will use the following:

Identifier Data type Description


Represents the month number e.g.
ThisMonth INTEGER
4 represents April
Coded as:
ThisSite CHAR • X for website X
• Y for Website Y

(i) Give the number of parameters of this function. ..............................................[1]

(ii) Some of the following function calls may be invalid.

Mark each call with:


• a tick (✓), for a valid call
• a cross (✗), for an invalid call

For any function calls which are invalid, explain why.

Tick (✓) Explanation


Function call
/ cross (✗) (if invalid)

MonthlyWebSiteSales(1, "Y")

MonthlyWebSiteSales(11, 'X', 'Y')

MonthlyWebSiteSales(12, 'X')

[3]

© UCLES 2015 9608/21/M/J/15


15

(d) The company decides to offer a discount on selected dates. A program is written to indicate
the dates on which a discount is offered.

The program creates a text file, DISCOUNT_DATES (with data as shown), for a number of
consecutive dates.

03/06/2015 TRUE
04/06/2015 FALSE
05/06/2015 FALSE
06/06/2015 FALSE
07/06/2015 FALSE
08/06/2015 FALSE
09/06/2015 FALSE
10/06/2015 TRUE
11/06/2015 FALSE

01/07/2015 FALSE

Each date and discount indicator is separated by a single <Space> character.

The discount indicators are:

• FALSE – indicates a date on which no discount is offered


• TRUE – indicates a date on which a discount is offered

A programming language has the built-in function CONCAT defined as follows:

CONCAT(String1 : STRING, String2 : STRING [, String3 : STRING] )


RETURNS STRING
For example: CONCAT("San", "Francisco") returns "SanFrancisco"
CONCAT("New", "York", "City") returns "NewYorkCity"

The use of the square brackets indicates that the parameter is optional.

© UCLES 2015 9608/21/M/J/15 [Turn over


16

The following incomplete pseudocode creates the text file DISCOUNT_DATES.

Complete the pseudocode.

OPENFILE "DISCOUNT_DATES" FOR ..................................................................................

INPUT ......................................................................................................................................

WHILE NextDate <>"XXX"

INPUT Discount

…………..…………………… = CONCAT(NextDate, " ", Discount)

WRITEFILE "DISCOUNT_DATES", NextLine

INPUT NextDate

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

OUTPUT "File now created"

CLOSEFILE [4]

© UCLES 2015 9608/21/M/J/15


17

Question 5(e) continues on page 18.

© UCLES 2015 9608/21/M/J/15 [Turn over


18

(e) The DISCOUNT_DATES text file is successfully created.

The company now wants a program to:


• key in a date entered by the user
• search the text file for this date
• if found, output one of the following messages:
o “No discount on this date”
o “This is a discount date”
• if not found, output “Date not found”

(i) Add to the identifier table to show the variables you need for this new program.

Identifier Data type Description


DISCOUNT_DATES FILE Text file to be used

[3]

© UCLES 2015 9608/21/M/J/15


19

(ii) Write the program code.

Do not include any declaration or comment statements for the variables used.

Programming language .....................................................................................................

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

.......................................................................................................................................[7]

© UCLES 2015 9608/21/M/J/15


20

BLANK PAGE

Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every reasonable
effort has been made by the publisher (UCLES) to trace copyright holders, but if any items requiring clearance have unwittingly been included, the publisher will
be pleased to make amends at the earliest possible opportunity.

To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge International
Examinations Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download at www.cie.org.uk after
the live examination series.

Cambridge International Examinations is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of University of Cambridge Local
Examinations Syndicate (UCLES), which is itself a department of the University of Cambridge.

© UCLES 2015 9608/21/M/J/15


Cambridge International Examinations
Cambridge International Advanced Subsidiary and Advanced Level
* 8 0 1 6 0 5 0 0 7 0 *

COMPUTER SCIENCE 9608/22


Paper 2 Fundamental Problem-solving and Programming Skills May/June 2015
2 hours
Candidates answer on the Question Paper.
No Additional Materials are required.
No calculators allowed.

READ THESE INSTRUCTIONS FIRST

Write your Centre number, candidate number and name in the spaces at the top of this page.
Write in dark blue or black pen.
You may use an HB pencil for any diagrams, graphs or rough working.
Do not use staples, paper clips, glue or correction fluid.
DO NOT WRITE IN ANY BARCODES.

Answer all questions.


No marks will be awarded for using brand names of software packages or hardware.

At the end of the examination, fasten all your work securely together.
The number of marks is given in brackets [ ] at the end of each question or part question.

The maximum number of marks is 75.

This document consists of 19 printed pages and 1 blank page.

DC (NF/JG) 112056/1
© UCLES 2015 [Turn over
2

Throughout the paper you will be asked to write either pseudocode or program code.

Complete the statement to indicate which high-level programming language you will use.

Programming language ...........................................................................................................................

1 A marathon runner records their time for a race in hours, minutes and seconds.

An algorithm is shown below in structured English.

INPUT race time as hours, minutes and seconds


CALCULATE race time in seconds
STORE race time in seconds
OUTPUT race time in seconds

(a) The identifier table needs to show the variables required to write a program for this algorithm.

Complete the table.

Identifier Data type Description


RaceHours INTEGER The hours part of the race time.

[3]

(b) Before the program is written, the design is amended.

The new design includes input of the runner’s current personal best marathon time (in
seconds).

The output will now also show one of the following messages:

• “Personal best time is unchanged”


• “New personal best time”
• “Equals personal best time”

(i) Show the additional variable needed for the new design.

Identifier Data type Description

[1]

© UCLES 2015 9608/22/M/J/15


3

(ii) Write program code for the new design.


Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.

Programming language ............................................................................................................

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

...............................................................................................................................................[7]

© UCLES 2015 9608/22/M/J/15 [Turn over


4

(c) The program code will be tested using white-box testing.

(i) Explain what is meant by white-box testing.

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

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

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

(ii) Complete the table heading.


Complete Test Number 1.
Add the data for Test Number 2 and Test Number 3.

Input values Output


Test Race Race Race Total time
number hours minutes seconds ....................... (seconds) Message
1 3 4 13 11053 11053

2 11053

3 11053

[6]

© UCLES 2015 9608/22/M/J/15


5

2 A program displays a menu with choices 1 to 4. The code to display the menu is written as the
procedure DisplayMenu.

(a) Pseudocode which uses this procedure is:

CALL DisplayMenu
REPEAT
OUTPUT "Enter choice (1..4)"
INPUT Choice
UNTIL Choice >= 1 AND Choice <= 4

(i) Describe what this pseudocode will do.

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

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

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

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

(ii) State why a loop is required.

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

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

(b) The following pseudocode is a revised design.

CONSTANT i 3
CALL DisplayMenu
NoOfAttempts 0
REPEAT
OUTPUT "Enter choice (1..4)"
INPUT Choice
NoOfAttempts NoOfAttempts + 1
UNTIL (Choice >= 1 AND Choice <= 4) OR NoOfAttempts = i

(i) Give the maximum number of inputs the user could be prompted to make.

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

(ii) State why this algorithm is an improvement on the one given in part (a).

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

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

© UCLES 2015 9608/22/M/J/15 [Turn over


6

(c) The pseudocode is in its initial stage of development.

The table below shows the action currently taken by the pseudocode following each menu
choice.

Menu
Description Program response
choice
Read data from the Calls a procedure ReadFile which for testing
1
customer file purposes outputs the message “Read file code”
2 Add a customer Outputs message “Add customer code”

3 Search for a customer Outputs message “Search customer code”

4 Terminates the program Ends

Complete the pseudocode for the design in part (b), shown again below, to respond to each
menu choice.

CONSTANT i 3
CALL DisplayMenu
NoOfAttempts 0
REPEAT
OUTPUT "Enter choice (1..4)"
INPUT Choice
NoOfAttempts NoOfAttempts + 1
UNTIL (Choice >= 1 AND Choice <= 4) OR NoOfAttempts = i

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

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

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

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

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

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

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

© UCLES 2015 9608/22/M/J/15


7

(d) The algorithm in part (c) is to be amended. The program will:


• repeatedly display the menu and respond to the user’s choice
• terminate when the user enters 4

Write program code for this final design which will be made up of:
• the main program
• procedure ReadFile
• procedure DisplayMenu

Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.

Programming language ............................................................................................................

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

...............................................................................................................................................[8]
© UCLES 2015 9608/22/M/J/15 [Turn over
8

3 When the guarantee on a computer runs out, the owner can take out insurance to cover breakdown
and repairs.

The price of the insurance is calculated from:

• the model of the computer


• the age of the computer
• the current insurance rates

Following an enquiry to the insurance company, the customer receives a quotation letter with the
price of the insurance.

A program is to be produced.

The structure chart below shows the modular design for this process:

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

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

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

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

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

.................................... ....................................
Premium rates

Premium rates

access premium rates


database ....................................

© UCLES 2015 9608/22/M/J/15


9

(a) Using the letters A to D, add the labelling to the chart boxes on the opposite page.

Modules
A Send quotation letter
B Calculate price
C Produce insurance quotation
D Input computer details
[2]

(b) Using the letters E to J, complete the labelling on the chart opposite.

Some of these letters will be used more than once.

Data items
E CustomerName
F CustomerEmail
G Model
H Age
I PolicyCharge
J PolicyNumber
[4]

© UCLES 2015 9608/22/M/J/15 [Turn over


10

4 A game is played between two players:

• they take turns at rolling a six-sided die (numbered 1 to 6) and record their throw
• a player scores 1 point if their throw is higher than their opponent
• they each roll the die 20 times
• if the player’s throw is the same as their opponent, the total points is unchanged
• the winner is the player with the larger number of points after 20 throws

The pseudocode will use variable NoOfThrows as shown.

Identifier Data type Description


NoOfThrows INTEGER Loop control variable

(i) Complete the pseudocode for the given algorithm.

FOR ....................................................................................................................................

INPUT Player1Throw

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

IF Player1Throw > Player2Throw


THEN

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

ENDIF
IF Player2Throw > Player1Throw
THEN
Player2Total Player2Total + 1
ENDIF

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

IF Player1Total > Player2Total


THEN
OUTPUT "Player1 is the winner"
ELSE
OUTPUT "Player2 is the winner"
ENDIF
[5]

(ii) Identify the game result which will produce incorrect output.

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

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

© UCLES 2015 9608/22/M/J/15


11

Question 5 begins on page 12.

© UCLES 2015 9608/22/M/J/15 [Turn over


12

5 A company creates two new websites, Site X and Site Y, for selling bicycles.

Various programs are to be written to process the sales data.

These programs will use data about daily sales made from Site X (using variable SalesX) and
Site Y (using variable SalesY).

Data for the first 28 days is shown below.

SalesDate SalesX SalesY


1 03/06/2015 0 1
2 04/06/2015 1 2
3 05/06/2015 3 8
4 06/06/2015 0 0
5 07/06/2015 4 6
6 08/06/2015 4 4
7 09/06/2015 5 9
8 10/06/2015 11 9
9 11/06/2015 4 1

28 01/07/2015 14 8

(a) Name the data structure to be used in a program for SalesX.

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

© UCLES 2015 9608/22/M/J/15


13

(b) The programmer writes a program from the following pseudocode design.

x 0
FOR DayNumber 1 TO 7
IF SalesX[DayNumber] + SalesY[DayNumber] >= 10
THEN
x x + 1
OUTPUT SalesDate[DayNumber]
ENDIF
ENDFOR
OUTPUT x

(i) Trace the execution of this pseudocode by completing the trace table below.

x DayNumber OUTPUT
0

[4]

(ii) Describe, in detail, what this algorithm does.

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

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

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

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

© UCLES 2015 9608/22/M/J/15 [Turn over


14

(c) The company wants a program to output the total monthly sales for one of the selected
websites.

The programmer codes a function with the following function header:

FUNCTION MonthlyWebSiteSales(ThisMonth : INTEGER, ThisSite : CHAR)


RETURNS INTEGER

The function returns the total number of bicycles sold for the given month and website.

The function will use the following:

Identifier Data type Description


Represents the month number e.g.
ThisMonth INTEGER
4 represents April
Coded as:
ThisSite CHAR • X for website X
• Y for Website Y

(i) Give the number of parameters of this function. ..............................................[1]

(ii) Some of the following function calls may be invalid.

Mark each call with:


• a tick (✓), for a valid call
• a cross (✗), for an invalid call

For any function calls which are invalid, explain why.

Tick (✓) Explanation


Function call
/ cross (✗) (if invalid)

MonthlyWebSiteSales(1, "Y")

MonthlyWebSiteSales(11, 'X', 'Y')

MonthlyWebSiteSales(12, 'X')

[3]

© UCLES 2015 9608/22/M/J/15


15

(d) The company decides to offer a discount on selected dates. A program is written to indicate
the dates on which a discount is offered.

The program creates a text file, DISCOUNT_DATES (with data as shown), for a number of
consecutive dates.

03/06/2015 TRUE
04/06/2015 FALSE
05/06/2015 FALSE
06/06/2015 FALSE
07/06/2015 FALSE
08/06/2015 FALSE
09/06/2015 FALSE
10/06/2015 TRUE
11/06/2015 FALSE

01/07/2015 FALSE

Each date and discount indicator is separated by a single <Space> character.

The discount indicators are:

• FALSE – indicates a date on which no discount is offered


• TRUE – indicates a date on which a discount is offered

A programming language has the built-in function CONCAT defined as follows:

CONCAT(String1 : STRING, String2 : STRING [, String3 : STRING] )


RETURNS STRING
For example: CONCAT("San", "Francisco") returns "SanFrancisco"
CONCAT("New", "York", "City") returns "NewYorkCity"

The use of the square brackets indicates that the parameter is optional.

© UCLES 2015 9608/22/M/J/15 [Turn over


16

The following incomplete pseudocode creates the text file DISCOUNT_DATES.

Complete the pseudocode.

OPENFILE "DISCOUNT_DATES" FOR ..................................................................................

INPUT ......................................................................................................................................

WHILE NextDate <>"XXX"

INPUT Discount

…………..…………………… = CONCAT(NextDate, " ", Discount)

WRITEFILE "DISCOUNT_DATES", NextLine

INPUT NextDate

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

OUTPUT "File now created"

CLOSEFILE [4]

© UCLES 2015 9608/22/M/J/15


17

Question 5(e) continues on page 18.

© UCLES 2015 9608/22/M/J/15 [Turn over


18

(e) The DISCOUNT_DATES text file is successfully created.

The company now wants a program to:


• key in a date entered by the user
• search the text file for this date
• if found, output one of the following messages:
o “No discount on this date”
o “This is a discount date”
• if not found, output “Date not found”

(i) Add to the identifier table to show the variables you need for this new program.

Identifier Data type Description


DISCOUNT_DATES FILE Text file to be used

[3]

© UCLES 2015 9608/22/M/J/15


19

(ii) Write the program code.

Do not include any declaration or comment statements for the variables used.

Programming language .....................................................................................................

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

.......................................................................................................................................[7]

© UCLES 2015 9608/22/M/J/15


20

BLANK PAGE

Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every reasonable
effort has been made by the publisher (UCLES) to trace copyright holders, but if any items requiring clearance have unwittingly been included, the publisher will
be pleased to make amends at the earliest possible opportunity.

To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge International
Examinations Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download at www.cie.org.uk after
the live examination series.

Cambridge International Examinations is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of University of Cambridge Local
Examinations Syndicate (UCLES), which is itself a department of the University of Cambridge.

© UCLES 2015 9608/22/M/J/15


Cambridge International Examinations
Cambridge International Advanced Subsidiary and Advanced Level
* 9 0 6 6 0 7 6 3 4 2 *

COMPUTER SCIENCE 9608/23


Paper 2 Fundamental Problem-solving and Programming Skills May/June 2015
2 hours
Candidates answer on the Question Paper.
No Additional Materials are required.
No calculators allowed.

READ THESE INSTRUCTIONS FIRST

Write your Centre number, candidate number and name in the spaces at the top of this page.
Write in dark blue or black pen.
You may use an HB pencil for any diagrams, graphs or rough working.
Do not use staples, paper clips, glue or correction fluid.
DO NOT WRITE IN ANY BARCODES.

Answer all questions.


No marks will be awarded for using brand names of software packages or hardware.

At the end of the examination, fasten all your work securely together.
The number of marks is given in brackets [ ] at the end of each question or part question.

The maximum number of marks is 75.

This document consists of 14 printed pages and 2 blank pages.

DC (LK/JG) 95389/4
© UCLES 2015 [Turn over
2

Throughout the paper you will be asked to write either pseudocode or program code.

Complete the statement to indicate which high-level programming language you will use.

Programming language ...........................................................................................................................

1 Horses are entered for a horse race. A horse may have to carry a penalty weight in addition to the
rider. This weight is added to the saddle. The penalty weight (if any) depends on the number of
wins the horse has achieved in previous races.

The penalty weight is calculated as follows:

Number of previous wins Penalty weight (kg)


0 0
1 or 2 4
Over 2 8

A program is to be written from the following structured English design.

1 INPUT name of horse


2 INPUT number of previous wins
3 CALCULATE penalty weight
4 STORE penalty weight
5 OUTPUT name of horse, penalty weight

(a) Complete the identifier table showing the variables needed to code the program.

Identifier Data type Description

[3]

(b) Line 3 in the algorithm above does not give the detail about how the race penalty weight is
calculated; this step in the algorithm must be expressed in more detail.

(i) The algorithm above currently has five stages. One technique for program design is to
further break down, where required, any stage to a level of detail from which the program
code can be written.

Name this technique.

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

© UCLES 2015 9608/23/M/J/15


3

(ii) Write pseudocode for the given structured English design.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

.......................................................................................................................................[5]

© UCLES 2015 9608/23/M/J/15 [Turn over


4

2 (a) Two operators available in a programming language are DIV and MOD. They perform integer
arithmetic as follows:

Expression Explanation
X DIV Y Computes the number of times Y divides into X

X MOD Y Computes the remainder when X is divided by Y

Calculate the value of the variables shown for the following code fragments.

Code Variable
(i) NumberLeftOver 37 MOD 10
NumberLeftOver ..................
[1]
(ii) Quantity 208

BoxSize 100

NumberOfBoxes Quantity DIV BoxSize NumberOfBoxes ....................

Temp (Quantity MOD BoxSize) + 1 Temp .......................................


[2]

(b) Bank customers withdraw money from their account at a cash dispenser machine using their
bank card. The machine operates as follows:

• it can dispense the following notes:


o $50
o $20
o $10
• the maximum amount for a single withdrawal is $500

When a customer withdraws money, they enter the amount to withdraw. (This must be a
multiple of $10).

The machine will always dispense the least possible number of notes.

A program is designed for the machine to process a withdrawal.

The following variables are used:

Identifier Data type Description


Amount INTEGER Amount to withdraw entered by the user

FiftyDollar INTEGER Number of $50 notes to dispense

TwentyDollar INTEGER Number of $20 notes to dispense

TenDollar INTEGER Number of $10 notes to dispense

Temp INTEGER Used in the calculation of the number of each note required

© UCLES 2015 9608/23/M/J/15


5

(i) The following four tests have been designed.

Complete the test data table showing the expected results with comments.

Input value Output


Comment
Amount FiftyDollar TwentyDollar TenDollar

70 1 1 0 Least possible number of notes

85

130

600
[3]

(ii) Complete the pseudocode.

INPUT ..................................................................................................................................
IF Amount > 500
THEN
OUTPUT "Refused – amount too large"
ELSE
..................................................................................................................................

THEN
OUTPUT "Refused - not a multiple of $10"
ELSE
FiftyDollar Amount DIV 50
Temp ....................................................................................................
TwentyDollar ....................................................................................
Temp .....................................................................................................
.....................................................................................................................
ENDIF
ENDIF
[5]

© UCLES 2015 9608/23/M/J/15 [Turn over


6

3 A flooring company provides for each customer an estimated price for a new job. Each job is
given a Job ID.

The job cost is calculated from the length (nearest metre) and width (nearest metre) of the room.

The process for calculating the price is as follows:

• the floor area is calculated with 18% added to allow for wastage
• the job cost is calculated at $50 per square metre

The structure chart shows the modular design for a program to produce a new job cost.

Produce flooring
job cost

Length
B D JobID

A
JobCost E
Width
CustomerName

Input job data Calculate job cost Send estimate


to customer

(i) Give the data items corresponding to the labels A to E in the structure chart.

A ....................................................................................................................................

B ....................................................................................................................................

C ....................................................................................................................................

D ....................................................................................................................................

E ....................................................................................................................................

[5]

© UCLES 2015 9608/23/M/J/15


7

(ii) The procedure below is one of the modules shown on the structure chart.

Parameters can be passed ‘by value’ or ‘by reference’.

Complete the procedure header below showing for each parameter:

• its parameter passing mechanism


• its identifier
• its data type

PROCEDURE CalculateJobCost( ...............................................................................................

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

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

............................................................................................ )

JobCost (Length * Width * 1.18) * 50

ENDPROCEDURE

[5]

© UCLES 2015 9608/23/M/J/15 [Turn over


8

4 A programming language has the built-in function CONCAT defined as follows:

CONCAT(String1 : STRING, String2 : STRING [, String3 : STRING] ) RETURNS STRING


For example: CONCAT("San", "Francisco") returns "SanFrancisco"
CONCAT("New", "York", "City") returns "NewYorkCity"

The use of the square brackets indicates that the parameter is optional.

(a) State the value returned by the following expressions.

If the expression is not properly formed, write ERROR.

(i) CONCAT("Studio", 54) ................................................... [1]

(ii) CONCAT("parity", "error", "check") ................................................... [1]

(iii) CONCAT(CONCAT("Binary", "▼", "Coded"), "▼", "Decimal")

▼ indicates a <Space> character

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

(b) A country has a number of banks. There are cash dispensers all over the country. Each bank
is responsible for a number of dispensers.

• banks have a three digit code in the range 001 – 999


• each dispenser has a five digit code in the range 00001 – 99999

A text file, DISPENSERS, is to be created.

It has one line of text for each dispenser. For example: 00342▼007.

This line in the file is the data for dispenser 00342 which belongs to bank 007.

Incomplete pseudocode follows for the creation of the file DISPENSERS.

© UCLES 2015 9608/23/M/J/15


9

For the creation of the file, data is entered by the user at the keyboard.

(i) Complete the pseudocode.

OPENFILE ........................................................ FOR WRITE

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

OUTPUT "Enter dispenser code (XXXXX to end)"


INPUT DispenserCode
IF DispenserCode <> "XXXXX"
THEN
OUTPUT "Enter bank code"
INPUT BankCode
LineString CONCAT(................................................. , "▼", BankCode)
// now write the new line to the file

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

ENDIF

UNTIL ..................................................................................................................................

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

OUTPUT "DISPENSERS file now created" [6]

(ii) No attempt has been made to validate the data entered by the user.

Describe two different types of validation check for the data entry.

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

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

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

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

(iii) The programmer coded this algorithm above and the user successfully entered 15
dispenser records into the text file.

There is data for another 546 dispensers which needs to be added.

State the error that will occur if the user runs the program a second time for further data
entry.

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

(iv) Give the ‘file mode’ available in the programming language which will be used to address
this issue.

.......................................................................................................................................[1]
© UCLES 2015 9608/23/M/J/15 [Turn over
10

(c) The complete data file is created with the structure shown.

A new program is to be written to search the file. 00001▼007


00002▼001
The program will: 00003▼002
00004▼003
• input a bank code 00005▼101
• output a list of all the dispensers which belong to this bank 00006▼004
00007▼004
• output the total number of dispensers for this bank

00024▼002
00025▼003
An example of a run of the program is shown: 00026▼007
00027▼007
00028▼102
Enter bank code 007
00001
00011
00022 99867▼013
00026
00027

There are 5 dispensers for this bank

© UCLES 2015 9608/23/M/J/15


11

Write the program code. Do not attempt to include any validation checks.
Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.

Programming language ............................................................................................................

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

.............................................................................................................................................[10]

© UCLES 2015 9608/23/M/J/15 [Turn over


12

5 A firm employs workers who assemble amplifiers. Each member of staff works an agreed number
of hours each day.

The firm records the number of completed amplifiers made by each employee each day.

Management monitor the performance of all its workers.

Production data was collected for 3 workers over 4 days.

Daily hours
worked Production data
Worker 1 5 Worker 1 Worker 2 Worker 3
Worker 2 10 Day 1 10 20 9

Worker 3 10 Day 2 11 16 11

Day 3 10 24 13

Day 4 14 20 17

A program is to be written to process the production data.

(a) The production data is to be stored in a 2-dimensional array ProductionData, declared as


follows:

DECLARE ProductionData ARRAY[1:4, 1:3] : INTEGER

(i) Describe two features of an array.

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

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

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

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

(ii) Give the value of ProductionData[3, 2].

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

(iii) Describe the information produced by the expression:

ProductionData[2, 1] + ProductionData[2, 2] + ProductionData[2, 3]

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

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

© UCLES 2015 9608/23/M/J/15


13

(b) Complete the trace table for the pseudocode algorithm below.

FOR WorkerNum 1 TO 3
WorkerTotal[WorkerNum] 0
ENDFOR

FOR WorkerNum 1 TO 3
FOR DayNum 1 TO 4
WorkerTotal[WorkerNum] WorkerTotal[WorkerNum] +
ProductionData[DayNum, WorkerNum]
ENDFOR
ENDFOR

FOR WorkerNum 1 TO 3
WorkerAverage WorkerTotal[WorkerNum]/
(4 * DailyHoursWorked[WorkerNum])
IF WorkerAverage < 2
THEN
OUTPUT “Investigate“, WorkerNum
ENDIF
ENDFOR

WorkerTotal
WorkerNum DayNum WorkerAverage OUTPUT 1 2 3

[8]

© UCLES 2015 9608/23/M/J/15 [Turn over


14

(c) An experienced programmer suggests that the pseudocode would be best implemented as a
procedure AnalyseProductionData.

Assume that both arrays, DailyHoursWorked and ProductionData, are available to the
procedure from the main program and they are of the appropriate size.

PROCEDURE AnalyseProductionData(NumDays : INTEGER, NumWorkers : INTEGER)

DECLARE ........................................................................................................................................

DECLARE ...................................................................................................................................... .

DECLARE ....................................................................................................................................... .

DECLARE ...................................................................................................................................... .

FOR WorkerNum 1 TO 3
WorkerTotal[WorkerNum] 0
ENDFOR

FOR WorkerNum 1 TO 3
FOR DayNum 1 TO 4
WorkerTotal[WorkerNum] WorkerTotal[WorkerNum] +
ProductionData[DayNum, WorkerNum]
ENDFOR
ENDFOR

FOR WorkerNum 1 TO 3
WorkerAverage WorkerTotal[WorkerNum]/
(4 * DailyHoursWorked [WorkerNum])
IF WorkerAverage < 2
THEN
OUTPUT "Investigate", WorkerNum
ENDIF
ENDFOR

ENDPROCEDURE

(i) Complete the declaration statements showing the local variables. [4]

(ii) The original pseudocode has been ‘pasted’ under the procedure header.

Circle all the places in the original pseudocode where changes will need to be made.

Write the changes which need to be made next to each circle. [3]

(iii) Write the statement for a procedure call which processes data for 7 days for 13 workers.

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

© UCLES 2015 9608/23/M/J/15


15

BLANK PAGE

© UCLES 2015 9608/23/M/J/15


16

BLANK PAGE

Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every reasonable
effort has been made by the publisher (UCLES) to trace copyright holders, but if any items requiring clearance have unwittingly been included, the publisher will
be pleased to make amends at the earliest possible opportunity.

To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge International
Examinations Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download at www.cie.org.uk after
the live examination series.

Cambridge International Examinations is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of University of Cambridge Local
Examinations Syndicate (UCLES), which is itself a department of the University of Cambridge.

© UCLES 2015 9608/23/M/J/15


Cambridge International Examinations
Cambridge International Advanced Subsidiary and Advanced Level
* 9 7 0 2 7 5 7 8 6 6 *

COMPUTER SCIENCE 9608/21


Paper 2 Fundamental Problem-solving and Programming Skills October/November 2015
2 hours
Candidates answer on the Question Paper.
No Additional Materials are required.
No calculators allowed.

READ THESE INSTRUCTIONS FIRST

Write your Centre number, candidate number and name in the spaces at the top of this page.
Write in dark blue or black pen.
You may use an HB pencil for any diagrams, graphs or rough working.
Do not use staples, paper clips, glue or correction fluid.
DO NOT WRITE IN ANY BARCODES.

Answer all questions.


No marks will be awarded for using brand names of software packages or hardware.

At the end of the examination, fasten all your work securely together.
The number of marks is given in brackets [ ] at the end of each question or part question.

The maximum number of marks is 75.

This document consists of 19 printed pages and 1 blank page.

DC (NH/CGW) 95372/4
© UCLES 2015 [Turn over
2

Throughout the paper you will be asked to write either pseudocode or program code.

Complete the statement to indicate which high-level programming language you will use.

Programming language ...........................................................................................................................

© UCLES 2015 9608/21/O/N/15


3

1 Computer programs have to evaluate expressions.

Study the sequence of pseudocode statements.

Write down the value assigned to each variable.

DECLARE h, w, r, Perimeter, Area : REAL


DECLARE A, B, C, D, E : BOOLEAN

h ← 13.6
w ← 6.4
Perimeter ← (h + w) * 2 (i) Perimeter …………………………………… [1]

r ← 10
Area 3.142 * r^2 (ii) Area ………………………………………………… [1]

Z ← 11 + r / 5 + 3 (iii) Z ………………………………………………………… [1]

A ← NOT(r > 10) (iv) A ………………………………………………………… [1]

2 A programmer uses an Integrated Development Environment (IDE) for all program development.

(i) Describe what is meant by an IDE.

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

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

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

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

(ii) Name three features you would expect to be available in an IDE to help initial error detection
or debugging.

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

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

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

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

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

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

© UCLES 2015 9608/21/O/N/15 [Turn over


4

3 A program is to simulate the operation of a particular type of logic gate.

• The gate has two inputs (TRUE or FALSE) which are entered by the user.
• The program will display the output (TRUE or FALSE) from the gate.

The program uses the following identifiers in the pseudocode below:

Identifier Data type Description


InA BOOLEAN Input signal
InB BOOLEAN Input signal
OutZ BOOLEAN Output signal

01 INPUT InA
02 INPUT InB
03 IF (InA = FALSE AND InB = FALSE) OR (InA = FALSE AND InB = TRUE)
OR (InA = TRUE AND InB = FALSE)
04 THEN
05 OutZ ← TRUE
06 ELSE
07 OutZ ← FALSE
08 ENDIF
09 OUTPUT OutZ

(a) The programmer chooses the following four test cases.

Show the output (OutZ) expected for each test case.

Input Output
Test case InA InB OutZ

1 TRUE TRUE

2 TRUE FALSE

3 FALSE TRUE

4 FALSE FALSE
[4]

© UCLES 2015 9608/21/O/N/15


5

(b) The selection statement (lines 03 – 08) could have been written with more simplified logic.

Rewrite this section of the algorithm in pseudocode.

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

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

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

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

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

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

© UCLES 2015 9608/21/O/N/15 [Turn over


6

4 A program is to be written to calculate the discount given on purchases.

A purchase may qualify for a discount depending on the amount spent. The purchase price
(Purchase), the discount rate (DiscountRate) and amount paid (Paid) is calculated as shown
in the following pseudocode algorithm.

INPUT Purchase

IF Purchase > 1000


THEN
DiscountRate 0.10 ←
ELSE
IF Purchase > 500
THEN
DiscountRate 0.05 ←
ELSE
DiscountRate 0 ←
ENDIF
ENDIF

Paid ←
Purchase * (1 - DiscountRate)
OUTPUT Paid

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

© UCLES 2015 9608/21/O/N/15


7

[6]

© UCLES 2015 9608/21/O/N/15 [Turn over


8

5 A driver buys a new car.

The value of the car reduces each year by a percentage of its current value.

The percentage reduction is:

• in the first year, 40%


• in each following year, 20%

The driver writes a program to predict the value of the car in future years.

The program requirements are:

• enter the cost of the new car (to nearest $)


• calculate and output the value of the car at the end of each year
• the program will end when either the car is nine years old, or when the value is less than
$1000

(a) Study the incomplete pseudocode which follows in part (b) and fill in the identifier table.

Identifier Data type Description

[3]

(b) Complete the pseudocode for this design.

OUTPUT "Enter purchase price"


INPUT PurchasePrice

CurrentValue ← ......................................................................................................
YearCount 1 ←
WHILE ....................................... AND .........................................................................

IF ............................................................................................................................
THEN
CurrentValue ← CurrentValue * (1 – 40 / 100)
ELSE
CurrentValue ← ......................................................................................
ENDIF

OUTPUT YearCount, CurrentValue

...................................................................................................................................
ENDWHILE

[6]

© UCLES 2015 9608/21/O/N/15


9

6 A firm employs five staff who take part in a training programme. Each member of staff must
complete a set of twelve tasks which can be taken in any order. When a member of staff
successfully completes a task, this is recorded.

A program is to be produced to record the completion of tasks for the five members of staff.

To test the code, the programmer makes the program generate test data.

The program generates pairs of random numbers:

• the first, in the range, 1 to 5 to represent the member of staff


• the second, in the range, 1 to 12 to represent the task

Each pair of numbers simulates the completion of one task by one member of staff.

(a) Explain why the generation of 60 (5 staff x 12 tasks) pairs of random numbers will not simulate
all tasks completed by all staff.

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

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

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

(b) Data is currently recorded manually as shown.

Staff Task number


number 1 2 3 4 5 6 7 8 9 10 11 12
1
2
3 
4
5 

The table shows that two members of staff have each successfully completed one task.

The program must use a suitable data structure to store, for all staff:

• tasks successfully completed


• tasks not yet successfully completed

The program will output the staff number and task number in the order in which tasks are
completed.

© UCLES 2015 9608/21/O/N/15 [Turn over


10

The program design in pseudocode is produced as follows:

01 DECLARE StaffNum : INTEGER


02 DECLARE TaskNum : INTEGER
03 DECLARE .....................................................................................................................................
04 DECLARE NewStaffTask : BOOLEAN
05
06 CALL InitialiseTaskGrid
07 Completed ← 0
08 WHILE Completed <> 60
09 NewStaffTask ← FALSE
10 WHILE NewStaffTask = FALSE
11 StaffNum ← RANDOM(1,5) //generates a random number
12 TaskNum ← RANDOM(1,12) //in the given range
13 IF TaskGrid[StaffNum, TaskNum] = FALSE
14 THEN
15 TaskGrid[StaffNum, TaskNum] ← TRUE
16 NewStaffTask ← TRUE
17 OUTPUT StaffNum, TaskNum
18 ENDIF
19 ENDWHILE
20 Completed ← Completed + 1
21 ENDWHILE
22 OUTPUT "Staff Task Count", Completed
23
24 // end of main program
25
26 PROCEDURE InitialiseTaskGrid()
27 DECLARE i : INTEGER
28 DECLARE j : INTEGER
29 FOR i ← 1 TO 5
30 FOR j ← 1 TO 12
31 TaskGrid[i, j] ← FALSE
32 ENDFOR
33 ENDFOR
34 ENDPROCEDURE

© UCLES 2015 9608/21/O/N/15


11

Study the pseudocode and answer the questions below.

Give the line number for:

(i) The declaration of a BOOLEAN global variable. .................... [1]

(ii) The declaration of a local variable. .................... [1]

(iii) The incrementing 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) (i) State the number of parameters of the InitialiseTaskGrid procedure.

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

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

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

(iii) Explain the purpose of lines 13 – 18.

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

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

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

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

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

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

(iv) Give the global variable that needs to be declared at line 03.

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

© UCLES 2015 9608/21/O/N/15 [Turn over


12

(d) Line 17 in the pseudocode outputs the staff number and the task number.
Staff number Staff name
A new requirement is to display the name of the
member of staff given in the table. 1 Sadiq
2 Smith
Write a CASE structure using variable StaffNum. 3 Ho
Assign to a new variable StaffName the appropriate 4 Azmah
staff name. 5 Papadopoulos

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

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

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

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

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

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

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

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

...............................................................................................................................................[4]

© UCLES 2015 9608/21/O/N/15


13

Question 7 begins on page 14.

© UCLES 2015 9608/21/O/N/15 [Turn over


14

7 ASCII character codes are used to represent a single character.

Part of the code table is shown below.

ASCII code table (part)


Character Decimal Character Decimal Character Decimal
<Space> 32 I 73 R 82
A 65 J 74 S 83
B 66 K 75 T 84
C 67 L 76 U 85
D 68 M 77 V 86
E 69 N 78 W 87
F 70 O 79 X 88
G 71 P 80 Y 89
H 72 Q 81 Z 90

Some pseudocode statements follow which use the built-in functions below:

ONECHAR(ThisString : STRING, Position : INTEGER) RETURNS CHAR


returns the single character at position Position (counting from the start of the string with value 1)
from the string ThisString.
For example: ONECHAR("Barcelona", 3) returns 'r'.

CHARACTERCOUNT(ThisString : STRING) RETURNS INTEGER


returns the number of characters in the string ThisString.
For example: CHARACTERCOUNT("BRAZIL") returns 6.

CHR(ThisInteger : INTEGER) RETURNS CHAR


returns the character with ASCII code ThisInteger.
For example: CHR(65) returns character 'A'.

ASC(ThisCharacter : CHAR) RETURNS INTEGER


returns the ASCII value for character ThisCharacter.
For example: ASC('A') returns 65.

(a) Show the values stored by variables A, B, C and D.

The & operator is used to concatenate two strings.

Num1 ← 15
A ← CHR(67) & CHR(65) & CHR(84) (i) A ..................................................... [1]
B ← ASC('P') - ASC('F') + 3 (ii) B ..................................................... [1]
C ← ASC(ONECHAR("BISCUITS", 3)) (iii) C ..................................................... [1]
D ← CHARACTERCOUNT("New York City") + 2 (iv) D ..................................................... [1]

© UCLES 2015 9608/21/O/N/15


15

(b) A program is to be written which accepts a string and then calculates a numeric value from
this string. The input string and the calculated value are then to be sent to a remote computer
over a communications link.

Study the following pseudocode:

OUTPUT "Enter string"


INPUT MyString
StringTotal 0 ←
FOR i ←
1 TO CHARACTERCOUNT(MyString)
NextNum ←
ASC(ONECHAR(MyString, i))
StringTotal ←
StringTotal + NextNum
ENDFOR

OUTPUT MyString, StringTotal

Write the above pseudocode algorithm as program code.

There is no need to show the declaration of variables or comment statements.

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

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

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

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

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

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

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

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

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

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

...............................................................................................................................................[6]

(c) Explain the purpose of sending the value of StringTotal to the remote computer, in
addition to MyString.

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

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

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

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

© UCLES 2015 9608/21/O/N/15 [Turn over


16

8 In this question you will need to use the given pseudocode built-in function:

ONECHAR(ThisString : STRING, Position : INTEGER) RETURNS CHAR


returns the single character at position Position (counting from the start of the string with value 1)
from the string ThisString.
For example: ONECHAR("Barcelona", 3) returns 'r'.

(a) Give the value assigned to variable y by the following statement:

y ← ONECHAR("San Francisco", 6) y ............................................. [1]

A program reads a string entered by the user. The string represents the addition or subtraction
of two fractions. Each part of the fraction within the string is always a single digit only and the
top digit is always less than the bottom digit.

Example strings are: "3/8+3/5" and "5/8-1/4"

The program steps are:


• the user enters the string
• the program isolates each digit and the operator
• the program computes the answer as either:
a fraction
a whole number followed by a fraction
a whole number
• the program displays the answer to the user

Op

é

N4Char
N3Char
N2Char
N1Char

The identifier table shows the variables to be used to store the characters in the string as
shown in the diagram.

Identifier Data type Description

FractionString String input by user.


STRING
For example: "5/8-1/4"
N1Char CHAR See diagram
N2Char CHAR See diagram
N3Char CHAR See diagram
N4Char CHAR See diagram
Op CHAR See diagram

© UCLES 2015 9608/21/O/N/15


17

(b) Study the sequence of pseudocode statements.

Show the values assigned to each variable.

FractionString ← "3/7+2/9"
N3Char ← ONECHAR(FractionString, 5) (i) N3Char .................................... [1]
Op ← ONECHAR(FractionString, 4) (ii) Op .............................................. [1]

(iii) Complete the function call to isolate the character '9' from FractionString.

FractionString ← "3/7+2/9"

ONECHAR(FractionString, ............... ) [1]

The following additional variables are to be used by the program:

Identifier Data type Description


N1 INTEGER The number value of N1Char
N2 INTEGER The number value of N2Char
N3 INTEGER The number value of N3Char
N4 INTEGER The number value of N4Char
TopAnswer INTEGER The numerator of the fraction answer
BottomAnswer INTEGER The denominator of the fraction answer

© UCLES 2015 9608/21/O/N/15 [Turn over


18

(c) The following pseudocode uses these additional built-in functions:

TONUM(ThisDigit : CHAR) RETURNS INTEGER


returns the integer value of character ThisDigit
For example: TONUM('8') returns digit 8.

TOSTR(ThisNumber : INTEGER) RETURNS STRING


returns the string value of integer ThisNumber
For example: TOSTR(27) returns "27".

Study the pseudocode.

Complete the three dry runs for the three given values of FractionString.

OUTPUT "Enter the expression"


INPUT FractionString

// isolate each number digit and assign its number value



N1Char ONECHAR(FractionString, 1)
N1 ← TONUM(N1Char)

N2Char ONECHAR(FractionString, 3)
N2 ← TONUM(N2Char)

N3Char ONECHAR(FractionString, 5)
N3 ← TONUM(N3Char)

N4Char ONECHAR(FractionString, 7)
N4 ← TONUM(N4Char)

BottomAnswer ← N2 * N4

Op ← ONECHAR(FractionString, 4)
IF Op = '+'
THEN
// add fractions
TopAnswer ←
(BottomAnswer / N2) * N1 + (BottomAnswer / N4) * N3
ELSE
// subtract fractions
TopAnswer ←
(BottomAnswer / N2) * N1 - (BottomAnswer / N4) * N3
ENDIF

IF TopAnswer = BottomAnswer
THEN
OUTPUT '1'
ELSE
IF TopAnswer > BottomAnswer
THEN
TopAnswer ←
TopAnswer MOD BottomAnswer
// the & operator joins strings or character values
OUTPUT "1 " & TOSTR(TopAnswer) & "/" & TOSTR(BottomAnswer)
ELSE
OUTPUT TOSTR(TopAnswer) & "/" & TOSTR(BottomAnswer)
ENDIF
ENDIF

© UCLES 2015 9608/21/O/N/15


19

(i) FractionString ← "2/5-3/8"

N1 N2 N3 N4 BottomAnswer Op TopAnswer OUTPUT

[2]

(ii) FractionString ← "3/4+1/4"

N1 N2 N3 N4 BottomAnswer Op TopAnswer OUTPUT

[2]

(iii) FractionString ← "7/9+2/3"

N1 N2 N3 N4 BottomAnswer Op TopAnswer OUTPUT

[3]

(d) The programmer writes code from the given pseudocode design. The program works, but the
design is limited.

The programmer is to make amendments to the design following suggested specification


changes.

(i) State the name for this type of maintenance.

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

(ii) Describe three specification changes which will make the program more useful.

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

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

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

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

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

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

© UCLES 2015 9608/21/O/N/15


20

BLANK PAGE

Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every reasonable
effort has been made by the publisher (UCLES) to trace copyright holders, but if any items requiring clearance have unwittingly been included, the publisher will
be pleased to make amends at the earliest possible opportunity.

To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge International
Examinations Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download at www.cie.org.uk after
the live examination series.

Cambridge International Examinations is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of University of Cambridge Local
Examinations Syndicate (UCLES), which is itself a department of the University of Cambridge.

© UCLES 2015 9608/21/O/N/15


Cambridge International Examinations
Cambridge International Advanced Subsidiary and Advanced Level
* 4 6 3 0 1 0 8 0 0 4 *

COMPUTER SCIENCE 9608/22


Paper 2 Fundamental Problem-solving and Programming Skills October/November 2015
2 hours
Candidates answer on the Question Paper.
No Additional Materials are required.
No calculators allowed.

READ THESE INSTRUCTIONS FIRST

Write your Centre number, candidate number and name in the spaces at the top of this page.
Write in dark blue or black pen.
You may use an HB pencil for any diagrams, graphs or rough working.
Do not use staples, paper clips, glue or correction fluid.
DO NOT WRITE IN ANY BARCODES.

Answer all questions.


No marks will be awarded for using brand names of software packages or hardware.

At the end of the examination, fasten all your work securely together.
The number of marks is given in brackets [ ] at the end of each question or part question.

The maximum number of marks is 75.

This document consists of 16 printed pages.

DC (NH/CGW) 95376/4
© UCLES 2015 [Turn over
2

Throughout the paper you will be asked to write either pseudocode or program code.

Complete the statement to indicate which high-level programming language you will use.

Programming language ...........................................................................................................................

1 Computer programs have to evaluate expressions.

Study the sequence of pseudocode statements.

Give the value assigned to each variable.

The statement may generate an error. If so, write ERROR.

The & operator is used to concatenate strings.

DECLARE N1 : INTEGER
DECLARE N2 : INTEGER
DECLARE Answer : REAL
DECLARE Found : BOOLEAN
DECLARE IsValid : BOOLEAN
N1 ← 3
N2 ← 9
Answer ← (N1 + N2) / 6
Answer ← 3 * (N1 – 2) + N2 / 2
IsValid ← (N1 > N2) AND (N2 = 9)
Found ← FALSE

IsValid ← (N1 > N2 / 2) OR (Found = FALSE)


Answer ← "1034" & " + " & "65"

(i) Answer .............................................................................................................................. [1]

(ii) Answer .............................................................................................................................. [1]

(iii) IsValid ........................................................................................................................... [1]

(iv) IsValid ........................................................................................................................... [1]

(v) Answer .............................................................................................................................. [1]

To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge International
Examinations Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download at www.cie.org.uk after
the live examination series.
© UCLES 2015 9608/22/O/N/15
3

2 A program is to simulate the operation of a particular type of logic gate.

• The gate has two inputs (0 or 1) which are entered by the user.
• The program will display the output (0 or 1) from the gate.

The program uses the following identifiers in the pseudocode below:

Identifier Data type Description


P INTEGER Input signal
Q INTEGER Input signal
X INTEGER Output signal

01 INPUT P
02 INPUT Q
03 IF (P = 1 AND Q = 0) OR (P = 0 AND Q = 1) OR (P = 0 AND Q = 0)
04 THEN
05 X 0 ←
06 ELSE
07 X 1 ←
08 ENDIF
09 OUTPUT X

(a) The programmer chooses the following four test cases.

Show the output (X) for each test case.

Input Output
Test case P Q X

1 1 1

2 1 0

3 0 1

4 0 0
[4]

(b) The selection statement (lines 03 – 08) could have been written with more simplified logic.

Rewrite this section of the algorithm in pseudocode.

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

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

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

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

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

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

© UCLES 2015 9608/22/O/N/15 [Turn over


4

3 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

© UCLES 2015 9608/22/O/N/15


5

CALL
CalculatePoints(AmountDue)

[6]
© UCLES 2015 9608/22/O/N/15 [Turn over
6

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

© UCLES 2015 9608/22/O/N/15


7

Question 4(b) continues on page 8.

© UCLES 2015 9608/22/O/N/15 [Turn over


8

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

© UCLES 2015 9608/22/O/N/15


9

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]

© UCLES 2015 9608/22/O/N/15 [Turn over


10

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

© UCLES 2015 9608/22/O/N/15


11

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

© UCLES 2015 9608/22/O/N/15 [Turn over


12

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

© UCLES 2015 9608/22/O/N/15


13

6 Some pseudocode statements follow which use the following built-in functions:

ONECHAR(ThisString : STRING, Position : INTEGER) RETURNS CHAR


returns the single character at position Position (counting from the start of the string with value 1)
from the string ThisString.
For example: ONECHAR("Barcelona", 3) returns 'r'.

CHARACTERCOUNT(ThisString : STRING) RETURNS INTEGER


returns the number of characters in the string ThisString.
For example: CHARACTERCOUNT("South Africa") returns 12.

(a) Study the following pseudocode statements.

Give the values assigned to variables x and y.

(i) x ← CHARACTERCOUNT("New Delhi") + 3 x .......................................... [1]

(ii) y ← ONECHAR("Sri Lanka", 5) y .......................................... [1]

(b) A program is to be written as follows:


• the user enters a string
• the program will form a new string with all <Space> characters removed
• the new string is output

NewString ""←
INPUT InputString

j ← CHARACTERCOUNT(InputString)
FOR i ←
1 TO j
NextChar ←
ONECHAR(InputString, i)
IF NextChar <> " "
THEN
// the & character joins together two strings
NewString ←
NewString & NextChar
ENDIF
ENDFOR

OUTPUT NewString

(i) Complete the identifier table below.

Identifier Data type Description

InputString STRING The string value input by the user

[4]
© UCLES 2015 9608/22/O/N/15 [Turn over
14

(ii) An experienced programmer suggests this pseudocode would be best designed as a


function.

Complete the re-design of the pseudocode as follows:

The main program:


• the user enters MyString
• the function is called and the changed string is assigned to variable ChangedString

The function:
• has identifier RemoveSpaces
• has a single parameter
• will include the declaration for any local variables used by the function

// main program
INPUT MyString
ChangedString ← RemoveSpaces(.........................................................................................)
OUTPUT ChangedString

// function definition

FUNCTION RemoveSpaces(................................................................) RETURNS ..................

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

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

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

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

j ← CHARACTERCOUNT(InputString)
FOR i ← 1 TO j
NextChar ← ONECHAR(InputString, i)
IF NextChar <> " "
THEN
// the & character joins together two strings
NewString ← NewString & NextChar
ENDIF
ENDFOR

.................................................................................................................................................
ENDFUNCTION
[7]

© UCLES 2015 9608/22/O/N/15


15

7 ASCII character codes are used to represent a single character.

Part of the code table is shown below.

ASCII code table (part)


Character Decimal Character Decimal Character Decimal
<Space> 32 I 73 R 82
A 65 J 74 S 83
B 66 K 75 T 84
C 67 L 76 U 85
D 68 M 77 V 86
E 69 N 78 W 87
F 70 O 79 X 88
G 71 P 80 Y 89
H 72 Q 81 Z 90

Some pseudocode statements follow which use these built-in functions:

CHARACTERCOUNT(ThisString : STRING) RETURNS INTEGER


returns the number of characters in the string ThisString.
For example: CHARACTERCOUNT("South Africa") returns 12.

CHR(ThisInteger : INTEGER) RETURNS CHAR


returns the character with ASCII value ThisInteger.
For example: CHR(66) returns 'B'.

ASC(ThisCharacter : CHAR) RETURNS INTEGER


returns the ASCII value for character ThisCharacter.
For example: ASC('B') returns 66.

(a) Give the values assigned to the variables A, B, C and D.

The & operator is used to concatenate two strings.

The expression could generate an error; if so, write ERROR.

Num1 ← 5
A ← ASC('F') + Num1 + ASC('Z') (i) A ..................................................... [1]
B ← CHR(89) & CHR(69) & CHR(83) (ii) B ..................................................... [1]
C ← CHARACTERCOUNT(B & "PLEASE") (iii) C ..................................................... [1]
D ← ASC(ONECHAR("CURRY SAUCE", 7)) (iv) D ..................................................... [1]

© UCLES 2015 9608/22/O/N/15 [Turn over


16

(b) A program is to be written to input a message string and then encrypt the message.

Study the following pseudocode:

OUTPUT "Enter message"


INPUT MyMessage
EncryptString "" ←
FOR i ←
1 TO CHARACTERCOUNT(MyMessage)
NextNum ←
ASC(ONECHAR(MyMessage, i) + 3)
EncryptString ←
EncryptString & CHR(NextNum)
ENDFOR

OUTPUT EncryptString

(i) Write the above pseudocode algorithm as program code.

Visual Basic and Pascal: You should include the declaration statements for variables.
Python: You should show a comment statement for each variable used with its data type.

Programming language .....................................................................................................

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

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

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

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

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

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

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

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

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

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

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

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

.......................................................................................................................................[8]

(ii) Describe the encryption algorithm used to encrypt the message string entered by the
user.

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

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

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

.......................................................................................................................................[2]
© UCLES 2015 9608/22/O/N/15
Cambridge International Examinations
Cambridge International Advanced Subsidiary and Advanced Level
* 4 5 3 1 9 9 0 1 8 1 *

COMPUTER SCIENCE 9608/23


Paper 2 Fundamental Problem-solving and Programming Skills October/November 2015
2 hours
Candidates answer on the Question Paper.
No Additional Materials are required.
No calculators allowed.

READ THESE INSTRUCTIONS FIRST

Write your Centre number, candidate number and name in the spaces at the top of this page.
Write in dark blue or black pen.
You may use an HB pencil for any diagrams, graphs or rough working.
Do not use staples, paper clips, glue or correction fluid.
DO NOT WRITE IN ANY BARCODES.

Answer all questions.


No marks will be awarded for using brand names of software packages or hardware.

At the end of the examination, fasten all your work securely together.
The number of marks is given in brackets [ ] at the end of each question or part question.

The maximum number of marks is 75.

This document consists of 19 printed pages and 1 blank page.

DC (GB) 116769
© UCLES 2015 [Turn over
2

Throughout the paper you will be asked to write either pseudocode or program code.

Complete the statement to indicate which high-level programming language you will use.

Programming language ...........................................................................................................................

© UCLES 2015 9608/23/O/N/15


3

1 Computer programs have to evaluate expressions.

Study the sequence of pseudocode statements.

Write down the value assigned to each variable.

DECLARE h, w, r, Perimeter, Area : REAL


DECLARE A, B, C, D, E : BOOLEAN

h ← 13.6
w ← 6.4
Perimeter ← (h + w) * 2 (i) Perimeter …………………………………… [1]

r ← 10
Area 3.142 * r^2 (ii) Area ………………………………………………… [1]

Z ← 11 + r / 5 + 3 (iii) Z ………………………………………………………… [1]

A ← NOT(r > 10) (iv) A ………………………………………………………… [1]

2 A programmer uses an Integrated Development Environment (IDE) for all program development.

(i) Describe what is meant by an IDE.

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

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

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

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

(ii) Name three features you would expect to be available in an IDE to help initial error detection
or debugging.

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

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

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

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

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

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

© UCLES 2015 9608/23/O/N/15 [Turn over


4

3 A program is to simulate the operation of a particular type of logic gate.

• The gate has two inputs (TRUE or FALSE) which are entered by the user.
• The program will display the output (TRUE or FALSE) from the gate.

The program uses the following identifiers in the pseudocode below:

Identifier Data type Description


InA BOOLEAN Input signal
InB BOOLEAN Input signal
OutZ BOOLEAN Output signal

01 INPUT InA
02 INPUT InB
03 IF (InA = FALSE AND InB = FALSE) OR (InA = FALSE AND InB = TRUE)
OR (InA = TRUE AND InB = FALSE)
04 THEN
05 OutZ ← TRUE
06 ELSE
07 OutZ ← FALSE
08 ENDIF
09 OUTPUT OutZ

(a) The programmer chooses the following four test cases.

Show the output (OutZ) expected for each test case.

Input Output
Test case InA InB OutZ

1 TRUE TRUE

2 TRUE FALSE

3 FALSE TRUE

4 FALSE FALSE
[4]

© UCLES 2015 9608/23/O/N/15


5

(b) The selection statement (lines 03 – 08) could have been written with more simplified logic.

Rewrite this section of the algorithm in pseudocode.

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

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

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

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

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

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

© UCLES 2015 9608/23/O/N/15 [Turn over


6

4 A program is to be written to calculate the discount given on purchases.

A purchase may qualify for a discount depending on the amount spent. The purchase price
(Purchase), the discount rate (DiscountRate) and amount paid (Paid) is calculated as shown
in the following pseudocode algorithm.

INPUT Purchase

IF Purchase > 1000


THEN
DiscountRate 0.10 ←
ELSE
IF Purchase > 500
THEN
DiscountRate 0.05 ←
ELSE
DiscountRate 0 ←
ENDIF
ENDIF

Paid ←
Purchase * (1 - DiscountRate)
OUTPUT Paid

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

© UCLES 2015 9608/23/O/N/15


7

[6]

© UCLES 2015 9608/23/O/N/15 [Turn over


8

5 A driver buys a new car.

The value of the car reduces each year by a percentage of its current value.

The percentage reduction is:

• in the first year, 40%


• in each following year, 20%

The driver writes a program to predict the value of the car in future years.

The program requirements are:

• enter the cost of the new car (to nearest $)


• calculate and output the value of the car at the end of each year
• the program will end when either the car is nine years old, or when the value is less than
$1000

(a) Study the incomplete pseudocode which follows in part (b) and fill in the identifier table.

Identifier Data type Description

[3]

(b) Complete the pseudocode for this design.

OUTPUT "Enter purchase price"


INPUT PurchasePrice

CurrentValue ← ......................................................................................................
YearCount 1 ←
WHILE ....................................... AND .........................................................................

IF ............................................................................................................................
THEN
CurrentValue ← CurrentValue * (1 – 40 / 100)
ELSE
CurrentValue ← ......................................................................................
ENDIF

OUTPUT YearCount, CurrentValue

...................................................................................................................................
ENDWHILE

[6]

© UCLES 2015 9608/23/O/N/15


9

6 A firm employs five staff who take part in a training programme. Each member of staff must
complete a set of twelve tasks which can be taken in any order. When a member of staff
successfully completes a task, this is recorded.

A program is to be produced to record the completion of tasks for the five members of staff.

To test the code, the programmer makes the program generate test data.

The program generates pairs of random numbers:

• the first, in the range, 1 to 5 to represent the member of staff


• the second, in the range, 1 to 12 to represent the task

Each pair of numbers simulates the completion of one task by one member of staff.

(a) Explain why the generation of 60 (5 staff x 12 tasks) pairs of random numbers will not simulate
all tasks completed by all staff.

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

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

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

(b) Data is currently recorded manually as shown.

Staff Task number


number 1 2 3 4 5 6 7 8 9 10 11 12
1
2
3 
4
5 

The table shows that two members of staff have each successfully completed one task.

The program must use a suitable data structure to store, for all staff:

• tasks successfully completed


• tasks not yet successfully completed

The program will output the staff number and task number in the order in which tasks are
completed.

© UCLES 2015 9608/23/O/N/15 [Turn over


10

The program design in pseudocode is produced as follows:

01 DECLARE StaffNum : INTEGER


02 DECLARE TaskNum : INTEGER
03 DECLARE .....................................................................................................................................
04 DECLARE NewStaffTask : BOOLEAN
05
06 CALL InitialiseTaskGrid
07 Completed ← 0
08 WHILE Completed <> 60
09 NewStaffTask ← FALSE
10 WHILE NewStaffTask = FALSE
11 StaffNum ← RANDOM(1,5) //generates a random number
12 TaskNum ← RANDOM(1,12) //in the given range
13 IF TaskGrid[StaffNum, TaskNum] = FALSE
14 THEN
15 TaskGrid[StaffNum, TaskNum] ← TRUE
16 NewStaffTask ← TRUE
17 OUTPUT StaffNum, TaskNum
18 ENDIF
19 ENDWHILE
20 Completed ← Completed + 1
21 ENDWHILE
22 OUTPUT "Staff Task Count", Completed
23
24 // end of main program
25
26 PROCEDURE InitialiseTaskGrid()
27 DECLARE i : INTEGER
28 DECLARE j : INTEGER
29 FOR i ← 1 TO 5
30 FOR j ← 1 TO 12
31 TaskGrid[i, j] ← FALSE
32 ENDFOR
33 ENDFOR
34 ENDPROCEDURE

© UCLES 2015 9608/23/O/N/15


11

Study the pseudocode and answer the questions below.

Give the line number for:

(i) The declaration of a BOOLEAN global variable. .................... [1]

(ii) The declaration of a local variable. .................... [1]

(iii) The incrementing 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) (i) State the number of parameters of the InitialiseTaskGrid procedure.

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

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

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

(iii) Explain the purpose of lines 13 – 18.

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

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

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

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

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

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

(iv) Give the global variable that needs to be declared at line 03.

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

© UCLES 2015 9608/23/O/N/15 [Turn over


12

(d) Line 17 in the pseudocode outputs the staff number and the task number.
Staff number Staff name
A new requirement is to display the name of the
member of staff given in the table. 1 Sadiq
2 Smith
Write a CASE structure using variable StaffNum. 3 Ho
Assign to a new variable StaffName the appropriate 4 Azmah
staff name. 5 Papadopoulos

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

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

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

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

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

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

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

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

...............................................................................................................................................[4]

© UCLES 2015 9608/23/O/N/15


13

Question 7 begins on page 14.

© UCLES 2015 9608/23/O/N/15 [Turn over


14

7 ASCII character codes are used to represent a single character.

Part of the code table is shown below.

ASCII code table (part)


Character Decimal Character Decimal Character Decimal
<Space> 32 I 73 R 82
A 65 J 74 S 83
B 66 K 75 T 84
C 67 L 76 U 85
D 68 M 77 V 86
E 69 N 78 W 87
F 70 O 79 X 88
G 71 P 80 Y 89
H 72 Q 81 Z 90

Some pseudocode statements follow which use the built-in functions below:

ONECHAR(ThisString : STRING, Position : INTEGER) RETURNS CHAR


returns the single character at position Position (counting from the start of the string with value 1)
from the string ThisString.
For example: ONECHAR("Barcelona", 3) returns 'r'.

CHARACTERCOUNT(ThisString : STRING) RETURNS INTEGER


returns the number of characters in the string ThisString.
For example: CHARACTERCOUNT("BRAZIL") returns 6.

CHR(ThisInteger : INTEGER) RETURNS CHAR


returns the character with ASCII code ThisInteger.
For example: CHR(65) returns character 'A'.

ASC(ThisCharacter : CHAR) RETURNS INTEGER


returns the ASCII value for character ThisCharacter.
For example: ASC('A') returns 65.

(a) Show the values stored by variables A, B, C and D.

The & operator is used to concatenate two strings.

Num1 ← 15
A ← CHR(67) & CHR(65) & CHR(84) (i) A ..................................................... [1]
B ← ASC('P') - ASC('F') + 3 (ii) B ..................................................... [1]
C ← ASC(ONECHAR("BISCUITS", 3)) (iii) C ..................................................... [1]
D ← CHARACTERCOUNT("New York City") + 2 (iv) D ..................................................... [1]

© UCLES 2015 9608/23/O/N/15


15

(b) A program is to be written which accepts a string and then calculates a numeric value from
this string. The input string and the calculated value are then to be sent to a remote computer
over a communications link.

Study the following pseudocode:

OUTPUT "Enter string"


INPUT MyString
StringTotal 0 ←
FOR i ←
1 TO CHARACTERCOUNT(MyString)
NextNum ←
ASC(ONECHAR(MyString, i))
StringTotal ←
StringTotal + NextNum
ENDFOR

OUTPUT MyString, StringTotal

Write the above pseudocode algorithm as program code.

There is no need to show the declaration of variables or comment statements.

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

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

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

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

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

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

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

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

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

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

...............................................................................................................................................[6]

(c) Explain the purpose of sending the value of StringTotal to the remote computer, in
addition to MyString.

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

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

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

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

© UCLES 2015 9608/23/O/N/15 [Turn over


16

8 In this question you will need to use the given pseudocode built-in function:

ONECHAR(ThisString : STRING, Position : INTEGER) RETURNS CHAR


returns the single character at position Position (counting from the start of the string with value 1)
from the string ThisString.
For example: ONECHAR("Barcelona", 3) returns 'r'.

(a) Give the value assigned to variable y by the following statement:

y ← ONECHAR("San Francisco", 6) y ............................................. [1]

A program reads a string entered by the user. The string represents the addition or subtraction
of two fractions. Each part of the fraction within the string is always a single digit only and the
top digit is always less than the bottom digit.

Example strings are: "3/8+3/5" and "5/8-1/4"

The program steps are:


• the user enters the string
• the program isolates each digit and the operator
• the program computes the answer as either:
a fraction
a whole number followed by a fraction
a whole number
• the program displays the answer to the user

Op

é

N4Char
N3Char
N2Char
N1Char

The identifier table shows the variables to be used to store the characters in the string as
shown in the diagram.

Identifier Data type Description

FractionString String input by user.


STRING
For example: "5/8-1/4"
N1Char CHAR See diagram
N2Char CHAR See diagram
N3Char CHAR See diagram
N4Char CHAR See diagram
Op CHAR See diagram

© UCLES 2015 9608/23/O/N/15


17

(b) Study the sequence of pseudocode statements.

Show the values assigned to each variable.

FractionString ← "3/7+2/9"
N3Char ← ONECHAR(FractionString, 5) (i) N3Char .................................... [1]
Op ← ONECHAR(FractionString, 4) (ii) Op .............................................. [1]

(iii) Complete the function call to isolate the character '9' from FractionString.

FractionString ← "3/7+2/9"

ONECHAR(FractionString, ............... ) [1]

The following additional variables are to be used by the program:

Identifier Data type Description


N1 INTEGER The number value of N1Char
N2 INTEGER The number value of N2Char
N3 INTEGER The number value of N3Char
N4 INTEGER The number value of N4Char
TopAnswer INTEGER The numerator of the fraction answer
BottomAnswer INTEGER The denominator of the fraction answer

© UCLES 2015 9608/23/O/N/15 [Turn over


18

(c) The following pseudocode uses these additional built-in functions:

TONUM(ThisDigit : CHAR) RETURNS INTEGER


returns the integer value of character ThisDigit
For example: TONUM('8') returns digit 8.

TOSTR(ThisNumber : INTEGER) RETURNS STRING


returns the string value of integer ThisNumber
For example: TOSTR(27) returns "27".

Study the pseudocode.

Complete the three dry runs for the three given values of FractionString.

OUTPUT "Enter the expression"


INPUT FractionString

// isolate each number digit and assign its number value



N1Char ONECHAR(FractionString, 1)
N1 ← TONUM(N1Char)

N2Char ONECHAR(FractionString, 3)
N2 ← TONUM(N2Char)

N3Char ONECHAR(FractionString, 5)
N3 ← TONUM(N3Char)

N4Char ONECHAR(FractionString, 7)
N4 ← TONUM(N4Char)

BottomAnswer ← N2 * N4

Op ← ONECHAR(FractionString, 4)
IF Op = '+'
THEN
// add fractions
TopAnswer ←
(BottomAnswer / N2) * N1 + (BottomAnswer / N4) * N3
ELSE
// subtract fractions
TopAnswer ←
(BottomAnswer / N2) * N1 - (BottomAnswer / N4) * N3
ENDIF

IF TopAnswer = BottomAnswer
THEN
OUTPUT '1'
ELSE
IF TopAnswer > BottomAnswer
THEN
TopAnswer ←
TopAnswer MOD BottomAnswer
// the & operator joins strings or character values
OUTPUT "1 " & TOSTR(TopAnswer) & "/" & TOSTR(BottomAnswer)
ELSE
OUTPUT TOSTR(TopAnswer) & "/" & TOSTR(BottomAnswer)
ENDIF
ENDIF

© UCLES 2015 9608/23/O/N/15


19

(i) FractionString ← "2/5-3/8"

N1 N2 N3 N4 BottomAnswer Op TopAnswer OUTPUT

[2]

(ii) FractionString ← "3/4+1/4"

N1 N2 N3 N4 BottomAnswer Op TopAnswer OUTPUT

[2]

(iii) FractionString ← "7/9+2/3"

N1 N2 N3 N4 BottomAnswer Op TopAnswer OUTPUT

[3]

(d) The programmer writes code from the given pseudocode design. The program works, but the
design is limited.

The programmer is to make amendments to the design following suggested specification


changes.

(i) State the name for this type of maintenance.

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

(ii) Describe three specification changes which will make the program more useful.

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

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

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

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

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

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

© UCLES 2015 9608/23/O/N/15


20

BLANK PAGE

Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every reasonable
effort has been made by the publisher (UCLES) to trace copyright holders, but if any items requiring clearance have unwittingly been included, the publisher will
be pleased to make amends at the earliest possible opportunity.

To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge International
Examinations Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download at www.cie.org.uk after
the live examination series.

Cambridge International Examinations is part of the Cambridge Assessment Group. Cambridge Assessment is the brand name of University of Cambridge Local
Examinations Syndicate (UCLES), which is itself a department of the University of Cambridge.

© UCLES 2015 9608/23/O/N/15

You might also like