April 2000
CA208 : 'C' PROGRAMMING

QUESTION 1 (Compulsory)

Total Marks: 30 Marks

Click here to access other questions

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

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)(i)Describe the purpose of a storage class modifier in C,quoting an
example.[2 marks ]
•A storage class modifier is used to alter the way the storage is created (1 mark)
•Any one of const, extern, static, volatile, auto or register (1 mark
)
(ii)Describe the purpose of a type modifier in C,quoting an
example.[2 marks ]

•A type modifier is used to modify the particular type of the data involved. (1 mark)
•Any one of signed unsigned long or short (1 mark)

(b)(i)Give a declaration for an array of 5 integers,with all the elements initialised to 0.[2 marks ]
The declaration should read as follows:
int MyArray[5]= {0, 0, 0, 0, 0};
And the following marking scheme should be used:
•A correct declaration;(1 mark)
•A correct initialisation.(1 mark)

(ii)Declare a two dimensional array of characters,of size 2 ×2,initialised with the
values a, b, c, d [2marks ]

The declaration should read as follows:
char MyArray[2][2]= {’a’, ’b’, ’c’, ’d’};
And the following marking scheme should be used:
•A correct declaration;(1 mark)
•A correct initialisation.(1 mark)

(c)Assuming x y and z are all declared as int state the value of z in the following
expressions,given the values of x and y
(i)z =x && y where x= 65535 and y= 0 [1mark ]
(ii)z =x && y where x= 65535 and y= 0 [1mark ]
(iii)z =x << y where x= 3 and y= 2 [1mark ]
(iv)z =x >> y where x= 3 and y= 2 [1mark ]

(i)0 [1 mark ]
(ii)1 [1 mark ]
(iii)0 [1 mark ]
(iv)12 [1 mark ]

(d)Give the result of applying the arithmetic operators in the following expressions,
when x= 4 y= 8 and z= 12
(i)(z % x)+y [1 mark ]
(ii)(y + z)%x [1 mark ]
(iii)y* = x [1 mark ]
(iv)y* = ++x [1 mark ]

(i)8 [1 mark ]
(ii)0 [1 mark ]
(iii)32 [1 mark ]
(iv)40 [1 mark ]

(e)Give the output of the procedure FooBar the definition of which is given below:
void foobar() {
  int *x, *y;
  int a[4]= {1, 2, 3, 4};

  x= &a[1];
  y= a;
  *(x-1)= 5;
  *y= 6;
  x++;
  y= (++y)+1;
  (*y)++;
  x= x+2;
  printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]);
}
[4 marks ]

The output is as follows:
6 2 4 4
And one mark should be awarded for each correct item.[4 marks ]

(f)Write a procedure,called CharCount the signature of which is given below,which takes in three parameters,a string s, a character c and an index i
The procedure should calculate how many times c appears in s and return this value as the index i
You may not use any C library functions.
void CharCount(const char s[], const char* c, int* i); [4 marks ]

A sample definition of CharCount follows:
void CharCount(const char s[ ], const char* c, int* i)
{
    *i= 0;
    while (*s!= ’\0’) {
      if(*s == *c) (*i)++;
      s++;
   }
}
And the following marking scheme should be used:
•Ensuring the value of the index is reset to zero;(1 mark)
•Declaring a suitable loop to iterate over the string;(1 mark)
•With a suitable test to ensure the endof the string has not been reached; (1 mark)
•Incrementing the index if the characters are equal.(1 mark) [4 marks ]

(g)Given the following definition of a simple linked list in C,define a recursive function, called search which takes three parameters,a linked list l, a value v and an index i. The function should return a reference to the the first element in the list which matches the value passed in,and the index i should indicate how many list elements have been traversed.

typedef struct node {
  struct node* next;
  int datum;
} list_element;

list_element *front, *back;
[6 marks ]

A sample definition of search follows:
list_element* search(const list_element* l, int v, int* i)
{
   list_element* result;

   *i= (*i)++; if(l== NULL) {
      return NULL;
   } else {
      if(l->datum== v) {
         return l;
}else {
         return search(l->next, v, i);
      }
   }
}
And the following marking scheme shouldbe used:
•Declaring an appropriate function signature;(1 mark)
•Declaring a suitable conditional structure to ensure the base case (where l->next == NULL has not been reached;(1 mark)
•Incrementing the index on each recursion;(1 mark)
•Testing the current list element to see if the value matches;(1 mark) •Returning a reference to it if it does;(1 mark)
•Recursing with the next list element if it does not;(1 mark) [6 marks ]