Você está na página 1de 8

Examiners M ark M oderatedM ark

 %  %
Total M arks Lecturer Signature Total M arks Student Signature

Programme:
BTech. Electronics Engineering (BELL), and
BTech. Power Engineering (BPEN)

Applied Programming (APR520S)

Examination 2, 1ST Opportunity


TOTAL MARKS: 50
DURATION: 120 min

DATE: 18 October 2018

EXAMINER: Ms. Aili Shigwedha


MODERATOR: Mr. Wilbard Mbandeka

STUDENT NAME: STUDENT NUMBER:

INSTRUCTIONS TO CANDIDATE:

1. This is a closed book exam. Answer all questions in this question paper.
2. All Examination materials (question paper, student answer scripts and unused writing papers) should
be returned back to the Examiner.
3. When answering questions, you should be guided by the marks allocated to each question.
4. Always write in ink (with exceptions of drawings) and clearly show all calculations and steps.
5. Where necessary, make reasonable approximations and assumptions. State and justify any such
assumptions and approximations made.
6. Wherever appropriate, include the correct units with your answers.
7. No cell phones are allowed for or during the duration of the examination.
8. NUST general examination condition rules and regulations apply.

Allowable Materials:
 Basic or Scientific Calculator (calculators may not be borrowed from fellow students during the examination).

Mark Allocations:
 (1 mark )
√ (0.5 mark)
SECTION 1: [30]

1.1. Write C function prototypes for the following functions:


(a) A function named mult() that accepts two single-precision numbers as parameters, multiplies these
two numbers and returns the result to the calling function. (2)
SOLUTION:
double mult(float, float);

(b) A function that produces a table of the numbers from 1 to 10, their squares, and their cubes. No
arguments shall be passed to the function and the function returns no value. (1)
SOLUTION:
void table();

1.2. Write C statements to do the following:


(a) Declare a 26-element character array named fun. (1)
char fun[26];

(b) Declare a character ch. (1)


char ch;

(c) Read a character from the terminal into the first element of fun. (1)
scanf(“%c”, &fun[0]);

(d) Copy the character read in part (c) into other elements of fun. (3)
int i;
for (i = 1; i < 26; i++)
fun[i] = fun[0];

(e) Change the last element of fun to the character ‘y’ (1)
fun[25] = ‘y’;

1.3. Consider each of the following C code segments, analyse and determine the output of each program?
Clearly show all relevant steps leading to the final evaluation.

a) (2)
int num = 0;
while (num <= 10)
{
num++;
printf("%d,",num);
}

SOLUTION:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

Code Analysis:

num Condition Output


(num <= 10?) (value of num)
0 T 1
1 T 2
2 T 3
3 T 4
APR520S Page 2 E2/10/2018

Total page marks:


4 T 5
5 T 6
6 T 7
7 T 8
8 T 9
9 T 10
10 T 11
11 F

b) (3)

SOLUTION:
27

Code Analysis:
Initialisation:
value = 3, limit = 9 //do not change
result = 0;

x Condition (x < 9?) result Output


0 T 0
1 T 0+3=3
2 T 3+3=6
3 T 6+3=9
4 T 9 + 3 = 12
5 T 12 + 3 = 15
6 T 15 + 3 = 18
7 T 18 + 3 = 21
8 T 21 + 3 = 24
9 F 24 + 3 = 27 27

c) (4)

SOLUTION:
31

APR520S Page 3 E2/10/2018

Total page marks:


Analysis:
i i< 5 a[i] a[i] % 3 == 0 ? s = s + ( a[i] / s) output
0 T a[0] = 10 10 % 3 = 1  F
1 T a[1] = 20 20 % 3 = 2  F
2 T a[2] = 30 30 % 3 = 0  T s = 1 + (30 / 1) =
31
3 T a[3] = 40 40 % 3 = 1  F
4 T a[4] = 50 50 % 3 = 2  F
5 F 31

d) (4)
int k;
float time[3];
for (k = 0; k < 3; k++) {
time[k] = (k-4) * 0.5;
printf("%.1f ", time[k]);
}

SOLUTION:
-2.0 -1.5 -1.0

Analysis:
k time[i] output
0 -2.0 = (0 - 4) * 0.5 -2.0
1 -1.5 = (1 - 4) * 0.5 -1.5
2 -1.0 = (2 - 4) * 0.5 -1.0

1.4. What will be the output if the following code is inserted at line 9 of the main() function?` (3)
Code to be inserted in line 8:
x = 1;
xLimit = 5;

Main function:

SOLUTION:
x is 2
x is 3
x is 4
x is 5
Final value of x is 7

APR520S Page 4 E2/10/2018

Total page marks:


1.5. What will be the output if this function call is invoked in the main() function as follow? (4)

first(3, 4); //this line is part of main()

SOLUTION:
Z = 8

Code Analysis:

x = 3 and y = 9 (and they do not change throughout the loop)

i Condition z = z + (x - 1)
(i < y?)
0 T z = 0 + (3 – 1) = 2
1 T z = 2 + (3 – 1) = 4
2 T z = 4 + (3 – 1) = 6
3 T z = 6 + (3 – 1) = 8
4 F

SECTION 2: [20]

2.1. Write a C program which declares an array of 100 integer numbers and initialises the array to the multiples
of 8 as follow: 8, 16, 24, 32, … (4)

SOLUTION:
int i;
int num[100];
for (i = 0; i < 100; i++) {
num[i] = 8 * (i + 1)
printf("%d, ", num[i]); //optional
}

2.2. Assume that the variable i and the array s have been defined with the following statement: (6)
int i;
int s[] = {3,8,15,21,30,41};

Using loop(s), write C statements that will output the array elements in the following manner/pattern:

3 8
15 21
30 41

APR520S Page 5 E2/10/2018

Total page marks:


SOLUTION:
for (k = 0; k <= 5; k += 2) {
printf("%d %d \n", s[k], s[k+1]);
}

Analysis:

i k <=5? s[k] s[k+1]


0 True 3 8
2 True 15 21
4 True 30 41
6 False -

2.3. The volume of a right circular cylinder is given by:𝑣 = 𝜋𝑟 2 ℎ (10)

Write a C function name vCylinder() that accepts two single-precision arguments corresponding to the
cylinder’s radius and height, respectively, and returns the cylinder’s volume.

Include the above function in a working C program. Make sure your function is called from the main() and
correctly display the returned volume. Have the main() prompt the user for radius and height, test the
vCylinder() by passing the required data, and. Ensure to display the volume with 4 decimal places.

This question is marked according to this scheme:


5% Program comments
5% Correct declarations
10% Prompting of radius and height
10% Displaying of volume with 4 decimal places
30% Implementation of vCylinder()
30% Implementation of main()
10% General program functionality

APR520S Page 6 E2/10/2018

Total page marks:


APR520S Page 7 E2/10/2018

Total page marks:


APR520S Page 8 E2/10/2018

Total page marks: