December 1998
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

 

(a) Name two type modifiers other than unsigned. [2]
Any two of the following: signed, long or short.

 

(b) Name two storage class modifiers other than static. [2]
Any two of the following: extern, auto, register, const, or volatile.

 

(c) Describe what a structure and a union are, and identify the difference between the two. [3]
A structure is an ordered fixed size sequence of named objects (1 mark). A union is an overlapping sequence of named objects (1 mark). The difference between the two is the size of a structure is the sum of the sizes of its constituent parts, and the size of a union is the size of the largest part (1 mark).

 

(d) Create appropriate declarations for each of the following variables:
(i) digits, an array of 10 unsigned integers. [1]
unsigned int digits[10];

 

(ii) 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.

 

(iii) pacc, a pointer to an array of 20 characters. [2]
char (*pacc)[20]; or char pacc[][20];

 

(e) Write a procedure maxpair that takes two integer valued parameters x and y as its arguments. The effect of the procedure should be to change the values of x and y so that x holds the larger of the two values, and y the smaller. [4]
Two definitions of the maxpair procedure are shown below:

void maxpair(int *x, int *y) {
   int temp;

   if (*x < *y) {
      temp = *x;
      *x = *y;
      *y = temp;
   }
}

void maxpair(int *x, int *y) {
    int temp;

    if (*y > *x) {
       temp = *y;
       *y = *x;
       *x = temp;
    }
}

and the following marking scheme should be used:

  • one mark for the procedure declaration. If the declaration just contains int x, int y, then award no marks, but mark the points below as though any references to x where actually *x.
  • one mark for initiating a swap if the first argument is smaller than the second.
  • one mark for assigning one of the arguments to the temporary variable.
  • one mark for the correct assignments to *x and *y.
  • no marks should be deducted for trivial syntactic errors.

 

(f) Trace the following code segment and produce the output:

void main(void) {
     int num_list[5] = (1,2,3,4,5);
     int i, *one, *two;

     one = &num_list[1];
     two = num_list;
     *(one-1) = 42;
     *two = 66;
     printf("First = %d\n", *one);
     printf("Second = %d\n", num_list[0]);

     one = one + 3;
     two = (++two) + 1;
     printf(""Third = %d\n", *one);
     printf("Fourth = %d\n", *two);

     for(i=0; i<5; i++) printf("%d ", num_list[i]);
}

 

[5]
One mark for the correct numbers appearing on each line below:

First = 2
Second = 66
Third = 5
Fourth = 3
66  2  3  4  5

Do not deduct marks for incorrect typesetting of the answer. i.e., if the second answer is written as Second = 66 instead of Second = 66, then marks should still be awarded.