0% found this document useful (0 votes)
49 views10 pages

2003 VITEC Software Design and Development Engineer Examination (Afternoon, Part 2)

The document describes the design and development of a number guessing game program. It includes: 1) An overview of the game rules where two competitors take turns guessing each other's randomly generated 4-digit numbers based on hints of "hits" and "misses". 2) Descriptions of key functions used in the program including generating random numbers, comparing guesses to the answer, and selecting the next guess based on previous results. 3) An explanation of how the program works by calling these functions to have the computer and player take turns guessing until one wins. Global variables and arrays are used to store and pass information between functions.
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)
49 views10 pages

2003 VITEC Software Design and Development Engineer Examination (Afternoon, Part 2)

The document describes the design and development of a number guessing game program. It includes: 1) An overview of the game rules where two competitors take turns guessing each other's randomly generated 4-digit numbers based on hints of "hits" and "misses". 2) Descriptions of key functions used in the program including generating random numbers, comparing guesses to the answer, and selecting the next guess based on previous results. 3) An explanation of how the program works by calling these functions to have the computer and player take turns guessing until one wins. Global variables and arrays are used to store and pass information between functions.
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/ 10

2003 VITEC

Software Design and Development Engineer Examination


(Afternoon, Part 2)

Questions must be answered in accordance with the following:


Question Nos. Q1
Question Selection All Sub-Questions are compulsory
Examination Time 15:30-16:30 (60 minutes)
Instructions:
1. Use an HB pencil. If you need to change an answer, erase your previous answer completely and
neatly. Wipe away any eraser debris.
2. Mark your examinee information and test answers in accordance with the instructions below.
Your test will not be graded if you do not mark properly. Do not mark on white on the answer
sheet outside of the prescribed places.
(1)Examinee Number
Write your examinee number in the space provided, and mark the appropriate space below
each digit.
(2)Date of Birth
Write your date of birth (in numbers) exactly as it is printed on your examination admission
card, and mark the appropriate space below each digit.
(3)Write each answer in the space specified for that question.
(4)Write your answers clearly and neatly. Answers that are difficult to read will receive a lower
score.
3. After the test, you may take this question booklet home with you.

Do not open the exam booklet until instructed to do so.


Inquiries about the exam questions will not be answered.

1
Q1. Read the following text regarding the creation of a game program, then answer Sub-
Questions 1 through 4.

A decision was made to create a number guessing game program in which a person
(hereinafter referred to as the “player”) competes with a computer. The rules of the game
are as follows.
(1) The competitors (player and program) each select a four-digit number consisting of
four different numerals. In this example, this four-digit number is called the correct
answer. The first digit in the correct answer (most significant digit) may be 0.
(2) Competitors take turns trying to guess each other’s correct answer, and the first one to
guess correctly wins. If both competitors guess correctly in the same number of tries,
then they tie.
(3) When a competitor receives the other competitor’s guess, it is compared with the
receiving competitor’s own correct answer, and a response is sent to the other
competitor consisting of the number of hits, hit, or numerals which are correct in
both numerical value and digit position; as well as the number of misses, miss, or
numerals which are contained in the correct answer, but have incorrect digit positions.
For example, if the correct answer is 1357 and the other competitor guesses 5310, then
the response consists of one hit (hit) and two misses (miss).

An overview of the number guessing game program is presented below.


1. The program determines a correct answer using random numbers.
2. The following are repeated until one of the competitors guesses the other’s correct
answer.
2-1. Comparison against correct answer
The program receives the player’s guess, compares it against the correct answer,
and returns the numbers of hits, hit and misses, miss.
2-2. Guessing process
2-2-1. The program determines the next guess based on the hit and miss it
has so far received, then sends that guess to the player.
2-2-2. The program receives the hit and miss calculated by the player.

Below, the variables defined in the function are local variables, and the function’s
arguments are passed by value unless otherwise specified. The array subscripts start at 0,
and array elements are designated as 0th, 1st, 2nd, etc.
Figure 1 shows the function for determining the correct answer. With this function,
numbers 0 through 9 are randomly generated one at a time, and an array tab consisting of
ten elements is used to obtain a four-digit number with no duplicate numerals. “random”

2
is a function for randomly generating natural numbers, and “%” is a modulus operator. The
array tab is used as follows.

c The initial value of all elements is 0.


d If the tab[a] value remains at the initial value 0 for a randomly generated single-
digit number a, then a is a number appearing for the first time, and 1 is inserted in
tab[a].
e If the value of tab[a] is 1, then the value is not changed.

function set_answer ()
int tab[10], n, i, a
Set all tab elements to 0
n = 0
for (i = 1 to 4)
repeat
a = random() % 10
until(tab[a] = 0)
tab[a] = 1
n = a

endfor
return n
endfunction

Fig. 1 “set_answer” Function

The algorithm for calculating hit and miss will now be considered.
First, the similarity of two four-digit numbers x and y is defined as follows, using the
hit and miss of the two numbers:
similarity(x, y) = hit * 10 + miss
The set of the similarity values and the (hit, miss) values has a one-to-one
correspondence, so the similarity is used within the program instead of the hit and
miss.
Similarity calculation is used when the player’s guess is compared with the correct
answer, and when the program is determining its own guess. This calculation is done
frequently, so a highly efficient execution method will now be considered.
In order to improve execution efficiency, before similarity is calculated during the
game, one of the numbers to be compared is translated into a form called an array
expression. In this translation process, if the subject number is d4d3d2d1, then i is stored
in the dith element of the array, and 0 is stored in the other elements. Figure 2 shows the

