CA208 1997 Term 3 (Set 3)
QUESTION [(Compulsory)
a. Declare a structure to store the following information.
Employeeinfomation
Employee name: Tom Brown
Employee address: 21 Queens Street
Age: 25
Salary : $2,500.00 [5 marks]
b. Declare an array of 5 for the above structure and initialise all attributes
of the 5 copies using the assignment operator or strcpy function. Numbers
may be initialised to 0 and characters to space.
[9 marks]
c. Under what circumstances a variable can be used as a pointer when it is not declared a pointer? [2 marks]
d. What is the difference between a structure and a union?
[4 marks]
QUESTION 2
Assume that a user keys in a number N. Accept the
..
number and write the following functions:
a. Write the main to accept the number N from user and invoke the next 2
functions and print out the results. [10 marks]
b. Write a recursive function that adds from N to 1 [5 marks]
c. Write a recursive function that adds from 1 to N. [5 marks]
QUESTION 3
int num, number[5];
int *iptr;
a. iptr = num;
Is the above statement legal? If it is not, explain
and correct the error.
[3 marks]
b. iptr = &number[0];
Is the above statement legal? If it is not, explain and correct the error.
[2 marks]
c. Is there an alternative to part (b)? Write down the alternative. [2 marks]
d. What is the output of the following segment of code?
int number[] = { 5,10,15,20,25,30}; int *iptr, count;
iptr = &number[2];
*(iptr-2) = 50;
--iptr;
*iptr = 100;
*(iptr+2) = 200;
++iptr = 150;
number[4] = 250;
iptr = iptr + 3;
*iptr = 300;
for( count = 0; count < 6; count ++)
printf("%d",number[count]); [10 marks]
e. What's the difference between *(iptr+2) and iptr += 2?
[3 marks]
QUESTION 4
a. Write a program that simulates the strlen function in
C. [7 marks]
b. Write a program that initialises an array with value from 1 to 10 and copy the content of this array of this array to another array. [13 marks]
QUESTION 5
a. Rewrite the following using the while loop and the do
while loop.
for ( i=0; ic10; i++)
counter++; [9 marks]
b. State the difference between the for-loop and the
while-loop and the difference between the while-loop and the do-while loop.
[4 marks]
c. Rewrite the following using a switch-case statement.
[7 marks]
if (x==10) printf("Full marks");
else if (x==5)
printf("Just passe);
else if (xc5)
printf("Fail");