UFMFN7-15-1 January 2016

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Faculty of Environment and

Technology
Academic Year: 15/16

Module Leader: Brian Carse/Khem Emrith


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

Examination Date: January 2016


Examination Start Time:
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: None

Details of approved material supplied by Student:

None
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 8
Instructions to Candidates:

Answer ALL questions.

Question 1. [20 marks]

The diagram below shows a state transition diagram used to design the embedded
software control system for a simple microwave oven.

Using the state transition diagram as a starting point, and making any reasonable
assumptions, devise a pseudocode solution for the operation of the oven control
software (hint: you should use the SWITCH/CASE statement).

[20 marks]

UFMFN7-15-1 Page 2 of 8
Question 2. [20 marks]

(a)

Discuss the advantages of using functions in C programs.


[10 marks]

(b)
The diagram below shows part of a structure chart for the design of an “intelligent”
washing machine which varies washing time with the characteristics of the washing
load.

The washing time is to be calculated as

Washing Time = (1+Weight )×(Dirt Level)×(Grease Level)2

Write a C function called CalculateWashingTime which takes floating point values of


Weight, Dirt Level and Grease Level as input parameters and returns the floating
point value of the Washing Time.

You should take care to ensure that the syntax of your C function, including
parameters, is correct.

[10 marks]

UFMFN7-15-1 Page 3 of 8
Question 3. [10 marks]

(a) The pre-processor directive #define can be used to define constants in a C


program e.g.

#define MaxSampleSize 100

i) State two advantages of using #define in this context and explain your
answers.
ii) State one more example of a pre-processor directive used in C-programs.

[6 marks]

(b) Consider the following C-program segment

#include <stdio.h>
int main(void)
{
int num, i;
int sum=0
printf("Please enter a number: ");
scanf("%d", &num);
for (i=1; i <= num; i++)
{
sum = sum + i;
printf(%d\n, sum);
}
return 0;
}

i) The above code will fail to compile due to syntax errors. Identify the syntax
errors and correct the code above.

ii) What is the output of running the corrected program when the value
entered for num is 3?

[4 marks]

UFMFN7-15-1 Page 4 of 8
Question 4. [10 marks]

Consider the following flowchart.

start

Initialise variables
Product=1; i=1

false
Increment i i < 5? print
by 1 product

true

Product = end
Product*i;

(a) Briefly explain what the flowchart is trying to achieve.

[3 marks]

(b) An incomplete C-program for the flowchart is given below

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

int main(void){
int product=1;
int i=1;

/*your code goes here*/

return 0;
} (Question 4 continued overleaf…….)

UFMFN7-15-1 Page 5 of 8
Complete the program so that it achieves the task shown in the flowchart.

[5 marks]

(c) What is the output of the program?

[2 marks]

Question 5. [10 marks].

(a)
Explain what is meant by an index to an array. For a C language array of size N,
what are valid indices?

Is a floating point value valid for an array index? If not, why not?
[4 marks]

(b) Consider the following array definitions in C language:

int Array1[] = {12, 23, 2, 14, 50, 30, 20, 10, 22};
int Array2[] = {10, 20, 30, 40, 50};

i) Declare an array of integer values, Array3, which is of the same size


as Array2.

ii) Write a for loop that allows you to add Array1 with Array2 such
that only even indices of Array1 are used and all indices of Array2
are used. Results of the addition are stored in Array3.

iii) What are the values contained in Array3[0] and Array3[2]?

[6 marks]

UFMFN7-15-1 Page 6 of 8
Question 6. [10 marks].

(a)

Explain, using examples, the difference between a local variable and a global
variable in a C-language program.

[4 marks]

(b) Consider the following C-program segment

int val = 20;

int *ptr = &val;

*ptr = 2;

val = *ptr * val;

i) What is the purpose of using the & operator?

ii) Write a printf() statement that allows the value pointed by ptr to
be displayed on screen.

iii) What value does the variable val contain after executing the above
statements?

[6 marks]

UFMFN7-15-1 Page 7 of 8
Question 7. [10 marks]

Explain what is meant by the following related to software design:

 Abstraction and Information Hiding


 Coupling
 Cohesion

[10 marks]

Question 8. [10 marks]

(a) Explain the difference between the verification and the validation of a program.
[4 marks]

(b) The following rules have been taken from a standards document. Briefly state
why you think each rule has been defined.

9.1 All automatic variables should be assigned a value before being used.

14.4 The goto statement shall not be used.

[6 marks]

END OF EXAMINATION PAPER

UFMFN7-15-1 Page 8 of 8

You might also like