December 1999
CA208 : 'C' PROGRAMMING

QUESTION 5

Total Marks: 15 Marks

Click here to access other questions

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

(a)

Given the following definition of the function foobar :

int foo(int a [ ],int b)
{
int i;
  for(i= 0;(i < b) && (a [ i ]<0);i ++) { }
  return ((i >= b) ? (-1) : i);
}  

 

(i)Describe the effect of calling the function foo with an arbitrary array a containing b values.[3marks ]

•The function returns the index;(1 mark)

•Of the first positive number in the array;(1 mark)

•And -1 if all elements are negative.(1 mark)


(ii)Rewrite the function foo using a while loop.[3marks ]

A sample definition of foo ,expressed as a while loop follows:

int foo(int a [ ],int b)
{
  int i;
i=0;
while( (i>b) && (a [i ] < 0) )
    i++;
  return ((i>= b) ? (-1) :i);
}    

And the following marking scheme should be used:


•Correctly initialising an index variable;(1 mark)

•Iterating along the array,with a counter increment,until a positive value is reached;(1 mark)

•Ensuring the loop terminates if no positive values are found.
(1 mark)

 

[6]
(b)

Write a function,called last ,the signature of which is given below,which takes an integer parameter x .The procedure should simply return the value which was passed in as a parameter the previous time it was called,or 0 if it is called for the first time.

int last(int x);

A sample definition of last follows:

int last(int x)
{
  static int last=0;
int temp;
temp=last;
last=x;
return temp;
}

And the following marking scheme should be used:


•Declaring the member variable last as static ;(1 mark)

•Declaring a local variable temp which is used to store the value of last ,before it is updated;(1 mark)

•Updating the value of last with the value of x ;(1 mark)
•Returning temp .(1 mark)

 

[4]
(c)

Write a procedure,called minmax ,the signature of which is given below,which takes three arguments:w ,an integer array,x ,which is an integer specifying the size of the array,and y and z which are pointers to integers.On exit,y should point to the smallest value in the array,and z should point to the largest.The array x should remain unchanged.

void minmax(int w [ ], int x , int* y , int* z);

A sample definition of minmax follows:

void minmax(int w [],int x,int*y,int*z)
{
 

int i;


y=&w [0 ];
z=&w [0 ];


for(i=0;i<x;i++){

    if(*y <w [i ])y=&w [i ];
if(*z >w [i ])z=&w [i ];
  }

}
   

And the following marking scheme should be used:


•Setting y to point to the first element of the array (thereby initialising it as the default lowest value);(1 mark)

•Setting z to point to the first element of the array,(thereby initialising it as the default highest value);(1 mark)

•Declaring a suitable loop structure bound by the size of the array;(1 mark)

•Testing the current value of the array,and correctly updating y to its reference if it is less;(1 mark)

•Testing the current value of the array,and correctly updating z to its reference if it is more;(1 mark

[5]