April
1999 QUESTION 3 Total Marks: 20 Marks |
Click here to access other
questions
GRADE A
|
(a) | Compare and contrast a function and a procedure. | [4] |
Function is a block of
codes which usually return 1 result and have associative data type which determine the
type of data would be return. Function reperence by using its name as part of the
expression or output statement. Procedure is a block of codes which may r\or may not return a result. If it returns result, the result can be 1 or several. Procedure does not have associative data type. Procedure call is a standalone statement. Both procedure and function are define before main module of program and the number of formal parameter and actual pararmeter of both must be the same and the data type must correspond.
|
||
(b) | Identify the differences between call-by-value and call-by-reference parameter passing. | [2] |
Call by value The formal parameter is only the copy of actual data, any changes happen to this parameter won't affect the actual data. Call by reference
|
||
(c) | This question is concerned with the
implementation of routines that examine a simple product database. The database can
contain at most MaxProductRange
records, and each record contains details of the amount of product in stock. The various
database types you should use in your routines are shown below. Const MaxProductRange = 200;
|
|
(i) Define a procedure, StockLevels, that takes as its arguments a database of product information and an integer n (you may assume that n £ MaxProductRange). This procedure should identify the number of product ranges held in the database, and return two results: the product number of the product which has the lowest stock level, and the product number of the product which has the highest stock level. (Remember to define a procedure and not a function.) | [6] | |
Procedure StockLevels
(dtbs : DatabaseType ; Var high : byte; var low = byte; n = integer); Var Max, Min = Byte; i = integer; Begin Max := 0; Min : = 100l For i = 1 to n do Begin IF dtbs [i] > Max then Begin Max : = dtbs [i]; high : = i; end; IF dtbs [i] < Min then Begin Min := dtbs[i]; low := i; end; end; end;
|
||
(ii) The designer of the database would like to know how many of the products are discontinued. The designer decides to use the DatabaseType representation of a database, and uses a stock level of zero to represent a discontinued product line. Explain why this solution is problematic, and describe the effect on the function StockLevels. | [3] | |
This way is
problematic, since we can't distinguish between product with no stock and product
discontinued as both use StockLevel = 0. The effect to function StockLevels is the lowest
stock level always 0. If there is no product with no stock, this function will return the
product number of product discontinued.
|
||
(iii) Define a replacement for the DatabaseType that provides a satisfactory solution to the discontinued item problem. | [2] | |
We can use Stack Level
= -1 for product discontinued, so we can distinguish it from non-stock product.
|
||
(iv) Using your new definition of DatabaseType, define a function CurrentProducts that takes as its arguments a database of stock levels, and returns the number of product ranges that are currently not discontinued. | [3] | |
Function
CurrentProducts ( dtbs : DatabaseType ) = integer; var i = integer; sum : integer; Begin sum : = 0; For i := 1 to MaxProductRange do begin IF dtbs[i] > - 1 then sum : = sum + 1; end; CurrentProducts : = sum end; Const MaxProductRange = 200; type StockLevel = -1..100; DatabaseType = Array[1 .. MaxProductRange] OF StockLevel;
|