August 2000
CA208 : 'C' PROGRAMMING

QUESTION 1 (Compulsory)

Total Marks: 30 Marks

Click here to access other questions

Click to access
SUGGESTED SOLUTIONS
for Question 1

(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]

(b) (i) Briefly describe the storage allocation of a struct. [2 marks]
(ii) Briefly describe the storage allocation of a union. [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]

(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]

(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]

(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]

(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]