CA208 1997 Term 3 (Set 2)
Question 1.
Compulsory
a. What is the index range for a 100 element array? [2 marks]
b. Declare an array to store a name of 30 characters. [2 marks]
c. We suspect the following program has errors. Identify and
correct the errors.
#include<stdio.h>
main( )
{
int i,j,list(10);
for(i=l,i<=lO,i++)
{
list[i] = 2*I+3;
for(j=l,j>=i,j++)
printf("`d"\n,list[j]);
}
} [8 marks]
d. A function prototype is also called a function . [2 marks]
e. Under what circumstances a variable can be used as a pointer when it is not declared a pointer? [2 marks]
f. What is the difference between a structure and a union? [4 marks]
Question 2.
Assume that user key 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.
a. Declare a nested structure for the following
information:
Student information
Student name: Michael J. Fox
Student no. : nl611763
Address
State: Chicago
Street : Queens Street
House no: 21
Sex: M
Weight: 65.5kg
Height: 175.5cm [12 marks]
b. Initialise the structure above to store the given information using the assignment operator and the strcpy function.
[8 marks]
Question 4.
a. What is the difference between passing by reference and passing by value?
[4 marks]
b. Write a swap function of nu mb er x and y using pass by value. [6 marks]
c. Write a swap function of nu mb er x and y using pass by reference. [6 marks]
d. What are the two operators associated with pointers? What do they mean?
[4 marks]
Question 5.
a. Rewrite the following using the while loop and the do
while loop.
for ( i=O; i<10; 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.
if (x==10) printf("Full marks");
else if (x==5) printf("Just pass");
else if (x<5) printf("Fail");