You are on page 1of 19

2000-CE

COMP STUD
Candidate Number
PAPER I
Centre Number
(SECT B & C)
HONG KONG EXAMINATION AND ASSESSMENT AUTHORITY
HONG KONG CERTIFICATE OF EDUCATION EXAMINATION 2000 Seat Number

COMPUTER STUDIES PAPER 2


Question-Answer Book

11:15 am – 1:15 pm (2 hours)


This paper must be answered in English
Marker’s Examiner’s
Use Only Use Only
Marker No. Examiner No.

Q.1
Q.2
Instructions:
Q.3
1. Write your Candidate Number, Centre Number and
Q.4
Seat Number in the spaces provided.
Q.4 0 1 2 3 4 0 1 2 3 4
2. Answer all questions.
Total
3. Write your answers in the spaces provided in this
question-answer book.

4. Supplementary answer sheets will be supplied


upon request. Write your Candidate Number on
each sheet and fasten them with string inside this
book.

Checker’s Use Only

Checker No.

Total

2000-CE-COMP STUD 1B & C-1 (C Version)


1. (a) Tai Ming writes a program to accept two character strings and output a message to say if the second character string
is part of the first one.

The program should produce the output below on the VDU. (In this output, all the data following a colon is entered
through the keyboard by the user. All other items are output from the program.)

Enter the length of character string 1 : 7


Enter the character 1 of string 1 : s
Enter the character 2 of string 1 : i
Enter the character 3 of string 1 : x
Enter the character 4 of string 1 : t
Enter the character 5 of string 1 : e
Enter the character 6 of string 1 : e
Enter the character 7 of string 1 : n

Enter the length of character string 2 : 4


Enter the character 1 of string 2 : t
Enter the character 2 of string 2 : e
Enter the character 3 of string 2 : e
Enter the character 4 of string 2 : n
String 2 is a part of string 1.

2000-CE-COMP STUD 1B & C-2 (C Version)


Tai Ming’s program is shown below:

Line Number Program Statement


10 #include <stdio.h>

20 #define true 1
30 #define false 0

