Download The Currency Handler Service

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Lattice model (finance) wikipedia , lookup

Quadtree wikipedia , lookup

Interval tree wikipedia , lookup

Linked list wikipedia , lookup

Binary search tree wikipedia , lookup

B-tree wikipedia , lookup

Transcript
CS1210 Data Structures and Algorithms
1. This question refers to the package Deque_dss listed in Appendix I.
a) Package Deque_dss can be described as a ‘software service’. Describe TWO ways in
which software services (whether they are provided in the form of packages or
otherwise) can facilitate the process of program design.
(4 marks)
b) Suppose we have:
adeq : Deque;
IN YOUR OWN WORDS AND DATA STRUCTURE DIAGRAMS explain how the
data structuring scheme in package Deque_dss works as a model of deques (doubleended queues). Do this by using the following as an illustrative example: the variable
adeq above models a deque which comprises (in order) the three upper-case character
elements 'D', 'S' and 'A'.
(6 marks)
c) Using the data structure scheme in package Deque_dss, code up fully as an Ada
subprogram the deque operation AddRear. This operation adds a new element (call it
e) to the rear of a deque (call it dq). Use only basic Ada instructions, ie, do NOT
make use of any other subprograms in AddRear’s algorithm.
(6 marks)
d) Suppose every deque is required to have a name, this being a string of 16 characters.
Explain how the data structure scheme of package Deque_dss could be modified to
incorporate this extra feature of deques. Write down in Ada all necessary
modifications to, and extra declarations needed by, the scheme.
(4 marks)
Page 1 of 9
CS1210 Data Structures and Algorithms
2. a) Define the concept of a set.
(3 marks)
b) One way of modelling a set in a program is to use an array-based data structure
scheme where the array cells are used to hold the set elements. Describe such a
scheme by writing down an appropriate collection of Ada declarations.
(4 marks)
c) An alternative way of modelling a set is to use the following scheme:
SUBTYPE Elem IS … … …; -- the set element (sub)type
TYPE Set IS ARRAY(Elem) OF Boolean;
BRIEFLY explain in your own words and diagrams how this scheme works.
(NOTE: do NOT explain the workings of any set operations here.)
(4 marks)
d) Here is the heading of an operation that adds an element to a set:
PROCEDURE AddElem(e : IN Elem; s : IN OUT Set);
i) For the scheme in part (b) give an OUTLINE ALGORITHM for AddElem.
(4 marks)
ii) For the scheme in part (c) give the COMPLETE ADA CODE for AddElem.
(2 marks)
e) State ONE reason why scheme (c) is preferable to scheme (b) for modelling sets.
State ONE circumstance under which we would be forced to use scheme (b) rather
than scheme (c).
(3 marks)
Page 2 of 9
CS1210 Data Structures and Algorithms
3. a) State TWO advantages of using packages to provide a software service, rather than
the latter being just ‘open’ code, stored in a file, that is copied and pasted into
programs.
(4 marks)
b) It is required to implement a software service for handling ‘traversable’ stacks, or ‘Tstacks’. The handling of a T-stack includes the normal stack operations of Init(ts)
(initialises a T-stack ts to empty), Push(e,ts) (places element e on the top of the Tstack ts), Pop(ts,e) (obtains by removal the top element e of T-stack ts),
depth(ts) (gives the number of elements in T-stack ts), and isEmpty(ts) (tests
whether T-stack ts is empty). However, T-stacks have the following additional
features:
•
A T-stack has an extra component called a ‘cursor’; basically, via the cursor any
T-stack element can be accessed (not just the top element).
•
There are three extra stack operations, all of which involve the cursor component
of a T-stack:
cRead(ts)
returns the element currently accessed by the cursor of T-stack ts
(but raises an exception called Cursor_Error if the cursor is
currently ‘null’)
cDown(ts)
moves the cursor of T-stack ts down one further position in the
stack (but raises the exception Cursor_Error if there is no such
position)
cReset(ts)
updates the cursor so that it accesses the top element of T-stack ts
(if the stack is empty, the cursor is set to ‘null’).
Provide the COMPLETE code of the SPECIFICATION of an Ada package
TStack_ati which provides the above service for handling T-stacks where stack
elements are integers. The package should enforce maximum data type abstraction.
The data structure scheme must utilise a LINKED LIST to hold the T-stack elements.
Do NOT provide any code for the package body.
(13 marks)
c) The TStack_ati package (also the CurrencyHandler_ati package in Appendix II)
can be regarded as an implementation of an abstract data type. ‘Abstract’ data types
are so-called because … why?
(3 marks)
Page 3 of 9
CS1210 Data Structures and Algorithms
4. Here is a linked list scheme for modelling a sequence of characters which utilises a
‘header record’ technique to hold the length of the list:
TYPE Node;
TYPE Pntr_to_Node IS ACCESS Node;
TYPE Node IS RECORD
val : Character;
link : Pntr_to_Node;
END RECORD;
TYPE ChrList IS RECORD
head : Pntr_to_Node;
leng : Natural := 0;
END RECORD;
a) ASSUMING that NO error condition exists whenever the following function is called,
complete the body of this function by supplying the Ada code for the missing
fragments represented by the ‘dots’:
FUNCTION suffix(soc : ChrList; i : Positive) RETURN ChrList IS
sffx : ChrList;
next : Pntr_to_Node := … … …;
BEGIN
FOR j IN … … … LOOP
-- firstly travel to ith. node in 'soc'
… … …;
END LOOP;
sffx.head := … … …;
sffx.leng := … … …;
… … …;
END suffix;
This function returns a fresh list of type ChrList which is a ‘suffix’ of the list soc
and corresponds to the section of soc from the ith node to the end node inclusive.
You may assume the availability of the function copy(ptr). This function takes a
chain of nodes accessed by the Pntr_to_Node pointer ptr and returns the head
pointer to another chain of nodes it has constructed which is an exact copy of the
chain accessed by ptr.
(5 marks)
b) What error condition might function suffix actually encounter? Hence explain what
would happen and why if the assumption in (a) is wrong when function suffix is
called.
(4 marks)
c) Summarise TWO main advantages of signalling errors with exceptions compared to
the more conventional technique of using ‘error flag’ parameters.
(4 marks)
d) Declare, with brief explanation, an exception that would be appropriate for signalling
the error condition that function suffix might encounter. Then show how the code of
suffix would be altered to make use of the exception.
(4 marks)
e) If an error condition does exist, would it make sense for function suffix to print out
an appropriate error message before returning? Explain your answer.
Page 4 of 9
CS1210 Data Structures and Algorithms
(3 marks)
Page 5 of 9
CS1210 Data Structures and Algorithms
5. The first three parts of this question refer to package CurrencyHandler_ati listed in
Appendix II.
a) What general class of abstract data structure does type CurrencyReserves being
modelled in package CurrencyHandler_ati belong to? Briefly justify your answer.
(4 marks)
b) State TWO advantages of using a dynamic data structure scheme like that in package
CurrencyHandler_ati over an equivalent one that is based on (sorted) arrays.
(4 marks)
c) Using the data structure scheme in package CurrencyHandler_ati, construct in Ada
a COMPLETE implementation of the following function:
FUNCTION isHeldIn(cnm : CurrencyName;
crs : CurrencyReserves) RETURN Boolean;
This function determines whether or not currency cnm occurs in the currency reserves
data structure crs, returning True or False as appropriate. Your implementation
should exploit the fact that the data structure being searched is based primarily on a
binary search tree.
NB, if your implementation of isHeldIn involves it calling another subprogram you
must give the COMPLETE code for this routine too.
In addition, state whether or not any part of your implementation of isHeldIn is ‘preorder’, ‘in-order’ or ‘post-order’. Explain your answer.
(7 marks)
d) Imagine a ‘tertiary tree’. A tertiary tree is like a binary tree except that each node has
THREE rather than two subtrees, as the following definition makes clear:
A tertiary tree is:
EITHER
empty
OR
a root element with three subtrees: ‘left’, ‘middle’ and ‘right’;
each subtree is also a tertiary tree.
Write down ONE outline RECURSIVE algorithm scheme (or ‘template’) for
processing tertiary trees based on the above definition. Are there other such schemes
as well as the one you have written? Explain your answer.
(5 marks)
Page 6 of 9
CS1210 Data Structures and Algorithms
6. Answer EACH of the following:
a) Write notes on the main issues concerned with designing programs specifically to test
software services effectively.
(12 marks)
b) Give a step-by-step explanation of what the following code achieves:
PACKAGE Widget_SIO IS NEW Ada.Sequential_IO(Widget);
sftv : Widget_SIO.File_Type;
awdgt : Widget;
… … …
Widget_SIO.Open(file => sftv, mode => Widget_SIO.Out_File,
name => "MyWidgets.dat");
Widget_SIO.Write(file => sftv, item => awdgt);
More generally, under what kinds of circumstance would it be appropriate to use the
type of file that is being handled by this code?
(8 marks)
Page 7 of 9
CS1210 Data Structures and Algorithms
APPENDIX I
The Deque Service
PACKAGE Deque_dss IS
-- THE DATA STRUCTURE MODEL:
-- assume deque elements are upper-case characters
SUBTYPE Elem IS Character RANGE 'A'..'Z';
TYPE Node;
TYPE Pntr_to_Node IS ACCESS Node;
TYPE Node IS RECORD
value : Elem;
link : Pntr_to_Node;
END RECORD;
TYPE Deque IS RECORD
first, last : Pntr_To_Node := Null;
END RECORD;
-- THE VARIOUS DEQUE OPERATIONS:
-- (re)initialises a deque to 'empty'
PROCEDURE Init(dQ : IN OUT Deque);
-- adds an element to the front of a deque
PROCEDURE AddFront(e : IN Elem; dQ : IN OUT Deque);
-- adds an element to the rear of a deque
PROCEDURE AddRear(e : IN Elem; dQ : IN OUT Deque);
-- removes and returns the front element of a (non-empty) deque
PROCEDURE RemFront(dQ : IN OUT Deque; e : OUT Elem);
-- removes and returns the rear element of a (non-empty) deque
PROCEDURE RemRear(dQ : IN OUT Deque; e : OUT Elem);
-- tests whether a deque is empty
FUNCTION isEmpty(dQ : Deque) RETURN Boolean;
-- returns the length of a deque
FUNCTION length(dQ : Deque) RETURN Natural;
TYPE dQElems IS ARRAY(Positive RANGE <>) OF Elem;
-- returns the elements of a deque as an array
FUNCTION elemsOf(dQ : Deque) RETURN dQElems;
-- returns the position of an element in a deque
-- (0 if the element does not occur in the deque)
FUNCTION isAt(e : Elem; dQ : Deque) RETURN Natural;
END Deque_dss;
Page 8 of 9
CS1210 Data Structures and Algorithms
APPENDIX II
The Currency Handler Service
PACKAGE CurrencyHandler_ati IS
-- auxiliaries:
maxNameLen : CONSTANT Positive := 12;
SUBTYPE CurrencyName IS String(1 .. maxNameLen);
-- the main (abstract) type
TYPE CurrencyReserves IS LIMITED PRIVATE;
-- the currency handling operations:
PROCEDURE Init(crs : IN OUT CurrencyReserves);
PROCEDURE Add_Currency(cnm : IN CurrencyName;
qty : IN Positive;
gbr : IN Float;
crs : IN OUT CurrencyReserves);
PROCEDURE Increase_Currency(cnm : IN CurrencyName;
qty : IN Positive;
crs : IN CurrencyReserves);
PROCEDURE Reduce_Currency(cnm : IN CurrencyName; qty : IN Positive;
crs : IN CurrencyReserves);
PROCEDURE Delete_Currency(cnm : IN CurrencyName;
crs : IN OUT CurrencyReserves);
PROCEDURE Alter_Rate(cnm : IN CurrencyName; gbr : IN Float;
crs : IN CurrencyReserves);
FUNCTION qtyOf(cnm : CurrencyName;
crs : CurrencyReserves) RETURN Integer;
FUNCTION gbprOf(cnm : CurrencyName; crs : CurrencyReserves) RETURN Float;
TYPE CurrencyName_List IS ARRAY(Positive RANGE <>) OF CurrencyName;
FUNCTION currenciesOf(crs : CurrencyReserves) RETURN CurrencyName_List;
FUNCTION totalGBP(crs : CurrencyReserves) RETURN Float;
FUNCTION pfSize(crs : CurrencyReserves) RETURN Natural;
-- Two error signals:
IllegalTrans, NameError : EXCEPTION;
PRIVATE
-- The data structure model:
TYPE Node;
TYPE Pntr_to_Node IS ACCESS Node;
TYPE Node IS RECORD
fcName : CurrencyName;
amount : Positive;
GBP_rate : Float;
leftST, rightST : Pntr_to_Node;
END RECORD;
TYPE CurrencyReserves IS RECORD
fcStock : Pntr_to_Node := Null;
nfcHeld : Natural := 0;
END RECORD;
END CurrencyHandler_ati;
END OF EXAMINATION PAPER
Page 9 of 9