Algorithm Practice
2000 Computing Studies 2/3 Unit
(Common)
Question 12:
Study the
following algorithm.
BEGIN
READ A
READ B
WHILE A <> 2 AND B <> 2
PRINT A * B
READ A
READ B
ENDWHILE
END
From the
input data
0, 1, 1, 2,
2, 3, 2, 2
the output
produced will be
(A) 0
(B) 0, 2
(C) 0, 2, 6
(D) 0, 2,
6, 4
Question 15:
Which of
the following programming structures has a pre-test repetition?
Question 14:
A person
wants to use a lawnmower. The person goes to a lockable shed, opens the shed
door, removes each object, in turn, that may be in the way and pushes the
lawnmower out of the shed. Which of the following algorithms best describes
this task?
Question 22:
(a) A
security alarm system has been installed at the entrance of a building. The
system will only allow entry to people who have keyed in the correct four digit
number, using a key pad, within 10 seconds. After entering the number, the user
presses an ‘ok’ button to allow the number to be checked. Only one attempt at
entering the four digit number is allowed.
A siren
will sound if:
• the number entered is incorrect
• the 10 second time period, allowed for keying in all
four digits and pressing the ‘ok’ button, has elapsed.
(i) Design a minimum set of
tests, for the given description, by completing
the table.
(ii) The
algorithm that is designed to operate the system is given below: There is at
least ONE error in the above algorithm. Identify the error(s) and explain the
impact on the system.
1.
BEGIN ALARM
2.
set time
elapsed to zero
3.
set number
to not valid
4.
WHILE time
elapsed < 10 seconds OR number is not valid
5.
IF ‘OK’ key
has been pressed THEN
6.
READ number
from key pad memory
7.
IF number
is not valid
8.
sound siren
9.
END IF
10.
END IF
11.
Update time
elapsed
12.
ENDWHILE
13.
sound siren
14.
END ALARM
(iii) The
original algorithm for the security alarm system does not allow the user to
re-enter the code number if they have accidentally pressed an incorrect key.
Rewrite the algorithm, using EITHER a flowchart OR pseudocode, so that
• a ‘cancel
key’ can be used to allow re-entry of the code number, and reset of the timer,
within 10 seconds;
• up to
three entry tries are allowed.
(b) A sports car has an
automated braking system. This braking system automatically corrects the
braking pressure when the car is travelling around corners dangerously. Braking
pressure will be automatically applied, based on conditions shown in the table.
Use EITHER
a flowchart OR pseudocode to write an algorithm that determines
when the
braking pressure should be automatically applied as specified.
2000
Computing Studies 3 Unit (Additional)
Question 11:
Array A =
[7, 21, 8, 7, 23, 11, 27]
Array B =
[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z]
B [1] = a
Using the
data in Array A and Array B, what will be the output of the following
algorithm?
BEGIN
FINAL = A [3]
COUNT = 2
REPEAT
DISPLAY B [A [COUNT] – COUNT]
COUNT = COUNT + 1
UNTIL COUNT = FINAL
END
(A) select
(B) second
(C) secret
(D) seconds
Question 12:
The set of
numbers 8, 5, 1, 7, 3, 2 was read into the following program:
BEGIN
SET COUNT TO 1
READ A
REPEAT
READ A
READ B
WHILE A > 0
DISPLAY B
A = A – 1
ENDWHILE
COUNT = COUNT + 1
UNTIL COUNT = 3
END
Which of
the following is the correct output?
(A)
1111333333
(B)
111113333333
(C)
555555557
(D)
555555557222
Question 16:
IF TEMP =
35°C THEN
SET CONDITION TO "HOT"
ELSE
IF TEMP = 25°C THEN
SET CONDITION TO
"WARM"
ELSE
IF TEMP = 20°C THEN
SET CONDITION TO
"NORMAL"
ELSE
SET CONDITION TO "COLD"
ENDIF
ENDIF
ENDIF
How many
values would be required to form a minimum set of test data for checking this
algorithm?
(A) 3
(B) 4
(C) 5
(D) 6
Question 17 The following diagram shows the
contents of an array of records used for mixing colours.
The name of
the array is ‘COLOUR’.
The
algorithm below is used to calculate colour values, using an array.
BEGIN
SET TOTAL TO 0
SET COUNTER TO 1
WHILE COUNTER < 4
TOTAL = TOTAL + COLOUR [COUNTER]
. GREENVALUE
ADD 1 TO COUNTER
ENDWHILE
WHILE COUNTER < 4
TOTAL = TOTAL + COLOUR [COUNTER]
. REDVALUE
ADD 1 TO COUNTER
ENDWHILE
PRINT TOTAL
END
What will
be the output of this algorithm?
(A) 10
(B) 11
(C) 18
(D) 25
1999 Computing Studies 3 Unit
(Additional)
Question
8:
A string, S, has been assigned the following
characters:
a b c d •
A table for
the characters of string S is given
below.
Using the
following algorithm:
BEGIN
Set Sum to zero
Set A to value of 'a'
Set Count to 1
REPEAT
Set Sum to Sum + A
Set A to value of S[count]
Set Count to Count + 1
UNTIL A<47
END
the
variable Sum will have the value
(A) 250
(B) 296
(C) 311
(D) 357
Question 17:
The array
NAME contains 60 elements, NAME [1] to NAME [60] of type string. Which one of
the following algorithms will place the string "Barney" into every
element of the array?
Question 18:
Using the
following algorithm with the array A:
BEGIN MAIN
PROGRAM
INITIALISATION
Set Count to 1
Set Row to 1
REPEAT
Col = Count
REPEAT
Col = Col + 1
temp = A[Row] [Col]
A[Row] [Col] = A[Col] [Row]
A[Col] [Row] = Temp
UNTIL Col = 4
Row = Row + 1
Count = Count + 1
UNTIL Row = 3
END MAIN
PROGRAM
which of
the following resulting arrays is correct?
Question 20:
In a buyer
rewards scheme, customers accumulate points each time they purchase goods.
There are four levels of rewards: Bronze, Silver, Gold and Platinum, depending
upon the number of accumulated points. The algorithm below is used to calculate
the reward level each time a purchase is made.
A customer
who has already accumulated 700 points makes new purchases of $500 and $1300.
Their new
reward level will be
(A) Bronze.
(B) Silver.
(C) Gold.
(D)
Platinum.
Question 22
(a) The
State Electoral Commission has decided to develop a computer-based voting
system based on the following scenario.
• All eligible voters eighteen years and older will
receive a unique IDENTIFYING NUMBER by mail from the Electoral Commission.
• Voters will go to polling booths at their local school
to vote.
• Computer terminals connected to a wide area network
will be used at the polling booths.
• Voters will have to enter their IDENTIFYING NUMBER
into the system in order to vote, and their name and date of birth will be
displayed for confirmation.
• Voters will have to vote for both the Legislative
Assembly and Legislative Council.
• Voters will input the number 1 against their preferred
candidate.
• Voters will be requested to check their selections and
make any changes if necessary before they are logged out of the system.
• A receipt will be given to each voter, indicating the
time, date and place of voting.
(i) Create
a design prototype for the voting system which shows:
1 input screens;
2 output screens;
3 output reports.
Ensure that
the screens show elements of good layout and are sequenced in a logical order.
(ii)
Justify the use of a prototype by the Electoral Commission.
(iii) What
is pre-processing? Give TWO examples using the above scenario.
(iv) There
are approximately three million voters who are eligible to vote.
Describe
the nature and size of the data structure that would be necessary
to store
the identification numbers and other details of each voter.
1998 Computing Studies 3 Unit Additional
Question 5. The following code segment
BEGIN
Set B to 100
Set A to 1
WHILE A<10
B = B / A
A = A – 1
Print A
ENDWHILE
END
will result
in
(A) an
infinite loop.
(B) a
compile-time error.
(C) a
divide-by-zero error.
(D) a
circular reference error.
Question 9. The following algorithm
initialises an array named ‘ALPHA’, indexed as ALPHA [ROW, COLUMN].
BEGIN
Set A to 1
REPEAT
Set Y to 1
REPEAT
Set ALPHA [Y, A] to A + 1
Set Y to Y + 1
UNTIL Y > 4
Set A to A + 1
UNTIL A > 4
END
Which of
the following arrays will be produced by the algorithm?
Question 11:
The array
FIDO contains 500 elements, FIDO [1] TO FIDO [500]. Which ONE of the following
algorithms will initialise all elements of FIDO to the value of 9?
Question 21
Use this
material for Question 21 parts (b) and (c).
A charity organisation conducts adventure holiday camps
for young disabled people 18–25 years of age. Participants must choose ONE
activity, such as rock climbing or white-water canoeing, during the camp. You
are part of a project team working on a new system for camp administration. The
system will cover the major areas of camper registration and allocation of
campers to selected activities. You have written the camper registration
program. The system uses a number of database files, of which only three are of
interest to you. These files and the fields relevant to your program are
summarised below.
Part of the data dictionary for
the system is shown below:
All fields must have an entry.
(b) (i)
Design and draw the entry screen for the camper registration program.
(ii)
Describe the features of good design that you have incorporated into your
screen.
(iii)
Describe, using diagrams if necessary, how you would display:
1. general input requirements;
2. error messages.
(c) (i)
Specify the data validation requirement for each of the following fields:
• family_name
• phone_number
• gender
• date_of_birth
• activity_code.
(ii)
Describe the requirements of good test data for the camper registration system.
(iii)
Full_name is a combination of the first_name and family_name fields. It is used
in a program for addressing letters to campers. When the first_name field
contains more than one name, only the first letter of the second name is used,
followed by a full stop.
Example:
Jane G. Brownwith
Describe
the program syntax of full_name using BNF or EBNF or
railroad
diagrams.
Question 22.
(a) HOLD is
a ten-element array that is used to store words. It does so with each of
its
elements storing either a letter of the word or the blank character .
(i) Write
an algorithm, using EITHER a flowchart OR pseudocode, that will find the number
of non-blank characters in the array HOLD, and store it in a variable called
LENGTH.
(ii) Using
a variable called LENGTH that holds the length of the word stored in the array
HOLD, write an algorithm that will reverse the order of the characters in HOLD
without affecting the blanks. You may use EITHER a flowchart OR pseudocode.
(iii) Write
an algorithm using EITHER a flowchart OR pseudocode that will perform an
ascending, alphabetical sort on the array HOLD.
(b) A
casualty ward of a hospital wants to install a computerised patient management
system. The system will display the name of the next person to see a doctor.
The system will store patients’ names in the priority order in which they will
see a doctor. The system can accommodate up to 50 patients. Patients waiting to
see a doctor are classified into one of the following categories:
The names of the patients are
stored in the array NAMES and their categories in the array CATEGORY.
Category S and R patients are treated as having equal priority and will be placed
in the queue waiting to see a doctor in the order in which they arrive at the
casualty ward. Category U patients
take precedence over the other patients.
Whenever a
patient with an emergency classification of U
arrives at the casualty ward they are given the highest priority available,
moving patients of lower priority down the order.
Using
flowcharts or pseudocode, write algorithms for the patient management system
to:
(i) list
the patients in the order in which they will see a doctor;
(ii) add a
new patient to the list of waiting patients, based on their priority;
(iii)
delete a patient from the list of waiting patients.
1999 Computing Studies 2/3 Unit
(Common)
Question 17:
Study the
following algorithm.
READ A
READ B
REPEAT
SET C TO (A+B)/2
PRINT C
READ A
READ B
UNTIL A = 0
The
following values are entered in sequence
4 6 2 0 0 8
3 5.
Which of
the following shows the correct output?
(A) 5
(B) 51
(C) 514
(D) 5144
Question 19:
What will
be the output produced by the following algorithm if A = 1 and B = 2?
READ A
READ B
WHILE NOT ((A<=0) AND (B<=0))
IF A>B THEN
PRINT ‘First’
ELSE
IF A<B THEN
PRINT ‘Second’
ELSE
PRINT ‘Equal’
ENDIF
ENDIF
READ A
READ B
ENDWHILE
(A) First
(B) Equal
(C) Second
(D) no
visible output
Question 22
(a) An
Internet service provider (ISP) has an on-line access policy that includes the
following conditions:
1 a customer is restricted to 120 minutes for each
on-line session;
2 no more than 20 Mb of download is allowed in each
on-line session.
Time is to
be logged in minutes, and downloads in whole megabytes. During an on-line
session a small message ‘access OK’ is displayed in a corner of the screen.
Once either
of the above conditions has been met, the message changes to ‘access
terminated’ and the customer is automatically logged off.
(i) Design a set of test data
that will be needed to desk check the above conditions and show the expected
display.
(ii) The
algorithm module below was designed to implement the access
policy.
BEGIN
INTERNET ACCESS CHECK
set time to zero
set download to zero
WHILE time < 120 OR download < 20
update time
update download
IF time < 120 OR download
< 20 THEN
display ‘ACCESS TERMINATED’
ELSE
display ‘ACCESS OK’
END IF
ENDWHILE
END
INTERNET ACCESS CHECK
There are
errors in the above algorithm.
Identify the line number(s) in
which an error occurs and explain the effect of that error.
(iii) Using
pseudocode or a flowchart, rewrite the algorithm so that it correctly carries
out its intended purpose. Use the space below.
(iv) In
response to complaints from customers, the ISP decides to allow downloads in
excess of the 20 Mb limit but intends to charge 10 cents for each Mb downloaded
above 20 Mb. Describe a modification that would need to be made to your
corrected
algorithm
in part (a) (iii) to accomplish this change of policy.
(b) (i)
Complete a desk check of the following pseudocode using the data values
[2, 3, 7, 5, 1, 6, 999] and a
table format of:
(ii) What
is the purpose of two input statements?
(iii) Write
the pseudocode as a flowchart.
1998 Computing Studies 2/3 Unit
(Common)
Question 12. The flowchart structure that
best shows a post-test is
Question 16:
BEGIN
set A = 2
WHILE A < 7
read B
set A = A + B
print A
ENDWHILE
END
If the
values available for B in the above algorithm are 1, 3, 1, 1, 5, then the
output from the algorithm would be
(A) 3, 6, 7
(B) 3, 6,
7, 8
(C) 2, 3,
6, 7
(D) 3, 5,
3, 4, 7
Question 17. An algorithm to determine the entry fee to an amusement
park would be:
A minimum
test set for the algorithm is
(A) 4, 13
(B) 4, 5,
13
(C) 4, 5,
12, 13
(D) 4, 5,
12, 13, 15
Question 18. Two players take turns until
they guess a secret number. After each guess the player is told if the guess is
too low, too high, or correct.
The
algorithm that best illustrates this game is
Question 20. The process of iteration best represented in pseudocode
is
(A) IF counter < 10 THEN increment
counter by 1
ENDIF
(B) WHILE counter < 10
display counter
increment counter by 1
ENDWHILE
(C) CASEWHERE iteration
required : increment counter by 1
not required : increment counter by 2
ENDCASE
(D) BEGIN SUBPROGRAM
iteration
END SUBPROGRAM
Question 22:
(a) The face value of a card can be 2, 3, 4, 5,
6, 7, 8, 9, 10, Jack, Queen, King, Ace or Joker. A game is played with a deck
of cards that contains one Joker. The deck is shuffled and then cards are dealt
out one at a time until the Joker is reached. One point is received for each
card dealt before the Joker appears. The CARD GAME algorithm below illustrates
this process.
(i) Using
pseudocode OR a flowchart, modify the CARD GAME algorithm so that it will not
print out the face value of the Joker. Indicate the line numbers of all
affected statements.
1.
BEGIN CARD
GAME
2.
Shuffle
cards
3.
Points = 0
4.
REPEAT
5.
READ card
6.
PRINT face
value of card
7.
Points =
Points + 1
8.
UNTIL card
= Joker
9.
PRINT
points
10.
END CARD
GAME
(ii) There
is at least one error in the scoring of the CARD GAME algorithm. Describe ONE
error and explain how it might be fixed.
(iii) A
player’s scores for ten turns of the CARD GAME algorithm have been recorded in
the array SCORES below. Using EITHER pseudocode OR a flowchart, write an
algorithm that will
• read the scores into the array;
• search the array SCORES;
• record the highest score in a variable called HIGH
VALUE.
Your
algorithm should print the highest score before it ends. 30 24 34 16 20 10 16 4
22 8
(b) A lift
has a computer-controlled alarm system and automatic sensors that can detect
whether people are in it. The lift is monitored continuously in its operation.
(i) If the
lift has a fault and stops between floors, one of two types of alarms
is
signalled.
Priority 1—if people are in the lift. A recorded message
is played ‘HELP IS ON THE WAY’ and an alarm is sounded.
Priority 2—if no people are in the lift. No message is
played but an alarm
is sounded.
Using
pseudocode OR a flowchart, write an algorithm, in the space
below, to
show the lift’s warning system.
(ii) If a
fire occurs in the building, the lift is programmed to stop at the nearest
floor. The alarm system plays a recorded message ‘FIRE—DO NOT USE LIFT’. When
there are no people in the lift, the water sprinkler system is activated.
Using
pseudocode OR a flowchart, write an algorithm to show the lift’s fire warning
system. You must include the consequences for BOTH the fire occurring AND the
fire not occurring.