December 1998
AP207: ADVANCED PROGRAMMING TECHNIQUES

QUESTION 2

Total Marks: 20 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to Question 2

 

(a) Identify the differences between call-by-value and call-by-reference parameter passing. [2]
Difference between call-by-value and call-by-reference:
  1. Call-by-value makes a copy of the argument passed as a parameter to the routine. Any changes to the parameter within the body of the routine are not reflected in the variable that was passed to the routine.
  2. In comparison, in a call-by-reference parameter passing scheme, the changes will be reflected in the original variable.

 

(b) This question is concerned with the implementation of routines that analyses a simple examination "pass mark" database. The database can contain at most ClassSize records. The various database types you should use in your routines are shown below.

Const ClassSize     = 20;
Type  MarkRange    = 0..100;
      DatabaseType = Array [1..ClassSize] of
                     MarkRange;

 

(i) Define a function averageMark that takes two arguments: a pass mark database; and an integer n (you can assume that n sign.GIF (56 bytes) ClassSize that identifies that the first n elements in the database contain student marks. The result from the function should be a Real that represents the average mark of the first n elements. [5]
A sample defintion of the average mark function is shown below:

Function averageMark(database: DatabaseType; n: Integer): Real;
Var i: Integer;
Begin
   averageMark := 0.0;
   For i := 1 To n Do
     averageMark := averageMark + database[i];
   averageMark := averageMark / n;
End;

and the following marking scheme should be used;

  • one mark for a function header with a return value Real.
  • one mark for the initialisation of the variable used as the return result to zero;
  • one mark for a for-loop that iterates over n values.
  • one mark for incrementing the result variable by each element from the pass mark database.
  • one mark for dividing the sum of the marks by n

do not deduct marks for trivial syntactic errors when marking the above points.

 

(ii) Define a procedure markBand that takes as its arguments a database of marks and an integer n (you can assume that n sign.GIF (56 bytes)ClassSize) that identifies the number of pass marks in the database, and returns two results: the lower mark of a candidate in the examination; and the highest mark (remember to define a procedure and not a function). [6]
A sample definition of the mark-band function is shown below:

Procedure markBand(database: DatabaseType; n: Integer;Var low: Integer; Var high: Integer);

Var i: Integer;
Begin
   low :=  100;
   high := 0;
   For i : = 1 To n Do
   Begin
      If database[i] >= high Then
         high := database[i];
      If database[i] <low Then
         low := database[i];
   End;
End;

and the following marking scheme should be used:

  • one mark for the procedure header that contains a DatabaseType and an integer with no return type (i.e., the definition of a procedure not a function).
  • one mark for correct use of Var in header.
  • one mark for initialising low to 100
  • one mark for initialising high to   0
  • ... alternatively, two marks for initialising low and high both to database[1].
  • one mark for setting high when the ith element in the database is greater than it.
  • one mark for setting low when the ith element in the database is less than it.

do not deduct marks for trivial syntactic errors when marking the above points.

 

(iii) The designer of the database would like to know may many "no-shows" there were for an examination. The designer decides to use the DatabaseType representation of a database, and uses a pass mark of zero to represent a no-show in the examination. Explain why this solution is problematic, and describe the effect on the two functions averageMark and markBand. [3]
No shows:
  • No shows are problematic as there is no way of distinguishing between a candidate who scored no marks, and one that didn't attend the examination.
  • This will effect the averageMark function by depressing the average (because the average of more students marks will be considered).
  • This will effect the markBand function, as the lowest mark will always be zero.

 

(iv) Define a representation for the DatabaseType that provides a satisfactory solution to the no-show problem. [1]
The students may have used a record type as the database element

Type MarkRecord = RECORD
                    done_exam : Integer;
                    mark : markRange
                  END

Alternatively, they may have altered MarkRange from -1 to 100, and used -1 as the representation of a non-show (also award 1 mark).

 

(v) Using your new definition of DatabaseType, define a function candidates that takes as its arguments a database of marks, and returns the number of students who attempted the examination. [3]
Given a suitable replacement for the type above, then award 1 mark for the function definition, one mark for the for-loop used in the function candidate, one mark for the conditional increment of a counter depending upon whether the student sat the examination.