Coding Practice 1
Coding Practice 1
Trace the following algorithmic fragment for N = 3. Show all working in a trace
table.
SUM = 0
loop COUNT from 1 to (N div 2)
if N mod COUNT = 0 then
SUM = SUM + COUNT
end if
end loop
if SUM = N then
output "perfect"
else
output "not perfect"
end if
Now find all possible values of N < 50, for which output is "perfect".
---------------------------------------------------------------------------
2
An insurance company holds a large database of information about its customers,
including the date of their next payment.
Once a month the database is searched to compile the following lists:
• list 1: customers whose next payment date will be within the next 30 days
• list 2: customers whose payment date has passed by more than 14 days but
less than, or equal to, 30 days
• list 3: customers whose payment date has passed by more than 30 days.
Customers who are in list 3 are flagged for deletion.
Outline the steps needed to search the collection and return the next job to be
printed.
(write algorithm only, not pseudocode)
----------------------------------------------------------------------------
4
A cycling tour lasts for 5 days. The total time for each competitor is recorded in
a one-dimensional array, TIMES[]. After each day’s race, the array entry for each
competitor is increased by their time for that day.
There are 10 competitors and the 3 fastest times are transferred to the array
FASTEST[] and displayed on a screen each day.
Construct an algorithm to transfer the 3 fastest times from the array TIMES[] to
the array FASTEST[]. Assume that the array TIMES[] is not sorted.
(write pseudocode and show the output of your algorithm for an example array
TIMES[]).
----------------------------------------------------------------------------
5
Trace the following algorithmic fragment. Show all working in a trace table.
NUM = 5
loop until NUM = 1
output NUM
if NUM mod 2 = 0 then
NUM = NUM / 2
else
NUM = NUM * 3 + 1
end if
end loop
output NUM
Experiment with different values of NUM, and find what is the meaning of the code?
---------------------------------------------------------------------------