40 int main()
50 {
60 char pat1[2][10];
70 int size1, size2, i, j;
80 int match;
90
100 printf("Enter the length of character string 1 : ");
110 scanf("%d", &size1);
120 for ( i = 0; i < size2; i++ )
130 {
140 printf("Enter the character %d of string 1 : ", i + 1);
150 scanf(" %c", &(pat1[0][i]));
160 }
170 printf("\n");
180 printf("Enter the length of character string 2 : ");
190 scanf("%d", &size2);
200 for ( i = 0; i < size2; i++ )
210 {
220 printf("Enter the character %d of string 2 : ", i + 1);
230 scanf(" %c", &(pat1[0][i]));
240 }
250 i = 1;
260 match == false;
270 while ( match && (i + size2 <= size1) )
280 {
290 for ( j = 0; j < size2; j++ )
300 if ( pat1[0][I - j] != pat1[1][j] )
310 match = false;
320 i--;
330 }
340 if ( match == true )
350 printf("String 2 is a part of string 1.\n);
360 else
370 printf("String 2 is NOT a part of string 1.\n");
380 }
However, there are several mistakes after line 80 in the program. Fill in the following table to show the location of
each mistake and its correction.

Line Number Corrected Statement

(12 marks)

2000-CE-COMP STUD 1B & C-3 (C Version)


(b) A small grocery store sells only 5 types of items: A, B, C, D and E which can be grouped into 3 different categories.
A, B, and C belong to category 1, D belongs to category 2, and E belongs to category 3. When a customer checks out
at the counter, a program in the cashier’s computer is executed to calculate the total number of purchased items for a
customer as well as the subtotals for the 3 categories. The algorithm of the computer program is shown below.

Step 1: Declare the following integer variables: totalsum, i, qty, complete.


Declare a character variable pitem.
Declare a one-dimensional integer array subtotal of size 3.
Step 2: Initialize totalsum to be 0; complete to be false and all elements in subtotal array to be
0.
Step 3: As long as complete is false, do steps 4 – 9 again.
Step 4: Read an item type into pitem.
Step 5: If pitem equals ‘Q’, then assign true to complete; otherwise
Step 6: Read in the quantity of the item type purchased into qty.
Step 7: Do Step 6 again when the input value of qty is not between 1 and 10 inclusively.
Step 8: Increase the corresponding element in subtotal by qty according to the category
of pitem.
Step 9: Increase the value of totalsum by qty.
Step 10: Display totalsum and the subtotal array.
Convert the algorithm into a C program.

2000-CE-COMP STUD 1B & C-4 (C Version)


(13 marks)

2000-CE-COMP STUD 1B & C-5 (C Version)


2. The following C program reads a set of character strings, stores them in an array, and uses another integer array to sort and
reference these character strings afterwards. Line numbers are added to the program listing for reference only.

Line Number Program Statement


100 #include <stdio.h>
110 #define true 1
120 #define false 0

130 int slist[40];


140 char varray[40][80];
150 char sitem[80];
160 int minv, size1;

170 void PrtOrdSeq(int pos)


180 {
190 while ( pos != -1 )
200 {
210 printf("%s\n", varray[pos]);
220 pos = slist[pos];
230 }
240 }

300 int P1(char sitem[], int nextstr)


310 ……

500 int P2(char sitem[], int i, int nextstr)


510 ……

700 int main()


710 {
720 int i;

730 for ( i = 0; i < 40; i++ )


740 slist[i] = -1;
750 size1 = 0;
760 do
770 {
780 printf("Enter character string %d :", size1 + 1);
790 scanf("%s", sitem);
800 if ( strcmp(sitem, "quit") != 0 )
810 size1 = P2(sitem, size1, minv);
820 } while ( strcmp(sitem, "quit") != 0 );
830 PrtOrdSeq(minv);
840 return 0;
850 }

(a) (i) What is the variable used for counting the number of inputted character strings?

(ii) Rewrite lines 760 - 820 by completing the following program segment:

printf("Enter character string %d :", size1 + 1);


scanf("%s", sitem);

while ( )
{

}
(6 marks)

2000-CE-COMP STUD 1B & C-6 (C Version)


(b) In the program, the array slist is used to reference and order the strings stored in the array varray. An index in
slist corresponds to a character string stored in varray. The value stored in an element of slist represents
where to find the next character string after the element in varray. If the value equals -1, there is no next character
string afterwards. Examples of the first 4 elements in varray and first 4 elements in slist after inputting 4
character strings are shown below:

varray
[0] [1] [2] [3]
boy cat apple flower

slist
[0] [1] [2] [3]
1 3 0 -1
In the above example, the alphabetical order of the four character strings represented by slist is ‘apple, ‘boy’, ‘cat’
and ‘flower’. The index of slist[2] corresponds to the third element of varray (varray[2]) which stores
‘apple’. The next character string is located by first taking the value stored in slist[2], which is 0; and it
represents the position of ‘boy’ in varray (varray[0]). Similarly, the next character string after ‘boy’ is located
by slist[0].

The function P1 is given below:

int P1(char sitem[], int nextstr)


{
int before;
int done_flag;

done_flag = false;
do
{
before = nextstr;
nextstr = slist[before];
if ( nextstr == -1 )
done_flag = true;
else if ( strcmp(sitem, varray[nextstr]) < 0 )
done_flag = true;
} while ( !done_flag );

return before;
}

(i) Complete the following table by dry-running P1.

Value of before just before


Case Values passed to the function
exiting the function
1 sitem = “bus” and nextstr = 2

2 sitem = “archie” and nextstr = 2

3 sitem = “dog” and nextstr = 1

(ii) What is the purpose of P1?

(8 marks)

2000-CE-COMP STUD 1B & C-7 (C Version)


(c) The function P2 is given below:

int P2(char sitem[], int i, int nextstr)


{
int curstr, newpos;

newpos = i;
strcpy(varray[newpos], sitem);
if ( i == 0 )
minv = 0;
else
{
if ( strcmp(sitem, varray[minv]) <= 0 )
{
slist[newpos] = minv;
minv = newpos;
}
else
{
curstr = P1(sitem, nextstr);
if ( slist[curstr] != -1 )
slist[newpos] = slist[curstr];
slist[curstr] = newpos;
}
}

return i + 1;
}

(i) Complete the following table by dry-running P2.

Value of slist[i] just before


Case Values passed to the function
exiting the function
1 sitem = “bus”, i = 4 and nextstr = 2

2 sitem = “come”, i = 5 and nextstr = 0

(ii) What is the purpose of P2?

(6 marks)

2000-CE-COMP STUD 1B & C-8 (C Version)


(d) Assume that the program is now run from the beginning and the user inputs the following character strings
sequentially into the program:

Peter Sang
Sue Black
Peter Ho
John June
quit

(i) Complete the following table with the value of MINV immediately before executing line 820.

sitem value before executing line 820 Value of minv


Peter Sang 0

Sue Black

Peter Ho

John June

(ii) Write the output of the program after execution.

(5 marks)

2000-CE-COMP STUD 1B & C-9 (C Version)


3. Wai-chu writes a computer game using a C program. The specifications of the game are as follows.

I. The game is played by 2 players.

II. The game uses a table of size n  n (i.e. a table with n rows and n columns) where n is an integer whose value is
between 2 and 8 inclusively. Each cell in the table is referenced by a row number and a column number.

III. At the beginning of the game, the computer asks a player to input n, and then it creates a table of size n  n. The
value of each cell in the table is an integer randomly generated between 1 and 9 inclusively.

For example, if n entered by a player is 3, the computer will create a table of size 3  3 as below:

column 1 column 2 column 3


row 1 5 6 3
row 2 2 1 7
row 3 1 4 9

IV. Each of the two players chooses any one of the cells in the table in turn. After a player has chosen a cell, the
content of the table is updated according to the following rules.

Rule A: In all cases, if the value in a cell is 0, it will remain 0.


Rule B: The value in the chosen cell will decrease by 1.
Rule C: The value(s) in the cell(s) of the same row will decrease by 1.
Rule D: The value(s) in the cell(s) of the same column will decrease by 1.
For example:
If player 1 chooses the cell referenced by row 2 and column 1, the values in the cells of the table will be updated
as follows.

column 1 column 2 column 3


row 1 4 6 3
row 2 1 0 6
row 3 0 4 9
If player 2 chooses the cell referenced by row 1 and column 2, the values in the cells of the table will be updated
as follows.

column 1 column 2 column 3


row 1 3 5 2
row 2 1 0 6
row 3 0 3 9

V. At the end of each turn, if ANY of the following 3 conditions is satisfied, the player of that turn wins, and the
game ends.

Condition 1: The sum of the values in any row is 0.


Condition 2: The sum of the values in any column is 0.
Condition 3: The sum of the values in any diagonal is 0.

2000-CE-COMP STUD 1B & C-10 (C Version)


The following shows a sample output of the game. (In this output, all the data following a colon is entered through the
keyboard by the user. All other items are output from the program.)

Enter the size of the table: 3


A 3 x 3 table is generated

The values of the cells in the table are >


5 6 3
2 1 7
1 4 9

Turn > Player 1


Please enter the row number: 2
Please enter the column number: 1

The values of the cells in the table are >


4 6 3
1 0 6
0 4 9

Turn > Player 2


Please enter the row number: 1
Please enter the column number: 2

The values of the cells in the table are >


3 5 2
1 0 6
0 3 9

Turn > Player 1


Please enter the row number: 1
Please enter the column number: 1

The values of the cells in the table are >


2 4 1
0 0 6
0 3 9

Turn > Player 2


Please enter the row number: 1
Please enter the column number: 3

The values of the cells in the table are >


1 3 0
0 0 5
0 3 8

Player 2 wins the game.

2000-CE-COMP STUD 1B & C-11 (C Version)


The declaration and the main body of the program have already been written by Wai-chu. Some functions and procedures
are missing. Part of the program is as follows.

#include <stdio.h>
#define true 1
#define false 0

int square[8][8];
int turn, row_chosen, column_chosen, n;

………..
………..

int main()
{
printf("Enter the size of the table: ");
scanf("%d", &n);
init(square);
printf("A %d x %d table is generated\n", n, n);
show(square);
turn = 2;
while ( !endgame(square) )
{
if ( turn == 1 )
turn = 2;
else
turn = 1;
printf("\n");
printf("Turn > Player %d\n", turn);
get_data();
process(square, row_chosen - 1, column_chosen - 1);
show(square);
}
printf("\n");
printf("Player %d wins the game.\n", turn);
}

Identifiers Description
square 2-dimensional array used to store the values of the cells in the table
turn variable used to store player number of the current turn
row_chosen variable used to store row number
column_chosen variable used to store column number
n variable used to store the size of the table

2000-CE-COMP STUD 1B & C-12 (C Version)


You are not allowed to add any new variable in answering the question.

(a) The procedure init is used to assign random integers to the n  n table. Complete the procedure init by filling in
the missing parts in the following program segment.

void init( )
{
int r, c;

srand(time(NULL));
for ( r = 0; r < n; r++ )
for ( c = 0; c < n; c++ )

square[r][c] = ;
}
(2 marks)

(b) Write the procedure get_data to get the row and column number entered by the players without data validation.

void get_data()
{

}
(2 marks)

2000-CE-COMP STUD 1B & C-13 (C Version)


(c) Write the procedure show to display the contents of the table.

void show(int square[8][8])


{
int r, c;

}
(3 marks)

(d) Write the procedure process to implement all the rules mentioned in the specification of the game.

void process(int square[8][8], int row, int column)


{
int r, c;

}
(5 marks)

2000-CE-COMP STUD 1B & C-14 (C Version)


(e) (i) Write a function sumrow that returns the sum of a row.

int sumrow(int square[8][8], int r)


{
int c, sum;

(ii) Write a function sumcolumn that returns the sum of a row.

int sumcolumn(int square[8][8], int c)


{
int r, sum;

(iii) Write a function sumdiagonal1 that returns the sum of a diagonal from upper left corner to lower right
corner.

int sumdiagonal1(int square[8][8])


{
int point, sum;

2000-CE-COMP STUD 1B & C-15 (C Version)


(iv) Write a function sumdiagonal2 that returns the sum of a diagonal from upper right corner to lower left
corner.

int sumdiagonal2(int square[8][8])


{
int point, sum;

}
(7 marks)

(f) Using the 4 functions in part(e), write a function endgame that returns a true value if any one of the 3 conditions
mentioned in specification V is satisfied, otherwise, returns a false value.

int endgame(int square[8][8])


{
int r, c, flag;

}
(6 marks)

2000-CE-COMP STUD 1B & C-16 (C Version)


4. (Note: In this question, a maximum of 4 marks will be awarded for effective communication.)

Fat Roberto’s is a popular Italian restaurant. The owner of the restaurant, Mr Roberto Celini, feels that the business is
growing so fast that the kitchen and t he dining room are getting out of control. Customers have been complaining that the
food quality and service are deteriorating. Mr Cellini decides to hire a consulting firm to review the operations of the
restaurant.

The consulting firm has conducted a survey of customers, the kitchen staff, the waiters and the manager of the restaurant.
The observations below are some of the most common problems cited in the survey.

Observations made by customers


• Orders are cancelled because waiting time is too long.
• Food is cold when delivered to the table.
• The bill is wrong.
• Customers are not informed when particular food items are sold out.

Observations made by waiters


• Different order slips must be used for entrées (main courses), beverages (drinks), appetizers, and desserts.
• Orders must be written in Italian for the Italian chef.
• Kitchen staff do not inform waiters when particular food items are sold out.
• Workloads of waiters is not fairly distributed.

Observations made by kitchen staff


• Some raw materials are frequently over-stocked while others are under-stocked or rotten when needed.
• Cooked food is left in the kitchen for a long time. By the time the food is delivered to the table, it has turned cold.
• Order slips were difficult to read or wrong.
• Orders are written for particular food items that are already sold out.

Observations made by the manager


• The record of the stock level of raw materials is not kept accurately.
• Waiters spend too much time writing orders and traveling back and forth from the kitchen. Thus, less time is spent
on serving the customers in the dining room.

The consulting firm has proposed to computerize the operations. Several networked computer terminals will be installed in
the kitchen, in the dining room, at the cashier counter and in the store-room. The computer system will connect the kitchen,
dining room, cashier counter and store-room to form an integrated information system.

(a) What essential data items should be included in a food order slip produced by the proposed system?

(4 marks)

(b) The consultant suggests using a touch screen monitor as the input device instead of a keyboard and mouse. Give
TWO advantages of this suggestion.

(2 marks)

2000-CE-COMP STUD 1B & C-17 (C Version)


(c) The availability of some food and beverages is restricted to certain time of periods and quantities. Briefly describe
the steps which the system will have to perform in order for a customers’ order to be processed correctly.

(4 marks)

(d) The consultant suggests waiters should use a personal identification number (PIN) to access the system. However, the
manager feels that using a PIN is not secure enough. Suggest TWO alternatives and give reasons to support your
suggestions.

(4 marks)

(e) The manager wants to have a monthly sales analysis and inventory planning report. What essential information
should be included in the report?

(3 marks)

(f) Briefly describe the steps which the system will have to perform to produce a customer’s bill.

(4 marks)

END OF PAPER

2000-CE-COMP STUD 1B & C-18 (C Version)


A Partial Character List for ASCII

Character ASCII Character ASCII Character ASCII


0 48 J 74 d 100
1 49 K 75 e 101
2 50 L 76 f 102
3 51 M 77 g 103
4 52 N 78 h 104
5 53 O 79 I 105
6 54 P 80 j 106
7 55 Q 81 k 107
8 56 R 82 l 108
9 57 S 83 m 109
: 58 T 84 n 110
; 59 U 85 o 111
< 60 V 86 p 112
= 61 W 87 q 113
> 62 X 88 r 114
? 63 Y 89 s 115
@ 64 Z 90 t 116
A 65 [ 91 u 117
B 66 \ 92 v 118
C 67 ] 93 w 119
D 68 ^ 94 x 120
E 69 _ 95 y 121
F 70 ` 96 z 122
G 71 a 97 { 123
H 72 b 98 | 124
I 73 c 99 } 125

List of Operators and Reserved Words (C)

#include, +, -, *, /, ++, --, +=, -=, *=, /=, %=, ==, %, >, <, =, >=, <=, !=, &&, ||, !, sqrt, rand, abs, strcat, strncat, strlen,
atoi, strcpy, strncpy, const, void, return, int, float, char, \0, strcmp, strncmp, true, false, FILE, main, /*…*/, if…else, for,
while, do…while, switch…case…break, break, continue, scanf, printf (%d, %f, %c, %s), \n, \t, fopen, getc, fgets, putc,
fputs, EOF, fclose

2000-CE-COMP STUD 1B & C-19 (C Version)

You might also like