?
2009-06-18 15:21:32 UTC
1. Write ‘C’ code fragments to:
(a) Declare integer variables called pens, pencils and total.
(b) Assign the value 25 to pens and 15 to pencils.
(c) Using an appropriate ‘C’ expression, assign total the contents of pens plus the contents of pencils.
2. Identify a suitable ‘C’ data type for each of the following:
(a) The number of students in a class.
(b) The title of the course on which a student is enrolled.
(c) The average examination mark for a module.
(d) The department in which the student is enrolled (either BE, CIE, CIS, CM or EME).
3. Consider the following ‘C’ program and clearly identify THREE errors that would prevent the program from compiling. (Note that the line numbers are for reference only and are not considered to be part of the program!)
1. #include (stdio.h)
2. void Main (void) {
3. char c;
4. printf("Enter a sentence:\n");
5. scanf("%c", &c);
6. while (c != ".") {
7. printf("%c\n", c);
8. scanf("%c", &c);
9. }
10. printf(".\n");
11. }
4. Define a constant LENGTH holding the value 5. Why is the use of constants beneficial when writing computer programs?
5. Explain how the code fragment below is executed by a computer and hence write down the output produced.
int x;
int y;
for (y=0; y<5; y++) {
for(x=0; x<=y; x++)
printf("%d", x);
printf("\n");
}
6. A mail-order catalogue charges £3.50 for postage and packing on goods up to and including a total value of £10.00. For orders over £10.00 up to and including £20.00, the cost is reduced to £1.50. However, postage and packing is free for orders over £20.00. Write a ‘C’ program that asks a user to enter the total cost of goods on an order and then displays the amount of postage to be added.
7. Rewrite the if statement below using a switch statement. (Note it is not necessary to include the declarations):
enum boatT {DINGY, ROWING, SPEED, YATCH,
RORO, CRUISELINE};
enum boatT boat;
if ((boat==DINGY) || (boat==ROWING))
size=1;
else
if ((boat==SPEED) || (boat==YATCH))
size=2;
else
size=3;
8. Write a ‘C’ function that accepts two integer parameters and returns the smallest of these values. If both parameters hold the same value, simply return that value.
9. Consider the global declarations and definitions below. Write a ‘C’ function that returns the number of integers in the array board that are less than 10. You should assume that values will have been stored in the array elsewhere.
#define SIZE 50
int board[SIZE][SIZE];
Question 10
(a) Define the terms “Bounded Iteration” and “Unbounded Iteration”.
(b) For each of the scenarios below, identify an appropriate iterative control structure provided by the ‘C’ programming language. You are required to explain your answer.
(i) It is required to repeatedly execute a section of code during the period that the value of a certain variable remains larger than 10.
(ii) It is required to obtain the total number of hours of sunshine in January given that the user enters the number of hours of sunshine for each day.
(iii) A user is asked to enter ‘y’ or ‘n’ to indicate ‘yes’ or ‘no’ to a particular question. If the user enters any other character, the program should again ask that the user enter ‘y’ or ‘n’.
(c) Write a ‘C’ program that reads a sequence of integers terminated by the value -999 and outputs their average. Note the termination value of -999 should not be included in the calculation of the average.
(d) Consider the following global definitions for an array of integers:
int myarr[500];
Write a ‘C’ function called initialise that that sets all elements in the array to the integer value passed as a parameter. For example, the statement initialise(5); should result in all 500 array elements holding the value 5.
Question 11
(a) ‘C’ provides two selection control structures, namely an “if” statement and a “switch” statement. Briefly indicate the circumstances under which each should be used.
(b) Write down the output of the following fragment of ‘C’ code. Explain your answer.
i = 3;
j = 4;
if (i >= j)
if (i > j)
printf("Point A\n");
else
printf("Point B\n");
printf("Point C\n”);
(c) A theatre company offers discount to customers who purchase tickets for performances in bulk. The percentage discounts offered are shown in the table below.
Number of Tickets
Percentage Discount
<3
None
3 to 5 (inclusive)
5%
6 to 10 (inclusive)
10%
>10
20%