1 a) (8 points - 2 for each part) names = {'Joe', 'Bill', 'Sammy', 'Mark', 'Jim'}; grades = [62, 37, 99, 100, 16]; [sortedGrades, Indices] = sort(grades); sortedNames = cellstr(char(names{Indices}))'; (Note: Allow full credit even if students do not do the cell to string and back to cell conversion. Give a bonus point if they do. This applies to sortedNames (above) and permNames (below). ------------------------------------------------------------ 1 b) (2 points) permNames = cellstr(char(names{randperm(5)}))'; ============================================================ 2 a) (2 points) a([1:2, 4:end]) results in [52, 36, 99, 5] ------------------------------------------------------------ 2 b) (2 points) b = [b, a(3)] results in [52, 36, 99, 5, 17] ------------------------------------------------------------ 2 c) (4 points) function [big rest] = extract(x) [big p] = max(x); rest = a([1:p-1, p+1:end]); or [a(1:p-1) a(p+1:end)]; end ------------------------------------------------------------ 2 d) (2 points) sortAscending (or simply sort) ============================================================ 3 a) (3 points) oil = pic >= 8; ------------------------------------------------------------ 3 b) (3 points) area = sum(sum(oil)); ------------------------------------------------------------ 3 c) (4 points) (Note there are many solutions. Here is just one.) (Assume rows = number of rows in array oil) hasOil = (oil * ones(rows, 1)) > 0; % column vector, non-0 => oil oilRows = hasOil .* [1:rows]'; % non-0 is row number northRow = min(oilRows(hasOil)); % minimum non-0 value ------------------------------------------------------------ 3 d) (4 points) oiln = oil; oiln(1:end-1,:) = oiln(1:end-1,:) | oil(2:end,:); oiln(2:end,:) = oiln(2:end,:) | oil(1:end-1,:); oiln(:,1:end-1) = oiln(:,1:end-1) | oil(:,2:end); oiln(:,2:end) = oiln(:,2:end) | oil(:,1:end-1); ------------------------------------------------------------ 3 e) (3 points) oile = oiln; oile(:,2:end) = oile(:,1:end-1); oile(:,1) = 0; ------------------------------------------------------------ 3 f) (3 points) oile(rWell, cWell) = true; ============================================================ 4 a) (2 points) (Note - for convenience, abbreviate multiOp to just mOp.) mOp(mOp(mOp(2, mOp(x, 2, '^'), '*'), mOp(mOp(5, x, '*'), y, '*'), '+'), ... mOp(3, mOp(y, 2, '^'), '*'), '-') ------------------------------------------------------------ 4 b) (2 points) mOp(mOp(mOp(2, '*', mOp(x, '^', 2)), '+', mOp(mOp(5, '*', x), '*', y)), ... '-', mOp(3, '*', mOp(y, '^', 2))) ------------------------------------------------------------ 4 c) (2 points) mOp('-', mOp('+', mOp('*', 2, mOp('^', x, 2)), mOp('*', mOp('*', 5, x), y)), ... mOp('*', 3, mOp('^', y, 2))) ------------------------------------------------------------ 4 d) (3 points) prefix: - + * 2 ^ x 2 * * 5 x y * 3 ^ y 2 infix: 2 * x ^ 2 + 5 * x * y + 3 * y ^ 2 (allow parentheses) postfix: 2 x 2 ^ * 5 x * y * + 3 y 2 ^ * - ============================================================ 5 a) (4 points) The number 1 is divisible by one The number 2 is divisible by two The number 3 is divisible by three The number 4 is divisible by two The number 5 is divisible by one The number 6 is divisible by six The number 7 is divisible by one The number 8 is divisible by two The number 9 is divisible by three The number 10 is divisible by two ------------------------------------------------------------ 5 b) (3 points) fprintf('The number %d is divisible by %s', ii, f{1+d2+d3}); ------------------------------------------------------------ 5 c) (3 points) ii = 1; while ii <= 10 d2 (stuff) d3 (stuff) fprintf (stuff) ii = ii + 1; end ============================================================ 6 a) (5 points) Cross off portions shown in upper case: ... elseif GRADE > 50 && grade <= 60 (first test and &&) disp('An E for effort ... elseIF GRADE > 70 && GRADE <= 80 (all after the else) disp('Average C') end ------------------------------------------------------------ 6 b) (5 points) (Covering a possible grade of 0 entails an extra trick. If students recognize the problem exists, give a bonus. If they solve it, give an extra bonus.) grade = input('Enter the grade from 0 to 100: '); switch ceil(grade/10 + (grade == 0)) (full credit for ceil(grade/10)) case {1,2,3,4,5} disp('Sorry - you blew it') case 6 disp('An E for effort is all you get') case 7 disp('Discouraging D') case 8 disp('Average C') case 9 disp('Not a bad B') case 10 disp('Nice A') otherwise disp('Invalid input') end ============================================================ 7 a) (4 points) count = 2x^3 + x^2 ------------------------------------------------------------- 7 b) (3 points) nesting is 3 deep ------------------------------------------------------------- 7 c) (3 points) Nesting of 'for' structures is the same as highest exponent. ============================================================= 8 a) (5 points) 0 hello 3 intwo 6 inthree 3 midtwo 6 inthree 3 outtwo 0 ciao 3 inthree 0 done ------------------------------------------------------------- 8 b) (5 points) Function definitions have arbitrary names as placeholders for arguments. There is no need to use those names when invoking the function. ============================================================= 9 a) (5 points) 0 hello 3 intwo 6 inthree 9 intwo 12 inthree 12 outthree 9 midtwo 12 inthree 12 outthree 9 outtwo 6 outthree 3 midtwo 6 inthree 9 intwo 12 inthree 12 outthree 9 midtwo 12 inthree 12 outthree 9 outtwo 6 outthree 3 outtwo 0 ciao 3 inthree 6 intwo 9 inthree 9 outthree 6 midtwo 9 inthree 9 outthree 6 outtwo 3 outthree 0 done ------------------------------------------------------------- 9 b) (5 points) 6 workspaces maximum (The functions nest 5 deep, implying 5 workspaces, however, the main command level really adds another workspace. The correct answer is 6, but 5 is acceptable at a cost of 2 points.) ============================================================= 10 a) (3 points) (Can not be done as requested. The following comes close, actually involving a cell array and yielding three triangles. Give full credit if answer is reasonable. Give a bonus if student recognizes this is not possible.) triangle = struct('color','red','coordinates', ... {struct('x',1,'y',3,'z',5), ... struct('x',7,'y',9,'z',13), ... struct('x',3,'y',5,'z',8)}); ------------------------------------------------------------- 10 b) (3 points) function d = dist3(p1,p2) d = ((p1.x-p2.x)^2 + (p1.y-p2.y)^2 + (p1.z-p2.z)^2)^0.5; end ------------------------------------------------------------- 10 c) (4 points) (The t1(n) and t2(n) of the first half dozen assignments presume the modified structure given in part a. Allow full credit if student has something reasonable, such as t1.coordinates(3).) function v = similar(t1, t2) d1(1) = dist3(t1(1).coordinates, t1(2).coordinates); d1(2) = dist3(t1(2).coordinates, t1(3).coordinates); d1(3) = dist3(t1(3).coordinates, t1(1).coordinates); d2(1) = dist3(t2(1).coordinates, t2(2).coordinates); d2(2) = dist3(t2(2).coordinates, t2(3).coordinates); d2(3) = dist3(t2(3).coordinates, t2(1).coordinates); v = sim(d1, d2(1),d2(2),d2(3))| ... sim(d1, d2(1),d2(3),d2(2))| ... sim(d1, d2(2),d2(1),d2(3))| ... sim(d1, d2(2),d2(3),d2(1))| ... sim(d1, d2(3),d2(1),d2(2))| ... sim(d1, d2(3),d2(2),d2(1)); end function s = sim(t, s1, s2, s3) p1 = t(1)/s1; p2 = t(2)/s2; p3 = t(3)/s3; s = abs(p1-p2)<0.01 & abs(p2-p3)<0.01 & abs(p3-p1)<0.01; end function d = dist3(p1, p2) d = ((p1.x - p2.x)^2 + (p1.y - p2.y)^2 + (p1.z - p2.z)^2)^0.5; end ============================================================= 11 a) (2 points) Event driven programming involves routines that are triggered when the user pokes buttons, enters text, etc. ------------------------------------------------------------- 11 b) (2 points) GUI, meaning Graphical User Interface, involves a screen with components that can respond to user manipulation, say, with a mouse. ------------------------------------------------------------- 11 c) (2 points) Event driven programming goes hand-in-hand with GUI. GUI provides the interactive screen, and event driven programming provides the programatic responses to actions on the screen. ------------------------------------------------------------- 11 d) (1 point) A call back function embodies the code that is executed in response to user action on a GUI. ------------------------------------------------------------- 11 e) (1 point) A handle is a reference or pointer to something, such as a GUI component. The programmer can access the properties of the component through the handle, typically with get and set functions in Matlab. ------------------------------------------------------------- 11 f) (1 point) GUIDE builds a struct called 'handles' which is made available to all the call back functions as an argument. 'handles' contains the handles of all the GUI components, plus anything else the programmer wishes to include. The advantage is in not having to make each of the handles global. ------------------------------------------------------------- 11 g) (1 point) 'handles' is revealed to be a struct by the use of the .field notation which is typical of structs.