Do not award
half marks. Do not deduct marks for trivial syntactic errors. Alternative
correct answers should be given credit, unless otherwise indicated
in the marking scheme.
(a)Consider the function foo the definition of which is given below:
int foo(int x[], int n) {
int i;
for(i= 0; (i< n) && (x[i]>= 0); i++)
{}
return((i>= n)?(-1):i);
}
(i)Describe the results of evaluating foo when x is the array x= {1,
2, 3, 4,
5, -1} and n= 6 [1mark ]
(ii)Describe the results of evaluating foo when x is the array x=
{-1, 0, 1, 2,
3, 4} and n= 6 [1mark ]
(iii)Describe the results of evaluating foo when x is the array x=
{1, 2, 3, 4,
5, -1} and n= 3 [1mark ]
(iv)Describe the effect of calling foo on an arbitrary array x containing
n
values.[3 marks ]
(i)6 [1 mark ]
(ii)1 [1 mark ]
(iii)-1 [1 mark ]
(iv) The function foobar returns the index of the first number
in the array less than zero;(1 mark)
Of the first number in the array less than zero;(1 mark)
If all the elements are greater than 0,-1 is returned.(1 mark)
[3 marks ]
(b)Write a function,called SumTotal the signature of which is given
below,which takes a single integer parameter n and returns the sum
total of all the values of n passed to SumTotal in the life of the
program.You should ensure that the initial value of the total is correctly
initialised, and your function may not refer to any global variables.
int SumTotal(int n); [3 marks ]
A sample definition
of SumTotal follows:
int SumTotal(int n) {
static int sum= 0;
return sum+n;
}
And the following marking scheme should be used:
Declaring a local v static variable for the sum;(1 mark)
Initialising this value to 0;(1 mark)
Returning the value of sum n (1 mark)
[3 marks ]
(c)Write a rocedure, called Sort the signature of which is given
below,which takes references to three integers, a b and c On exit,the
three integers should be sorted such that a is the largest, and c
is the smallest.The procedure should also print out the integers in
their sortedorder.
void Sort(int* a, int* b, int* c); [6 marks ]
A sample definition
of Sort follows:
void Sort(int* a, int* b, int* c) {
int temp;
if(*a < *b) {
temp= *a;
*a = *b;
*b= temp;
}
if(*a < *c) {
temp= *a;
*a = *c;
*c= temp;
}
if(*b < *c) {
temp= *c;
*c = *b;
*b= temp;
}
printf("a= %d; b= %d; c= %d\n", *a, *b, *c);
}
And the following marking scheme shouldbe used:
Declaring a temporary variable to use for swapping numbers;(1
mark)
Correctly dereferencing parameters and pointers;(1 mark)
Testing *a and *b are correctly ordered;(1 mark)
Testing *a and *c are correctly ordered;(1 mark)
Testing *b and *c are correctly ordered;(1 mark)
An appropriate print statement at the endof the procedure.
[6 marks ]
|