(a)
(i) Declare an integer variable, a, defined elsewhere in the program
code. [1 mark]
(ii) Declare a pointer, ptr which points to an array of five integers.
[1 mark]
(iii) Declare a constant, pi, which has a value of 3.142. [1 mark]
(iv) Define a data type, String, which allows arrays of characters
up to 30 long to be stored. [1 mark]
(a) (i) extern int a; [1 mark]
(ii) int (*ptr)[5]); [1 mark]
(iii) const float pi=3.142; [1 mark]
(iv) typedef char String[30]; [1 mark]
(b) (i) Briefly describe the storage
allocation of a struct. [2 marks]
(ii) Briefly describe the storage allocation of a union. [2 marks]
(b) (i) Each member has its own memory
allocated; (1 mark)
The size of the struct is the sum of the sizes of the members.
(1 mark)
[2 marks]
(ii) The members share overlapping memory; (1 mark)
The size of the union is the size of the largest member. (1
mark)
[2 marks]
(c) State the result of applying the
arithmetic operators in the following expressions, given x= 5, y=
10 and z=15, in each statement:
(i) x * y; [1 mark]
(ii) z % x; [1 mark]
(iii) ++x * x; [1 mark]
(iv) x++ + --z; [1 mark]
(c) (i) 50 [1 mark]
(ii) 0 [1 mark]
(iii) 36 [1 mark]
(iv) 19 [1 mark]
(d) State the result of applying the
bitwise operators in the following expressions, given x= 23, y= 115
and z=2, in each statement:
(i) x & y; [1 mark]
(ii) x | y; [1 mark]
(iii) x y; [1 mark]
(iv) x >> z; [1 mark]
(d) (i) 7 [1 mark]
(ii) 31 [1 mark]
(iii) 24 [1 mark]
(iv) 5 [1 mark]
(e) Explain, step by step, with reference
to the action of each statement in the source code, the effect of
calling the following procedure with two integer reference parameters.
void FooBar(unsigned int* x, unsigned int* y)
{
*x += *y;
*y = *x - *y;
*x -= *y;
}
[4 marks]
(e) The procedure FooBar
Adds the value stored in y to the one stored in x, keeping
the result in x; (1 mark)
Then stores in y the result of subtracting the value in y from
that in x; (1 mark)
Finally it subtracts the value in y from that in x, storing
the result in x; (1 mark)
The effect is that the values stored in x and y are swapped.
(1 mark)
[4 marks]
(f) Implement an iterative function,
called Divide, the signature of which is given below, which takes
in two integer parameters x, y and a reference parameter z. On exit,
z should reference the remainder when x is divided by y, and the function
should return the result of the division. You may not use either the
division or modulo operators.
int Divide(int x, int y, int* z); [5 marks]
(f) A sample definition of Divide follows:
int Divide(int x, int y, int* z)
{
int counter= 0;
*z= 0;
do {
x= x- y;
counter++;
} while(x>= y);
(*z)= x;
return counter;
}
And the following marking scheme should be used:
Initialising the correct values of (*z) and counter; (1 mark)
Declaring a loop bound by a condition appropriate to the calculation
inside; (1 mark)
Ensuring the correct values are calculated inside the loop;
(1 mark)
Returning the correct value of the divider; (1 mark)
Returning the correct value of the remainder. (1 mark)
[5 marks]
(g) Implement a recursive procedure,
called ShowBinary, the signature of which is given below, that takes
in a single integer value, and prints out the binary representation
of that integer.
void ShowBinary(int x); [5 marks]
(g) A sample definition of ShowBinary is
given below:
void ShowBinary(int x)
{
int remainder;
remainder= x%2;
if(x!=0) {
ShowBinary(x/2);
printf("%d", remainder);
}
}
And the following marking scheme should be used:
Calculating the remainder using the mod operator; (1 mark)
Recursing while x!= 0; (1 mark)
Recursing when the base case has not been reached; (1 mark)
With the correct x\2 argument; (1 mark)
Printing out the remainder after each recursive step; (1 mark)
[5 marks]
|