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

BACHELOR OF ELECTRICAL AND ELECTRONICS

ENGINEERING WITH HONOURS


BACHELOR OF MECHANICAL ENGINEERING WITH HONOURS
BACHELOR OF CIVIL ENGINEERING WITH HONOURS

AUG 2020 FINAL EXAMINATION (TAKE-HOME)

COURSE NAME: PROGRAMMING IN C++ / PROGRAMMING


METHODOLOGY & PROBLEM SOLVING
COURSE CODE: EGC3173 / EGC3113
TIME ALLOWED: 3 HOURS
TIME: 2.00 PM – 5.00 PM (Malaysia Time)
DATE: 22 AUGUST 2020

This examination question paper consists of NINE (9) pages including the cover page.
This examination contributes 50% towards the total mark of the course.

INSTRUCTIONS TO CANDIDATE:

1. Answer ALL FIVE (5) questions. All questions carry equal marks.
2. All working steps, answer, and codes must be handwritten. Cross through any work
that you do not wish to be marked.
3. Answers to exam questions written in a programming language other than C++ will
not receive credit.
4. Write your answers on A4 size paper, either in Black or Blue colour pen – any
unreadable answer / information will be voided.

1
5. Write your name and SEGi Student ID on every page (top right corner) of your
answer sheets, and number all your answer sheets (bottom right corner) in
chronological order.
6. Scan all your answer sheets into a single PDF (together with the Student’s Answer
Script Cover Page provided), and name it under the Module Code, followed by your
SEGi Student ID, eg: EME3283_SUKD123456.pdf
7. Upload your single PDF file to the Blackboard submission page before 6 pm on
22 August 2020 (Malaysia time).

INFORMATION TO CANDIDATE:

You are permitted to use a non-programmable electronic calculator in this examination.


This is an Open Book Exam but you are NOT permitted to share your answers with anyone
or get help from anyone to answer the questions before the aforesaid Blackboard
submission time.
LATE submission will be awarded FAIL for the module and NO appeal will be granted for
the following reasons
 Lack of knowledge on using Blackboard in submission
 Poor internet connection / no internet connection
 No computer for submission

Assessment / CLO Mapping


Assessment \ CLO CO1 CO2 CO3
1. Test 1 /

2. Test 2 / /
3. Assignment /
4. Final Exam / /

CO1 Analyses the programs/instructions in C++ language. [C4, PO3]


CO2 Develops flow control structures in writing programs using C++ language. [C6, PO3]
CO3 Evaluates the ability to engage in independent learning of modern tools for the
development of programs in C++. [C5, PO5]

Question CO
1 1
2 1
3 2
4 2
5 2

2
Answer all FIVE (5) questions

Question 1

(a) Interpret the respective output displayed by the following statements.


If nothing is displayed due to an error, then answer “nothing”.

(10 marks)

(b) Analyze whether the following variable names are valid or invalid. If they are
invalid, state the reason.

(i) 19meter
(ii) cos_1
(iii) switch
(iv) r3su1t
(v) _20year old

(5 marks)

3
(c) In the following program, analyze any errors that would prevent the program from
compiling or running logically and write ONLY the necessary corrections.
Copy Table Q1(c) in your answer script and fill it up. Provide your answer which
includes the corrected codes and their corresponding line numbers ONLY in
Table Q1(c).

Hint: some lines may have multiple errors while some lines may have no error, or
there might be missing code in between two lines.

Table Q1(c)
Line No. Corrections

(5 marks)

(Total: 20 marks)

4
Question 2

(a) Part of program development is the visualization of the program using a flowchart.
Draw the flowchart for the following pseudocodes:

(i) Declare variables A, B, C, Total, Average, and Product as floats.


Obtain A, B, and C from the user.
Calculate the Total = A + B + C.
Calculate the Product = A × B × C.
Calculate the Average = Total / 3.
Display the Total, Average, and Product to the user.
(5 marks)

(ii) Obtain Num1 and Num2 from the user.


If Num1 is less than 25: Sum = Sum + Num1.
Else: Sum = Sum – Num2.
Display Sum to the user.
(Assume Num1, Num2, and Sum are integers.)
(5 marks)

(b) The distance travelled by an accelerating vehicle can be determined by the


following formula:
1
𝑑 = 𝑣 𝑡 + 𝑎𝑡
2
where
𝑑 is the distance travelled in meters (m),
𝑣 is the initial velocity of the vehicle in meter per second (m/s),
𝑡 is time travelled in seconds (s), and
𝑎 is the acceleration of the vehicle in meter per square seconds (m/s 2).

