0% found this document useful (0 votes)
40 views

Coding Practice 1

Uploaded by

Muhammad Haseeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Coding Practice 1

Uploaded by

Muhammad Haseeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

Construct an algorithm to illustrate the monthly process described above.


(write algorithm only, not pseudocode)
---------------------------------------------------------------------------
3
Print jobs are sent to a shared printer from all workstations and added to the
print queue in the order in which they are sent. A priority is given to each job
based on the number of pages requested.
• The highest priority (1) is given to jobs with 1–3 pages.
• The lowest priority (4) is given to jobs with more than 50 pages.
The jobs sent to the printer are held in a collection of objects. Each object
includes the priority that has been given and the time it was sent to be printed.
If any job has been waiting more than 10 minutes it is moved to the front of the
queue and is the next to be printed.

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?
---------------------------------------------------------------------------

You might also like