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

(a)

(i)Name two type modifiers other than unsigned .[2 marks ]

Any two of the following,one mark for each:signed ,long or short .

(ii)Name two storage class modifiers other than volatile .[2 marks ]

Any two of the following,one mark for each:extern ,auto ,register ,static or const .

 

[4]
(b)

(i)Declare a two dimensional array of integers,called fb ,which is of size 3 ×3,and initialise it with the values 1 through to 9.[2 marks ]

int fb [3 ][3 ]=1,2,3,4,5,6,7,8,9;
(1 mark) for the array declaration, and (1 mark) for the initialisation.

(ii)Declare a dynamic array of floating point integers,called floaters ,which may have values implicitly changed and therefore should not be optimized.The size of the array should only be decided at allocation time.[2 marks ]

volatile float**floaters;
(1 mark)for the use of volatile ,and (1 mark)for the actual array
declaration.

 

[4]
(c)

What is the result of applying the bitwise operators in the following expressions?

(i)7 & 7 [1 mark ]

7


(ii)7 | 7 [1 mark ]

7


(iii)1 » 1 [1 mark ]

0


(iv)1 « 1 [1 mark ]

2

 

[4]
(d)

Give the result of applying the arithmetic operators in the following expressions, when x =3,y =5 and z =7:

(i)(x+y*z) [1 mark ]

38


(ii)(x+y)%z [1 mark ]

1


(iii)(++x*y) [1 mark ]

20


(iv)(--x+ ++y) [1 mark ]

8

 

[4]
(e)

Given the following definition of the procedure foobar :

void foobar(int state,int timeout){
  switch(state){
  case 1:
    if(timeout <0 ){
      printf("Suspend task;");
      break;
    }
  case 2:
    printf("Running task;");
    break;
  default:
    printf("Halt task;");
  case 3:
    printf("Delete task;");
  }    
}      

Trace the following calls and produce their output:

(i)foobar(1,-1);[1 mark ]

Suspend task;


(ii)foobar(1,0);[1 mark ]

Running task;


(iii)foobar(3,0);[1 mark

Delete task;


(iv)foobar(5,0);[1 mark ]

Halt task;Delete task;

 

[4]
(f)

Write a procedure,called Exchange ,the signature of which is given below,which takes in three integer parameters,x ,y and z by reference.On exit,the value of z should be equal to the sum of the other two parameters,and the other two parameters should be exchanged.

void Exchange(int*x,int*y,int*z);

A sample definition of Exchange follows

void Exchange(int*x,int*y,int*z)
{
  int temp;

*z=*x +*y;
temp=*x;
*x=*y;
*y=temp;
}  



And the following marking scheme should be used:


•Correctly de-referencing pointers when doing the arithmetic;
(1 mark)

•Summing *x and *y to give *z ;(1 mark)

•Declaring a local temporary variable,and assigning one of the values to it; (1 mark)

•Swapping the two values using the temporary variable.(1 mark)

 

[4]
(g)

A Fibonacci sequence is a sequence of numbers where the next number in the sequence is calculated by the sum of the previous two numbers.For example,a sequence which started with the numbers (1,2)would have as its third entry 3 (calculated by 1+2).The next number would be 5 (calculated by 2+3)and so on.

Write an iterative procedure,called Fibonacci ,the signature of which is given below, which takes three parameters,x ,y and z .The procedure should use x and y as the initial two entries in the sequence,and print out the next z entries. void Fibonacci(int x,int y,int z);

A sample definition of Fibonacci follows:

void Fibonacci(int x,int y,int z)
{

int first,second,third;


first=x;
second =y;
while(z!=0){

    third=first +second;
printf("%d \n",third);
first=second;
second=third;
z--;
  }  
}    

And the following marking scheme should be used:


•Declaring three local variables,first ,second and third and initialising first and second with the values of x and y respectively;(1 mark)

•Declaring a suitable iterative structure,bounded by z iterations;
(1 mark)

•Calculating the value of third on each iteration;(1 mark)
•Printing out this value;(1 mark)

•Correctly updating the value of first with the current value of second on each iteration;(1 mark)

•Correctly updating the value of second with the value of third .
(1 mark)

[6]