Professional Documents
Culture Documents
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
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.
Checker No.
Total
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.)
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.
(12 marks)
(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:
while ( )
{
}
(6 marks)
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].
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;
}
(8 marks)
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;
}
(6 marks)
Peter Sang
Sue Black
Peter Ho
John June
quit
(i) Complete the following table with the value of MINV immediately before executing line 820.
Sue Black
Peter Ho
John June
(5 marks)
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:
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.
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.
#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
(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)
}
(3 marks)
(d) Write the procedure process to implement all the rules mentioned in the specification of the game.
}
(5 marks)
(iii) Write a function sumdiagonal1 that returns the sum of a diagonal from upper left corner to lower right
corner.
}
(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.
}
(6 marks)
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.
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)
(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
#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