Write a program that prompts the user for the necessary information, performs the
calculation for distance travelled by an accelerating vehicle, and displays the
distance travelled with 2 decimal places. Ensure that the user inputs the values with
correct units.
(10 marks)

(Total: 20 marks)

5
Question 3

(a) Apply the if-else flow control structure to write a program that performs the
following tasks: prompts the user to input a number, then displays the number with
a message saying whether the number is positive, negative or zero.
(10 marks)

(b) Apply the while and switch flow control structure to write a program that produces
the outputs corresponding to the relevant inputs as shown in
Table Q3(b). Invalid marks entered (< 0 or > 100) should generate an error message
and then terminate the program.

Hint: assume the input as integers and divide by 10 for the switch case.

Table Q3(b)
Input: Marks Output: Grades
80 – 100 HD
70 – 79 D
60 – 69 C
50 – 59 P
0 – 49 F
(10 marks)

(Total: 20 marks)

6
Question 4

(a) Write a program that performs the following task 5 times using the for-loop flow
control structure to: obtain an integer number from user, calculate the average of
all integers entered so far, and display the average. Below is an example of the input
and output of the program with some explanations.

Enter an integer: 1
Average so far: 1 Average = 1 / 1
Enter an integer: 2
Average so far: 1.5 Average = (1 + 2) / 2 = 1.5
Enter an integer: 3
Average so far: 2 Average = (1 + 2 + 3) / 3 = 2

(10 marks)

7
(b) An engineer wants to create the following 8 x 8 array 𝐴 from a 2 x 2 array 𝐵:
1 2 0 0 0 0 0 0
⎡3 4 0 0 0 0 0 0⎤
⎢ ⎥
0 0 1 2 0 0 0 0
⎢ ⎥
0 0 3 4 0 0 0 0⎥ 1 2
𝐴= ⎢ ; 𝐵=
⎢0 0 0 0 1 2 0 0⎥ 3 4
⎢0 0 0 0 3 4 0 0⎥
⎢0 0 0 0 0 0 1 2⎥
⎣0 0 0 0 0 0 3 4⎦
To do this, the engineer wrote the following code to duplicate copies of array 𝐵 to
form 𝐴. However, the code does not give the result wanted. In the following code,
analyze any errors and write ONLY the necessary corrections in Table Q4(b).
Copy Table Q4(b) in your answer script and fill it up. Provide your answer which
includes the corrected codes and their corresponding line numbers ONLY.

Table Q4(b):
Line No. Corrections

(10 marks)

(Total: 20 marks)

8
Question 5

The electrical output voltage (𝑉) of a temperature sensor has been calibrated with various
temperatures (𝑇). The calibration result is shown in Table Q5.

Table Q5
Temperature 𝑻 (℃) 2 5 13 20 29 35 43
Voltage 𝑽 (𝐦𝐕) 3 5 8 12 14 17 21

The data in Table Q5 is fitted using linear regression model to obtain the best fit line:
𝑉 = 𝑎 + 𝑎 𝑇, where the coefficients 𝑎 and 𝑎 are given by:

𝑛 ∑(𝑇 𝑉 ) − ∑ 𝑇 ∑ 𝑉
𝑎 =
𝑛 ∑(𝑇 ) − (∑ 𝑇 )
𝑎 =𝑉−𝑎 𝑇
∑ ∑
with 𝑛 = 7, 𝑉 = is the mean of 𝑉, and 𝑇 = is the mean of 𝑇.

Write a program that applies arrays, function and loop structures to perform the following:

(a) Declare two arrays called temp and volts and initialize them respectively
with the values of Temperature 𝑇 and Voltage 𝑉 as shown in Table Q5.
(4 marks)

(b) Write a function named Sum() that apply a loop structure to calculate and return
the sum of the elements of a given array.
(6 marks)

(c) Assume that the values of 𝑇 𝑉 and 𝑇 have been calculated and stored in arrays
TV and Tsq respectively, write the statements to perform calculation for the
coefficient 𝑎 , and 𝑎 for the linear regression model using the function written in
Part (c) where necessary.
(4 marks)

(d) Output the values of 𝑎 and 𝑎 into a file named “linearModel.txt”.


(6 marks)

Note: you may assume all necessary libraries are already #included for your answer.

(Total: 20 marks)

END OF QUESTION PAPER

You might also like