Overall, students did well on the exam. If you haven't done well on a particular question, be sure to re-do it, perhaps with the help of a member of the course staff, instead of just reading the solution.
chars, substring (subarray)
indexing: the most common error was using the typical nested loop
pattern for matrix traversal without adjusting for the length of the
string that is being matched:
for r= 1:numberOfRows
for c= 1:numberOfCols % WRONG. Should be 1:numberOfCols-2
....
end
end
Some students created new (wrong) syntax for working with a 2-d array of
chars. Remember: 2-d simple array syntax, whether each
component stores a number or a character, is
(b) Computing histogram data: the simplest algorithm uses a single loop that works through the rows of the cell array to access the surname strings and increment the tally. An inefficient algorithm uses nested loops to look for--a search!--surnames of length 1, then surnames of length 2, ..., etc.
Review the syntax for using a cell array! use curly brackets {}, like this
uint8)uint8 values.
Remember this: the smallest type uint8 value is 0--no
negatives--and the upper bound is 255.
If you subract two uint8
values you may get an underflow, e.g.,
x=uint8(100)-uint8(150) results in x getting the
value 0, not -50. If you need to subtract, you should do so in type
double.
Another common error occurred in doing the interpolation. Different
methods may be used to do the interpolation, and one is
is to add a fraction of the difference between the left and right data:
left + fraction*(right-left) or
left + integerMultiplier*(right-left)/3
but a number of students did the subtraction incorrectly as
left-right instead of right-left.
See the posted solutions for a method that avoids
the difficulty of subtracting uint8 values (the same method
as discussed earlier in the semester for linear interpolation).
A number of students neglected the last column of the array. Some
students neglected the third dimension--handling the three layers--by using
just two indices with the array. Note that it is NOT necessary to handle
the 2- and 3-dimensional cases separately. A component of a 2-d array
A
can be accessed as A(rowIndex,colIndex) as well as
A(rowIndex,colIndex,1), i.e., there is only one layer in the
third (layer) dimension.
chars, cell array, struct, and struct array)MakeRoom to create a struct, and
strfind to get the positions
of the spaces in a string.
strfind pair by pair--and
handling the last unpaired space index in the string. Many students used
incorrect indices: indexing poorly when creating a substring would mean that
the roomID contains extra spaces/characters at the beginning and end.
% Efficient
n = length(Q);
for i = 1 : n - 2
for j = i + 1 : n - 1 % j starts at i+1
for k = j + 1 : n % k starts at j+1
% do stuff
end
end
end
% Inefficient
n = length(Q);
for i = 1:n
for j = 1:n
for k = 1:n
if i < j && j < k
% unique combination. do stuff
end
end
end
end
In the second (inefficient) approach, the if-statement still has to
execute n3 times even though there are far fewer cases
for "doing stuff".
Other smaller issues... To print several variable values in one line, an easy way is to use
fprintf with appropriate format sequences. A number of
students incorrectly used size to determine the length
of the 1-d struct array Q. The problem is that the
question does not specify whether Q is a row or column
1-d cell array. If Q is a row cell array of length 5,
then size(Q) returns the vector [1 5]; if
Q is a column of length 5, then [5 1] would be returned.
So if you used size(Q), then you would need to write more
code to determine which is really the length of Q.
The correct and SIMPLE thing to do is to use
function length to determine the length of a
ONE-D array, not function size.
% Action to take after prelim
if yourScore > 88
toDo= celebrate + later look at the solution
elseif yourScore > 67
toDo= redo problems + later check solution
else
toDo= meet with course staff to go over exam ...
+ stay caught up from now on ...
+ do NOT just read the solution--must redo problems
end