Sample Examination, Semester 1, 2019
109759 Programming (MATLAB & C)
ENG 1002, 1002UAC
Course Coordinator: Dr Cheryl Pope
Official Reading Time: 10 mins
Writing Time: 120 mins
Total Duration: 130 mins
Questions Time Marks
Answer all 11 questions 120 mins 120 marks
120 Total
Instructions for Candidates
• This exam is a SAMPLE for the purposes of understanding the structure of the
exam. It is NOT intended to be used as a primary source for studying the
course content, which is covered in the formative materials used during the
course. The exam will cover the same concept areas but will use different,
often new, problems to assess.
Solutions are not provided to encourage active attempts at applying concepts
(discourage passive studying of specific answers). However, you can check
your code by testing it and you are welcome to discuss questions on the
discussion board and PASS sessions.
• Begin each answer on a new page
• Examination material must not be removed from the examination room
Permitted Materials
• General or translation dictionary is permitted
DO NOT COMMENCE WRITING UNTIL INSTRUCTED TO DO SO
Course ID: 109759 Page 2 of 7
Sample Examination, Semester 1, 2019
Programming Structures: All Code Questions in this Section must be answered
in MATLAB
Question 1 5 marks
Variables and Operators
Write MATLAB code that intialises three variables: h1, h2 and h3 representing
three heights, and calculates the mean of the heights. storing this value in mean.
[5 marks]
[Total for Question 1: 5 marks]
Question 2
Iteration
(a) i. Write MATLAB code to print the odd numbers between 10 and 1.
[4 marks]
ii. Re-write this code, using a while loop.
[3 marks]
(b) What will be the value of result when the following code runs?
result = 0;
for i = 1:5
for j = i:3
result = result+1;
end
end
[2 marks]
[Total for Question 2: 9 marks]
Please go on to the next page. . .
Course ID: 109759 Page 3 of 7
Sample Examination, Semester 1, 2019
Question 3
Selection
(a) Rewrite the following as a single if-else statement
if strcmp(player,"rock") && strcmp(computer,"scissors")
playerWon = true;
elseif strcmp(player,"paper") && strcmp(computer,"rock")
playerWon = true;
elseif strcmp(player,"scissors") && strcmp(computer,"paper")
playerWon = true;
else
playerWon = false;
end
[3 marks]
(b) Complete the missing MATLAB if-statement
% this script prompts for a divisor and outputs an error message
% if the divisor is not valid.
number = input(’Enter a divisor: ’);
<missing line>
disp(’invalid divisor’);
end;
[2 marks]
[Total for Question 3: 5 marks]
Question 4
Input and Output
(a) Write MATLAB code to read in two floating point numbers from the user
and print their product with a precision of 4 digits following the decimal
point.
[4 marks]
[Total for Question 4: 4 marks]
Please go on to the next page. . .
Course ID: 109759 Page 4 of 7
Sample Examination, Semester 1, 2019
Question 5
Functions
(a) Write a MATLAB function called compareNums that takes two numbers x
and y as its parameters and returns two numbers: big and small where big
is the larger of x and y and small is the smaller of x and y.
[7 marks]
(b) Write a MATLAB statement that calls the function written above
[3 marks]
[Total for Question 5: 10 marks]
Question 6 5 marks
Code Style
Describe 5 of the ways you can make your code more readable and maintainable
[5 marks]
[Total for Question 6: 5 marks]
Question 7 2 marks
Libraries
What role do libraries play in programs?
[2 marks]
[Total for Question 7: 2 marks]
Please go on to the next page. . .
Course ID: 109759 Page 5 of 7
Sample Examination, Semester 1, 2019
Question 8
Data Structures - arrays, matrices and strings
(a) Given a 2D array, called A, of size n x m, write a MATLAB script that will
store all of the odd values in the ‘fence’ of A in a new vector. MATLAB matrix
shortcuts can not be used for this problem (ie A(x,:) can not be used to get
an entire row)
(note: the ‘fence’ is the outer rows and columns)
Sample Output: Given A = [ 5, 4, 6, 7, 3 ; 1, 2, 3, 4, 5 ; 5, 6, 4, 2, 4 ; 4, 5,
3, 2, 1]
[5 7 3 1 5 5 5 3 1]
Fence of the above matrix is
5 4 6 7 3
1 5
5 4
4 5 3 2 1
[14 marks]
(b) Write a MATLAB script that reads in a string and a character from the user
and prints out the number of times the character appears in the string
[5 marks]
[Total for Question 8: 19 marks]
Question 9
Problem Solving, Design, Implementation and Testing in MATLAB
(a) Create a function called makeLayered that takes a single parameter n and
returns a square 2D array that has 1 at its centre and layers of numbers
around this increasing up to and including the value n.
For example: makeLayered(3) will return the 2D array
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3
[8 marks]
(b) Give three test cases for the makeLayered function that will test a range of
outcomes. For each test case explain what types of inputs that case is testing
and give the expected output.
[4 marks]
[Total for Question 9: 12 marks]
Please go on to the next page. . .
Course ID: 109759 Page 6 of 7
Sample Examination, Semester 1, 2019
All Questions in this Section must be answered in C
Question 10
Defensive Programming in C - fundamental C programming structures
(a) What does it mean for a variable to be ’in scope’? What determines the
scope of a variable in C?
[3 marks]
(b) write a C program that Prompts the user for a string (less than 20 characters
long) representing a name, reads that string into a character array, prints
the number of characters in the string.
[4 marks]
(c) List and give an example of three different defensive programming practices
[6 marks]
[Total for Question 10: 13 marks]
Please go on to the next page. . .
Course ID: 109759 Page 7 of 7
Sample Examination, Semester 1, 2019
Question 11
Memory & Data Representation in C
(a) Explain the difference between: 1) an int and a long int 2) a float and a
double. Include in your explanation when you would use each.
[4 marks]
(b) Write a function common char that takes two strings as arguments and re-
turns a new string that contains a single copy of all of the characters that
appear in either of the two strings. For example, if the strings ”hello” and
”world” were passed as arguments the function would return the string ”hel-
lowrd” (o and l were already in array from ”hello”
You may use string functions in your solution.
[14 marks]
(c) Explain the difference between call-by-reference and call-by-value. Give an
example of each in your explanation.
[4 marks]
(d) Write a program called odds evens that reads a (non-empty) sequence of in-
tegers. As for question 1 above, your program should first ask for the num-
ber of integers to read and dynamically allocate an array just big enough to
hold the number of values you read.
You should then read in the elements of the array using a loop. Then your
program must dynamically allocate two arrays one to hold all the even num-
bers from the array you have just read and one to hold the odd numbers
from the array. Your code must allocate just enough space to each of the
arrays to hold the odd and the even numbers.
[14 marks]
[Total for Question 11: 36 marks]
End of exam