April 2000
CA208 : 'C' PROGRAMMING

QUESTION 5

Total Marks: 15 Marks

Click here to access other questions

Click to access
SUGGESTED SOLUTIONS
for Question 5

(a)Consider the function foo the definition of which is given below:
int foo(int x[], int n) {
   int i;
   for(i= 0; (i< n) && (x[i]>= 0); i++) {}
      return((i>= n)?(-1):i);
}
(i)Describe the results of evaluating foo when x is the array x= {1, 2, 3, 4,
5, -1} and n= 6 [1mark ]
(ii)Describe the results of evaluating foo when x is the array x= {-1, 0, 1, 2,
3, 4} and n= 6 [1mark ]
(iii)Describe the results of evaluating foo when x is the array x= {1, 2, 3, 4,
5, -1} and n= 3 [1mark ]
(iv)Describe the effect of calling foo on an arbitrary array x containing n
values.[3 marks ]

(b)Write a function,called SumTotal the signature of which is given below,which takes a single integer parameter n and returns the sum total of all the values of n passed to SumTotal in the life of the program.You should ensure that the initial value of the total is correctly initialised, and your function may not refer to any global variables.
int SumTotal(int n); [3 marks ]

(c)Write a rocedure, called Sort the signature of which is given below,which takes references to three integers, a b and c On exit,the three integers should be sorted such that a is the largest, and c is the smallest.The procedure should also print out the integers in their sortedorder.
void Sort(int* a, int* b, int* c); [6 marks ]