MECN2012 - June Exam
MECN2012 - June Exam
Duration
06/2024 3 Hours 09:00 CLM/FNB PC POOL
MECN2012A
Course Code No(s)
Year of Study 2
Mr B Chirewa
External examiner(s)
Time allowance
Course MECN2012 Hours 3
Nos.
Signature: ..........................................................
BLESSING CHIREWA
Name: ……………………………………………
(EXTERNAL EXAMINER)
Signature: …………………………………………...
Name:
Chioniso Kuchwa-Dube
……………………………………………
(HEAD OF DIVISION)
Signature: …………………………………………...
Signature: …………………………………………...
MECN2012/A
PAGE 1 of 9
Part A
Find and correct the ten errors in the provided code (BaseAddition.m). BaseAddition.m calculates the
sum of two numbers of any base less than base 11.
The program first ensures the lengths of two the numbers are equal by padding the shorter one with
leading zeros. For example, when adding the following binary numbers: Num1 = [1 1 1 0 1 1] and
Num2 = [1 1 1], the first part of the program must produce:
After which, the program iteratively calculates the sum of corresponding digits from the two numbers,
Num1 and Num2, considering a carry value from the previous digit addition. The final result,
FinalSum will be as follows:
Provided is a MATLAB script called Q1_InputData.m that contains the matrices Num1, and Num 2
that represent digits of the first and second number involved in the addition, listed in their original
length, as shown below.
PTO/Page 1…
MECN2012/A
PAGE 2 of 9
You are required to submit only the corrected MATLAB script for Part A. The errors given will not be
of the incorrect mathematical operator (e.g. multiplication) but may be of the incorrect type thereof (e.g.
matrix multiplication instead of element-wise multiplication), however incorrect logical operators may
have been used. Note as well that there will be NO more than one error per line. Do not move, adjust
or change the line numbering in the code, if you wish to delete a line of code simply comment it
out. (10 Marks)
Part B
You are also required to submit a separate script in the tab labeled Question 1 B on MATLAB
Grader. This script must contain ONLY the error description of the 10 identified errors. Each
error description will be of the form [Line number]-[Error effect]-[Error type] where each of the
fields is as follows:
• [Line number] - the line of the original program file in which the error occurred;
• [Error effect] - the letter describing the type of effect that the error would have on the
execution of the code (from the list provided in Addendum 1 A);
• [Error type] - the number describing the type of error (from the list provided in
Addendum 1.B).
Note the following formatting requirements for the error descriptions in the separate script:
• There must be no more than 10 lines in the submitted file;
• There must be only one error description per line;
• Error descriptions must be entered in increasing order of line number;
• There should be no spaces between the segments of the error description;
• There should be no brackets in the error descriptions;
• There should be no quotation marks or any other punctuation except the hyphen.
For example, supposing there is an error due to an unrecognised variable on line 5, the
corresponding error code would be 5-B-6.
See ADDENDUM 1 A and ADDENDUM 1 B for the listed error effects and types.
(10 Marks)
(Total: 20 Marks)
ADDENDUM 1 A: Error Effect Codes
While this is not a comprehensive list of all possible error effects, when describing any error
select the effect which best describes the expected or observed effect.
A: Compilation error - Program does not run at all
B: Runtime error - Program starts execution but fails at the point of error
QUESTION 1 TO BE CONTINUED OVERLEAF….
PTO/Page 2…
MECN2012/A
PAGE 3 of 9
C: Infinite loop - Program starts execution but becomes stuck in an infinite loop due to error
code
D: Output value error - Program completes execution but output is of unexpected or incorrect
value
E: Output format error - Program completes execution but output is of unexpected or incorrect
format
PTO/Page 3…
MECN2012/A
PAGE 4 of 9
Consider a program that sorts a sequence of numbers in descending order and displays the final
result. For example if given the following numbers: 1 3 2 5 3 4 10 9 8 9; the displayed output of the
program will be: 10 9 9 8 5 4 3 3 2 1. Figure 1 shows a flowchart for the given program. You are
required to convert the flow chart (as is) into a MATLAB script.
(Total: 20 marks)
PTO/Page 4…
MECN2012/A
PAGE 5 of 9
You are to write three separate functions to modularise the process of reading data from multiple excel
files into a nested structure array. The main function MUST call GetFileNames and GetData in its
execution. The functions, inputs and outputs are defined as follows.
Sub-function 1: GetFileNames
This function must retrieve the file names of multiple excel files that contain the KeyWord, from your
‘DroneMasses.xlsx’ ‘DroneProperties.xlsx’
PTO/Page 5
MECN2012/A
PAGE 6 of 9
The returned variable, function name and parameter list are given below:
Sub-function 2: GetData
This function retrieves information from the worksheets in the excel files saved in FileNames. This
function then saves the information in a structure array called Data.
Data= GetData(FileName,SheetName,CurrentSheet,Data)
1. FileName, a character array containing the full file name of each Microsoft Excel file that
contains your desired prefix and extension. Note this is singular.
2. SheetName, the name of the current worksheet in the given spreadsheet file.
3. CurrentSheet, a cell array containing all the data in the current worksheet, SheetName.
4. Data, a structure array containing all the information in the relevant excel files the working
directory. It will be initially empty.
1. Data, a nested structure array containing all the information in the relevant excel files from
the working directory.
The numerical information from the columns in the excel spreadsheet must be saved in Data in the
following manner: Data.FileName.SheetName.ColumnName = ColumnNumericalData
QUESTION 3 TO BE CONTINUED OVERLEAF….
PTO/Page 6
MECN2012/A
PAGE 7 of 9
Where FileName no longer includes the file extension .xlsx. Note: You might find the MATLAB
built-in function split useful (only for this sub-routine).
Main: Main_Function
This main function, called Main_Function retrieves the data from the relevant files and saves the
information in a structure array called Data. The main function MUST call GetFileNames and
GetData in its execution. This function then saves the structure array, Data in a .mat file called
Q3_Output.mat.
1. KeyWord, which is the file name key word that will be used to distinguish the relevant
files that you should use from the other files in your working directory.
2. filetype, which is the extension of the files that contain the data that you need, i.e. ‘.xlsx’
Note that this function MUST call the functions GetFileNames and GetData in its execution. Failure
to do so as well as any hard coding will result in a mark of zero for this section.
1. Q3_Output.mat, a .mat file where the nested structure array Data is saved.
2. Data, a nested structure array containing all the information in the relevant excel files
from the working directory.
The numerical information from the columns in the excel spreadsheet must be saved in Data in the
following manner: Data.FileName.SheetName.ColumnName = ColumnNumericalData
Your program must run completely silently and must not require any manual inputs from the user.
Do not deviate from the structure described above. Any unexpected behaviour or code structure that
deviates from what has been outlined in here will be heavily penalised.
PTO/Page 7
MECN2012/A
PAGE 8 of 9
subfunc1 and subfunc2 are marking parameters and have automatically been included in your
solution template on MATLAB Grader. You don't have to worry about these parameters.
PROVIDED FILES
DELIVERABLES
• MATLAB function submitted on MATLAB Grader that performs all the actions listed above.
Each code must contain an opening comment in the first two lines of the file with the student number
as well as the date of finalisation of the code in the following format:
%1234567
%YYYY-MM-DD
Note: Your script will be tested with different input data to what has been supplied in this exam. Do not
attempt to ‘hard code’ for any of these input cases.
(Total: 40 Marks)
(TOTAL: 80 MARKS)
----------------------END OF ASSESSMENT ----------------------
Page 9