Professional Documents
Culture Documents
HH
HH
DURATION: 3 HOURS
SECTION B
Question 1
a. Write a program that will find the sum of even numbers from 1 to 250 and output the sum
and average. (10 marks)
b. Write a C program that will find the sum of 10 elements in an array. The elements are 1,
3, 5, 4, 7, 2, 99, 16, 45, and 67. Output the contents of the array. The sum of the elements
in the array should be printed. (15 marks)
(Total 25 marks)
Question 2
a. Find and correct the error in each of the following (Note: there may be more than one
error):
b. Correct the following code so that it prints whether a given integer is odd or even:
if ( value % 2 )
printf( "Even integer\n" );
else
printf( "Odd integer\n" ); (10 marks)
c. Correct the following code so that it outputs odd integers from 2 to 350:
counter = 1;
Do {
if ( counter % 2 == 1 )
printf( "%d\n", counter );
counter += 2;
} While ( counter < 350 ); (10 marks)
(Total 25 marks)
Question 3
i. Function square that takes integer arguments, s1 and s2, and returns an integer result.
(5 marks)
ii. Procedure largest that takes three floats, a, b, c, and outputs the largest. (5 marks)
iii. Procedure instructions that does not receive any arguments and displays instructions
to a user. (5 marks)
b. Write a Function Circumference that calculates the area and circumference of a circle.
(Total 25 marks)
Question 4
a. i. Define an array of type float called numbers with 5 elements, and initialize the
elements to the values 0.0, 1.1, 2.2, …, 5.5. Assume the symbolic constant SIZE has
been defined as 5. (5 marks)
ii. Define a pointer, zPtr, that points to an object of type int. (3 marks)
iii. Print the elements of array numbers using array subscript notation. Use a for
statement and assume the integer control variable i has been defined. Print each
number with 1 position of precision to the right of the decimal point. (3 marks)
iv. Give two (2) separate statements that assign the starting address of array numbers to
the pointer variable zPtr. (3 marks)
ii. Use the function string copy to copy the contents of x into y. (4 marks)
c. Create a structure definition called card that contains two character pointer members, and
an array member called deck reserving space for 52 members. (3 marks)
(Total 25 marks)
Question 5
b. Write the C program to open a file called homework for writing. Output an appropriate
message if the file cannot be opened. (10 marks)
d. Write a single statement to accomplish each of the following. Assume that each of these
statements applies to the same program.
i. Write a statement that opens the file "oldmast.dat" for reading and assigns the
returned file pointer to ofPtr. (4 marks)
ii. Write a statement that opens the file "trans.dat" for reading and assigns the returned
file pointer to tfPtr. (4 marks)
(Total 25 marks)
END OF EXAMINATION