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

p2d

The document contains a series of algorithm and programming questions related to string pattern analysis, sensor applications in smart homes, and stack operations. It includes tasks such as dry running algorithms, modifying code for efficiency, and writing pseudocode for specific operations. Additionally, it discusses debugging techniques and SQL commands relevant to database management.

Uploaded by

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

p2d

The document contains a series of algorithm and programming questions related to string pattern analysis, sensor applications in smart homes, and stack operations. It includes tasks such as dry running algorithms, modifying code for efficiency, and writing pseudocode for specific operations. Additionally, it discusses debugging techniques and SQL commands relevant to database management.

Uploaded by

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

Section C Algorithm and Programming

[DSE 2014 ICT P2D Q.4(a)]


10. Mr Chan works on string pattern analysis. Consider the following algorithm for a given string ST.

Line
1 check  TRUE
2 n  length of ST
3 for i from 1 to n do
4 if i-th character of ST ≠ (n-i+1)-th character of ST
5 check  FALSE
6 return check

(a) Dry run the algorithm with the following different string values of ST and give the corresponding
return values.

(i) ACGT (1 mark)

(ii) GACTTCAG (1 mark)

(iii) ACGCA (1 mark)

(b) What is the purpose of this algorithm? (1 mark)

(c) Modify line 3 in the algorithm to improve its efficiency. (1 mark)

[New question]
11. Some sensors are used in a smart home.

(a) The light in a room is turned on automatically when someone enters the room. What sensor should be
used? (1 mark)

(b) There is a brightness adjustment system in the room. State what sensor should be used and describe how
the sensor helps the system adjust the brightness of the room. (3 marks)

SP-DSE-ICT 2–1 1
[Modified from DSE 2015 ICT P2D Q.1(a), (b)]
12. Func(a, b) is a function with two positive integer inputs a and b, where a ≥ b. It returns the integral
part of (a÷b). For example,

Func(6, 2) returns 3 and Func(7, 3) returns 2.

(a) What will Func(14, 3) return? (1 mark)

The following algorithm ALG1 processes a Boolean array B with indices from 1 to n.

ALG1
Step 1: for k from 1 to n do Step 2
Step 2: B[k]  True
Step 3: B[1]  False
Step 4: for i from 1 to n do Steps 5 to 7
Step 5: if B[i] = True then do Steps 6 to 7
Step 6: for j from 2 to Func(n, i)
Step 7: B[i × j]  False

(b) Suppose that n = 6. Dry run ALG1. Use ‘F’ and ‘T’ to denote ‘False’ and ‘True’ respectively.

(i) What are the values in B after the first pass of the loop in Step 4? (2 marks)

(ii) What are the final values in B? (2 marks)


[DSE 2019 ICT P2D Q.1]
13. Peter uses stacks to manage boxes. Each box stores some apples. In the following example, a stack contains
3 boxes with 10, 20 and 30 apples.

30
20
10  Bottom of the stack

Below are the operations on the stacks:

Operation Description
Push(S, k) Pushes a box with k apples into stack S.
Pop(S) Removes a box from stack S and returns the number of apples in the box.
Empty(S) Returns TRUE if stack S has no boxes in it; otherwise, returns FALSE.

(a) (i) Initially there is an empty stack A. Write down the final content of A after executing the following
pseudocode. (2 marks)

Push(A, 10)
Push(A, 20)
TMP  Pop(A)
if Empty(A) then Push(A, 30)

(ii) Initially there is an empty stack B. Write down the final content of B after executing the following
pseudocode. (2 marks)

Push(B, 10)
Push(B, 20)
Push(B, 30)
Push(B, Pop(B)+Pop(B))

SP-DSE-ICT 2–3 3 Go on to the next page


(b) Initially there is a non-empty stack A and an empty stack B, as shown below:

30
20
15
5  Bottom of A  Bottom of B

Write down the final content of A and B after executing the following pseudocode. (3 marks)

TMP  0
while not Empty(A) do
TMP  TMP + Pop(A)
if TMP > 30 then
Push(B, 30)
TMP  TMP - 30
Push(B, TMP)

(c) Initially there is a non-empty stack X and an empty stack Y. REV(X,Y) is a subprogram for moving
all the boxes in stack X to stack Y, where the boxes in Y are in reverse order. An example is shown
below:

40 10
30 20
20 30
10  Bottom of X  Bottom of Y  Bottom of X 40  Bottom of Y

Initial content Final content

Write the pseudocode for REV(X,Y). (3 marks)


(d) Initially there is a non-empty stack A and an empty stack B.

40
. . . .
. . . .
. . . .
13
99
. . 40
. .
Bottom . .
. .
N boxes 12 . .
. .
3  Bottom  Bottom 13  Bottom  Bottom
of A of B of A of B

Initial content Final content

It is found that the apples in the bottom N boxes in A are rotten. Write the pseudocode for removing
the bottom N boxes and keeping the remaining boxes in the original order in A with the use of
REV(X,Y). (4 marks)

(e) When implementing REV, Peter uses break points for debugging. Describe how break points can help
Peter write a program. (2 marks)

END OF SECTION C

END OF PAPER

SP-DSE-ICT 2–5 5
Database (SQL commands - based on SQL-92 Standard)

Constants FALSE, TRUE


Operators +, −, *, /, >, <, =, >=, <=, <>, %, _ , ' , AND, NOT, OR
SQL ABSOLUTE (ABS), AVG, INT, MAX, MIN, SUM, COUNT
ASC, AT, CHAR (CHR), CHAR_LENGTH (LEN), LOWER, TRIM, SPACE, SUBSTRING
(SUBSTR/MID), UPPER, VALUE (VAL)
DATE, DAY, MONTH, YEAR
ADD, ALL, ALTER, ANY, AS, ASC, BETWEEN, BY, CREATE, DELETE, DESC,
DISTINCT, DROP, EXISTS, FROM, GROUP, HAVING, IN, INDEX, INNER JOIN, INSERT,
INTEGER, INTERSECT, INTO, LEFT [OUTER] JOIN, LIKE, MINUS, NULL, RIGHT
[OUTER] JOIN, FULL [OUTER] JOIN, ON, ORDER, SELECT, SET, TABLE, TO, UNION,
UNIQUE, UPDATE, VALUES, VIEW, WHERE

Symbols Used in Entity-Relationship Diagrams

Meaning Symbol Meaning Symbol

1 Relationship 1
Entity Entity One-to-One Relationship

1 Relationship M
Attribute
Attribute One-to-Many Relationship

Attribute M Relationship N
Key Attribute Many-to-Many Relationship

Participation constraints:
Relationship Relationship
Relationship Use on Mandatory side
Use on Optional side

You might also like