December
1999 QUESTION 5 Total Marks: 15 Marks |
Click here to access other
questions
SUGGESTED SOLUTIONS |
(a) |
Given the following definition of the function foobar :
(i)Describe the effect of calling the function foo with an arbitrary array a containing b values.[3marks ] •The function returns the index;(1 mark) •Of the first positive number in the array;(1 mark) •And -1 if all elements are negative.(1 mark)
A sample definition of foo ,expressed as a while loop follows:
And the following marking scheme should be used:
Iterating along the array,with a counter increment,until a positive value is reached;(1 mark) Ensuring
the loop terminates if no positive values are found.
|
[6] | |||||||||||||||||||||||||||
(b) |
Write a function,called last ,the signature of which is given below,which takes an integer parameter x .The procedure should simply return the value which was passed in as a parameter the previous time it was called,or 0 if it is called for the first time. int last(int x); A sample definition of last follows:
And the following marking scheme should be used:
Declaring a local variable temp which is used to store the value of last ,before it is updated;(1 mark) Updating
the value of last with the value of x ;(1 mark)
|
[4] | |||||||||||||||||||||||||||
(c) |
Write a procedure,called minmax ,the signature of which is given below,which takes three arguments:w ,an integer array,x ,which is an integer specifying the size of the array,and y and z which are pointers to integers.On exit,y should point to the smallest value in the array,and z should point to the largest.The array x should remain unchanged. void minmax(int w [ ], int x , int* y , int* z); A sample definition of minmax follows:
And the following marking scheme should be used:
Setting z to point to the first element of the array,(thereby initialising it as the default highest value);(1 mark) Declaring a suitable loop structure bound by the size of the array;(1 mark) Testing the current value of the array,and correctly updating y to its reference if it is less;(1 mark) Testing the current value of the array,and correctly updating z to its reference if it is more;(1 mark |
[5] |