Review the prelims as well as the review questions for the prelims!
% Given an array of Point objects Pts where each object has two properties,
% x and y, sort Pts so that the objects are in the order of
% increasing distance from (0,0)
% Write two different scripts to solve this problem:
% (a) Make effective use of built-in function sort.
% (b) Use the INSERTION SORT algorithm; do not use built-in function sort.
Consider the following function:
function y = mergeSort(x)
% x is a column n-vector.
% y is a column n-vector consisting of the values in x sorted
% from smallest to largest.
n = length(x);
if n==1
y = x;
else
m = floor(n/2);
% Sort the first half..
yL = mergeSort(x(1:m));
% Sort the second half...
yR = mergeSort(x(m+1:n));
% merge...
disp('Call merge')
y = merge(yL,yR);
end
How many lines of output are produced by the call y = mergeSort(rand(7,1))
?
Consider the function
function MeshTriangle(x,y,L)
% x and y are 3-vectors. L is a nonnegative integer.
% Adds to the current figure window a level-L partitioning of the
% triangle whose vertices are specified by the 3-vectors x and y.
% Assumes that hold is on.
if L==0
% No subdivision required...
fill(x,y,'y','linewidth',1.5)
else
% A subdivision is called for...
% Determine the side midpoints (a(1),b(1)), (a(2),b(2)), and (a(3),b(3))
a = [(x(1)+x(2))/2 (x(2)+x(3))/2 (x(3)+x(1))/2];
b = [(y(1)+y(2))/2 (y(2)+y(3))/2 (y(3)+y(1))/2];
% Color the interior triangle magenta...
fill(a,b,'m','linewidth',1.5)
% Apply the process to the three "outer" triangles...
MeshTriangle([x(1) a(1) a(3)],[y(1) b(1) b(3)],L-1)
MeshTriangle([x(2) a(2) a(1)],[y(2) b(2) b(1)],L-1)
MeshTriangle([x(3) a(3) a(2)],[y(3) b(3) b(2)],L-1)
end
Assume that the length-3 vectors x and y define an equilateral triangle and that MeshTriangle(x,y,2)
is executed. What fraction of the original triangle is displayed yellow?
Suppose z = MyF(x)
is a function that accepts a real value x and returns a real value y. Assume that MyF() is very expensive to evaluate. Write a script that sets up an n-by-n array Z with the property that Z(i,j)
is assigned the value of MyF(1+abs(i-j))
. Assume that n is initialized.
Assume that insertSort(x)
is faster than mergeSort(x)
if the length of the input vector is less than 500. How would you modify the mergeSort() function to make it more efficient?
% Let A be a connectivity matrix, i.e.,
% if A(i,j) is one, then there is a link on webpage j
% to webpage i. Write a fragment that prints "yes" if it is
% possible to go from web page #100 to webpage #200 in one or two clicks.
% Assume that the number of webpages is >=200.
% Thus, if A(101,100) = 1 and A(200,101) = 1 then "yes".
Refer to class Interval from Lecture 22. Implement the following function:
function idxs = greatestOverlap(iArray)
% Find the biggest pairwise overlap between Intervals in iArray.
% iArray is an array of Interval references. iArray has a length greater than 1.
% idxs is a vector of length 2 storing the indices of the two Intervals in
% iArray that overlap the most. If there is not a pair of overlapping
% Intervals in iArray, idxs is the empty vector.
% Write efficient code: avoid unnecessary iteration.
Implement the following class as specified:
classdef LengthUnit < handle
% A LengthUnit represents a length (distance) in imperial units: feet and
% inches. The values are non-negative and inches must be less than 12.
properties
feet % a non-negative integer value
inches % a non-negative value
end
methods
function L = LengthUnit(ft, in)
% Constructor: Create a LengthUnit object with ft feet and in inches.
% If one or both parameters are negative, halt the program and
% display an error message.
%--Write your code here
end
function ft = inFeet(self)
% self references a LengthUnit. ft is self represented in feet.
%--Write your code here
end
function L = add(self, other)
% self and other reference LengthUnits. L references a new
% LengthUnit that is the sum of self and other.
%--Write your code here
end
function yesno = isLongerThan(self, other)
% self and other reference LengthUnits. yesno is true (1) if
% self is longer than other; otherwise yesno is false (0).
% For full credit, make effective use of method inFeet.
%--Write your code here
end
end %methods
end %classdef
Write a script that
Errata: P14.1.3 part (b) should read “... then the reverse of s is the concatenation of the reverse of s(2:n) and s(1) in that order.” The subvector was incorrectly specified as s(1:n-1).
Please see the Insight Errata link for the full errata list.