You are on page 1of 9

Faculty of Environment and Technology

Academic Year: 17/18


Examination Period: January

Module Leader: Khem Emrith


Module Code: UFMFN7-15-1
Module Title: C Programming

Duration: 2 Hours

Standard materials required for this examination:


Examination Answer Booklet Yes

Multiple Choice Answer Sheet No


Type of paper e.g. G3, G14 N/A
Graph Paper
Number of sheets per student 0

Additional materials required for this examination:


Details of additional material supplied by UWE Bristol: None

To be collected with Answer Booklet (please delete as appropriate) N/A

Details of approved material supplied by Student:

None

To be collected with Answer Booklet (please delete as appropriate) N/A

University approved Calculator Yes

Candidates permitted to keep Examination Question Paper Yes

Candidates are NOT permitted to turn the page


over until the exam starts

UFMFN7-15-1 Page 1 of 9
Instructions to Candidates:

Answer ANY FIVE out of the SIX questions provided

Question 1 (20 marks)

(a) Consider the following problem specification that requires the development of software
that computes the monthly water bill for the West of England (WoE) Water customers.

• A customer’s monthly bill includes a £25 water demand charge plus a consumption
(usage) charge of £0.90 for every thousand gallons of water used.

• Consumption of water is figured from current and previous meter readings (in
thousands of gallons). Current meter reading is the one most recently taken and
the previous meter reading is the one taken at the end of the previous quarter.

• If the customer’s unpaid balance is greater than zero, a £5 late charge is added to
the bill as well.

• A printed bill needs to be generated.

Design a structure chart (structure diagram) for the water bill software. The structure chart
should show information passing between the modules.

(Note: A STRUCTURE CHART, NOT A FLOWCHART, IS REQUIRED).

(10 marks)

UFMFN7-15-1 Page 2 of 9
(b) Consider the following pseudocode which represents the functional steps of a program
to control a phone that can be only used to dial but not to receive calls.

Repeat Forever{
Wait for a new Event
Switch(state){
case Idle:
if (Event == Pickup)
state = Dialling
case Dialling:
if (Event == ReceiveRingTone)
state = RemoteEndRinging
if (Event == HangUp)
state = Idle
case RemoteEndRinging:
if (Event == RemoteEndAnswers)
state = Connected
if (Event == HangUp)
state = Idle
case Connected:
if (Event == HangUp)
state = Idle
} /*end switch*/
} /*end repeat*/

You are required to draw a State Transition Diagram to represent the pseudocode above.

(10 marks)

UFMFN7-15-1 Page 3 of 9
Question 2 (20 marks)

(a) Explain the following terms as used in a C-Program, providing examples to justify
your answer.
(i) Variable
(ii) Data type
(iii) Pre-processor directive

(9 marks)

(b) Consider the following C program:

1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. void foo1(int a, int b){
5. static int c = 5;
6. if (a>b)
7. c = c + a;
8. else
9. c = c + b;
10. return c;
11. }
12.
13. int main(void) {
14. for(i=0 ; i<4 ; i++){
15. printf("\nResult: %d ", foo1(i,2));
16. }
17. return 0;
18. }

(i) The above program will fail to compile due to syntax errors. Identify the syntax
errors and correct the program for it to compile successfully.

(ii) Explain the purpose of the keyword static in the above C-program.

(iii) What is printed on the standard output when the above program is executed?

(11 marks)

UFMFN7-15-1 Page 4 of 9
Question 3 (20 marks)

(a) Consider the following flowchart:

start

Declare x and y

Read values
for x and y

Decrement x

true
Increment y
x>y?

false
e

Display values
for x and y

stop

(i) Write a c-program to that corresponds to the flowchart above.

(6 marks)

(ii) What value is printed on standard output with the following values: x=4,
y=2;

(2 marks)

UFMFN7-15-1 Page 5 of 9
(a) Explain, using an appropriate example what you understand by a nested loop.
(4 marks)

(b) Consider the following incomplete C-program:

#include <stdio.h>
#include <stdlib.h>

int main(void) {

float range1 = 20.0, range2 = 50.0;

/* Your code goes here */

return 0;
}

Complete the program above using the following specification:

The above program needs to request the user to enter a floating point value from
the standard input and test whether that value is within the specified range ( range1
< value < range2), assuming that the values for range1 and range2 are already
provided in the program. The program then displays a message on the standard
output to indicate whether the value entered was within range or out-of-range.
(8 marks)

UFMFN7-15-1 Page 6 of 9
Question 4 (20 marks)

(a) Write C-language statements to declare and initialise the following


(i) A variable of type integer with a value of 10
(ii) A one dimensional array with the following integer values: 1, 10, 6, 21, 10
(iii) A pointer to the variable defined in (i)
(6 marks)

(b) Explain, using appropriate examples, what the following operators are used for
when using pointers in a C-program:
(i) addressing operator
(ii) dereferencing operator
(6 marks)

(c) Consider the following C-Program function

void swap(int A, int B)


{
int temp;
temp = A;
A = B;
B = temp;
}

The function is called from main() using the statement swap(x,y) where x and y
are integers defined in main(). The intention is that the values of x and y in main()
are swapped.

(i) Explain why the function above fails to do what is required.

(3 marks)

(ii) Re-write the function implementation of swap for it to achieve its purpose and show
how it is called in main(). (Hint: use pointers)

(5 marks)

UFMFN7-15-1 Page 7 of 9
Question 5 (20 marks)

(a) List TWO advantages of using functions in a C-Program.


(4 marks)

(b) Write the implementation of a C-Program function named CheckSign that takes as
input an integer number and returns a ‘1’ if the number is positive, a ‘-1’ if the
number is negative, or otherwise it returns a ‘0’.

(5 marks)

(c) Consider the following C-Program.

#include <stdio.h>

int main(void) {

int myArr[]={-2,3,-4};

/* Your code goes here */

return 0;
}

Complete the program above so that it performs the following task:

The program will need to make function calls to the CheckSign function defined in
(b) to check the sign of the each value in the array myArr[] and will need to
convert any negative value to a positive value.
(4 marks)

(d) Explain what you understand by the term ‘modular decomposition’ when writing C-
programs.
(3 marks)

(e) List TWO advantages of using a modular decomposition approach when coding C-
programs.
(4 marks)

UFMFN7-15-1 Page 8 of 9
Question 6 (20 marks)

(a) Write a C-program code segment that allows you to insert a software delay in a
program.

(4 marks)

(b) Consider the following C-program segment:


int i;
int myArray[20];
for (i=0; i<20; i++){
myArray[i] =i*i + 2*i;
}
The above segment needs to be tested using a ‘simple loop testing’ approach.
What steps are involved in carrying out this test?
(5 marks)

(c) Consider the following function:

int factorial (int n){


int i, fact =1;
for (i=2; i<=n; i++)
fact *= i;
return fact;
}

(i) What is the result of executing the above function when n=2.

(ii) Rewrite the function but this time using a while loop.
(6 marks)

(d) Explain, using an appropriate example, what is the purpose of using the break
statement within switch-case statement in C-programs.
(5 marks)

END OF QUESTION PAPER

UFMFN7-15-1 Page 9 of 9

You might also like