3
function for translating the number y into an array expression. “pos” is an array which
comprises ten elements and is called by reference.

function make_pos(y, pos)


int i, m
Set all pos elements to 0
for(i = 1 to 4)
m = y % 10
y = y / 10
pos[ b ] = i
endfor
endfunction
Fig. 2 “make_pos” Function

When the array expression (pos) for the number y is used, similarity(x, y) can be
determined using the “sim” function in Figure 3.

4
function sim(x, pos)
int hit, miss, i, p
hit = 0
miss = 0
for(i = 1 to 4)
p = x % 10
x = x / 10
if( c )
if( d ) hit = hit + 1
else miss = miss + 1
endif
endif
endfor
return hit * 10 + miss
endfunction
Fig. 3 “sim” Function

The guessing algorithm will now be considered.


If all four-digit numbers consisting of four different numerals are generated, the set of
generated numbers will necessarily contain the correct answer. Therefore, these numbers
are generated in advance and inserted in the “nums” array.
The algorithm for selecting a guess value in order to guess the player’s correct answer is as
follows.
Numbers are taken from “nums” sequentially, and a number which is not inconsistent with
the similarity information obtained thus far is used as the next guess.
For example, consider a case in which the third guess is to be selected. Assume that the
similarity values of the correct answer and the previous two guesses were as follows:

Guess similarity
First 1234 12
Second 1345 02

In this case, assume that the next number taken from “nums” is 2304. A calculation is
performed to determine whether 2304 is suitable as the next guess, with the following
results: similarity(2304, 1234) = 12,similarity(2304, 1345) = 11.
These results show that 2304 is inconsistent with the second result, so it is not suitable as
the next guess.

5
Now assume that the next number taken from “nums” is 0432. A calculation is performed
to determine whether 0432 is suitable as the next guess, with the following results:
similarity(0432, 1234) = 12,similarity(0432, 1345) = 02. These
results show that 0432 is not inconsistent with the previous results, so it is used as the next
guess.
When these considerations are put together, the function for determining the kth guess is as
shown in Figure 4. In this case, the “nums” array is an array containing correct answer
candidates; guess[i-1] is the array expression for the ith guess; and log[i-1] is the
similarity value between the ith guess and the correct answer. “index” is a
subscript specifying a position before which the correct answer does not exist in the
“nums” elements. The initial value for “index” is –1.

function make_guess(k)
int j
index = index + 1
for() // Infinite loop
j = 0
while(j < k – 1 and sim(nums[index], guess[j]) = log[j])
e

endwhile
if(j < k - 1)
f

else
return nums[index]
endif
endfor
endfunction
Fig. 4 “make_guess” Function

6
When all of the above-mentioned functions are used, the number guessing game program
is as shown in Figure 5. “nums”, “guess”, “log” and “index” are global variables.

int log[M], guess[M][10], nums[N], index // M and N are array sizes


main()
char r
In “nums”, insert all four-digit numbers comprising four different numerals
for() // Repeat game
play()
write “Continue game? Y/N”
read r
if(r = “N” ) // Player selects to end game
exit
endif
endfor
endmain

function play()
int ans, pos_a[10], i, r, g, hit, miss, guess_person
ans = set_answer()
make_pos(ans, pos_a)
index = -1
i = 1
for() // Repeat until correct
read guess_person // Read player’s guess
r = sim(guess_person, pos_a) // Calculate similarity
write r / 10, r % 10 // Display results
g = make_guess(i) // Calculate i th guess
write g // Display guess
read hit, miss // Read guess results
if(hit = 4 or r = 40) // Was guess correct?
break
endif
log[i-1] = 10 * hit + miss // Record results
make_pos(g, guess[i-1]) // Record guess
i = i + 1
endfor
if( g ) // Player wins
write “You win”
elseif( h ) // Computer wins
write “I win”
else
write “It’s a tie” // Tie
endif
endfunction
Fig. 5 Number Guessing Game Program

7
Sub-Question 1
In the program in Figure 5, what is the minimum size N of the “nums” array?

Sub-Question 2
Insert appropriate words in blanks a through h in Figs. 1 through 5.

Sub-Question 3
After the player plays the game for a while, the program stops responding. An investigation
of the issue showed that it occurred because the player’s hit or miss response value was
incorrect, so the program could not find a candidate which would be the correct answer.
Changes need to be made so that in such cases, “make_guess” returns a value of –1.
What type of judgment should be added to the “make_guess” function in Fig. 4? Answer
in 4 words or less.

Sub-Question 4
In the algorithm of the program in Figure 5, the computer always repeats the same guesses
every time, even when the player plays against the computer repeatedly. For this reason, it
becomes possible for the player to find numbers that are difficult for the computer to guess.
In order to avoid this, the program should use a different sequence of guesses each time. In
order to use a different guessing sequence each time, a certain operation should be
performed on the “nums” array value immediately before “play” is called from “main”.
Describe this operation in 4 words or less.

8
[Memo]

9
[Memo]

10

You might also like