Comments on Prelim 2

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.

Question 1

Both Parts a (trace of nested loops on matrix) and b (loop pattern for matrix traversal and the algorithm for finding the best in a set) were very well done. Good job!

Question 2

(a) 2-d simple array of 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
arrayName(rowIndex,colIndex) % a single component
arrayName(rowIndex, colStartIndex:colEndIndex) % a row subarray

(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

cellArrayName{rowIndex,colIndex} % the content of one cell

Question 3 (3-d array, computing with type uint8)

This question was generally well done. The most common error was mishandling the subtraction of type 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.

Question 4 (chars, cell array, struct, and struct array)

This was the most challenging question on the prelim and tested many of the concepts and skills that you learned in Project 5. Students did well on A number of students had trouble with the case where more than one room definition occurs in a single cell in the input cell array--using a loop to process the returned values from 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.

Question 5 (struct array and nested loops for "all UNIQUE combinations")

This question was generally well done. Students did well on dealing with the conditional (if statement and boolean expression) and understanding the use of nested loops to get all combinations. Of the algorithmic problems that we saw, the most common was inefficiently using a conditional to rule out the duplicates instead of efficiently using progressive indices in the loop ranges:
  % 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.

Statisics

Max: 100
Median: 85
Mean: 80.0
S.Dev: 16.3
% 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