August
1997 QUESTION 3 Total Marks: 20 Marks |
Click here to access other
questions
SUGGESTED SOLUTIONS |
3. | (a) Describe the relationship between a pointer and an array. | [3] | ||||||||
A variable declared as an array can be manipulated as a pointer | [1] | |||||||||
to the first element of the array. | [1] | |||||||||
A pointer is not necessarily an array. | [1] | |||||||||
(b) Describe the difference between call by reference and call by value parameter passing. | [2] | |||||||||
If a formal parameter of a function or procedure is passed via a call-by-reference parameter passing scheme, then any changes to that variable during the lifetime of the procedure invocation will carry through after the procedure has returned. | [1] | |||||||||
In contrast, changes to variables which are passed using the call-by-value scheme are only retained during the lifetime of the procedure/function invocation. | [1] | |||||||||
(c) Write a procedure called replace that takes two integer arguments, called sum and diff. It should change the values of these two variables so that the value of sum on exit is equal to the sum of the two values on entry, and the value of diff on exit is equal to the difference between the two values on entry. | [5] | |||||||||
Two definitions of the replace procedure are shown below: | ||||||||||
![]() |
||||||||||
and the following marking scheme should be used: | ||||||||||
two marks for the procedure declaration. If the declaration just contains int sum, int diff, then award no marks, but mark the points below as though any references to sum where actually *sum. | ||||||||||
one mark for declaring a temporary variable and assigning *sum or *diff to it. | ||||||||||
If the temporary variable contains the value for *sum | ||||||||||
then award one mark incrementing *sum by *diff. | ||||||||||
one mark for subtracting *diff from temp, and assigning the result to *diff. | ||||||||||
If the temporary variable contains the value for *diff | ||||||||||
one mark for subtracting *sum from *diff, and assigning the result to *diff. | ||||||||||
then award one mark incrementing *sum by temp. | ||||||||||
no marks should be deducted for trivial syntactic errors. | ||||||||||
[5 marks] | ||||||||||
(d) Trace the following code segment and produce the output: | [10] | |||||||||
Award one mark
for each of numbers in the output below. Do not deduct marks if the answers do not include
the accompanying text such as first =.
|
||||||||||
|