April 1999
CA208: 'C' PROGRAMMING

QUESTION 1

Total Marks: 20 Marks

Click here to access other questions

GRADE A
Sample student's solutions are indicated in green.
Return to Question 1

 

(a) Name two type modifiers other than signed. [2]
long and unsigned

 

(b) Name two storage class modifiers other than const. [2]
extern and static

 

(c) Create appropriate declarations for each of the following variables:
(i) digits, an array of 15 short integers. [1]
(ii) decimals, an array of 5 floats which may have their value implicitly changed, and therefore should not be optimized. [1]
(iii) apc, an array of 20 pointers to characters. [1]
(i) short digits[15];

(ii) volatile float decimals [5];

(iii) char *apc[20[;

 

(d) Trace the following code segment and write down the output produced:

void main(void) {
   printf("6&3 = %d\n", 6&3 );
   printf("6&&3 = %d\n", 6&&3 );
   printf("6|3 = %d\n", 6|3 );
   printf("6||3 = %d\n", 6||3 );
}

[4]
Output:

6&3=2
6&&3=1
6|3=7
6||3=1

 

(e) Trace the following code segment and write down the output produced:

void main(void) {
   int a = 5;
   int b = 2;
   int c = 1;

   printf("b+c*a = %d\n", b+c*a);
   printf("(b+c)*a = %d\n", (b+c)*a);
   printf("++b +c*a = %d\n", ++b +c*a);
   printf("(b++ +c)*a = %d\n", (b++ +c)*a);

}

[4]
Output:
b+c*a=7
(b+c)*a=15
++b+c*a = 8
(b++ +c)*a = 20

 

(f) Informatics requires a function which will calculate the average mark for a group of students in an exam. Marks are all whole numbers in the range 0..60, and are stored in integer arrays.

Write a function, which builds on the prototype given below, which takes two parameters - the integer array and a specifier representing the size of an array - and will return the average mark of all those stored in the array. Note that although the marks which are passed into the array are all whole numbers, this will not necessarily be the case for the average.

    double averageMark(int marks[], int size);

[5]
double averageMark(int marks[ ], int size)
{

double average;
int i, sum =0;
for(i=0; i<size;i++)
            sum+=marks[i];
average = sum/size;
return average;

}