August 1997
CA208: 'C' PROGRAMMING

QUESTION 1 (Compulsory)

Total Marks: 20 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to Question 1

1. (a) Create appropriate declarations for each of the following variables:
Definitions of various C types:
(i) digits, an array of 10 integers. [1]
int digits[10]; [1]
(ii) rates, an array of 6 floats. [1]
float rates[6]; [1]
(iii) apc, an array of 20 pointers to characters. [1]
char *apc[20]; or char *(apc[20]); Note: the [ ] have higher precedence that *, so in the absence of parentheses, the array descriptor is applied first then the pointer descriptor. [1]
(iv) pacc, a pointer to an array of 20 characters [2]
char (*pacc)[20]; or char pacc[ ] [20]; [2]
(b) What is printed when the following program is executed? [4]

The following will be printed:
p1 is 2 [1]
p2 is 30 [1]
p3 is 30 [1]
p4 is 3 [1]
If the students omit the p1 is . . ., but give the correct numerical values of p1, etc., then award the marks as above.
(c) The following function is supposed to find the average mark from an array of n marks in the range 0 to 100. Find, and correct the seven errors in its definition: [7]

Errors in the definition of averageMark:
real is not a valid type in C. It should be float or double. [1]
sum is not initialised to zero. [1]
Commas in the for statement instead of semi-colons. [1]
The auto-increment should be written as += and not =+ [1]
The starting index for the for-loop should be 0, [1]
and the termination condition for the for-loop should be i<n. [1]
Alternatively, give 2 marks if the student changes the use of the index to marks [i - 1].
The coercion double should be in brackets. [1]
Therefore a program such as the above below should be awarded seven marks:
(d) Write a function istriangle that takes three integers as its arguments, each representing the length of one of the sides of a triangle. The function should return 1 if the numbers form a valid triangle and 0 otherwise. Hint: the sum of any two sides must be greater than the third side. [4]
The students may have produced any of the following answers for the definition of istriangle. Give full marks (4 marks) if they use either the first or second definitions shown below. A maximum of three marks can only obtained if their answers are similar to the third and fourth function below.
(i) Possible answer one:
[4 marks]
(ii) Possible answer two:
[4 marks]
(iii) Possible answer three:

[3 marks]
(iv) Possible answer four:
[3 marks]