You are on page 1of 5

Fall 2014 Prelim 1 Solutions

Question 1: (15 points)


(a) What is the output from executing the following script? If the program doesnt terminate or if
there will be an error during execution, write the word error instead of the output.
a = [5 7 3 6];
b = [2 1 3];
for i = 4:-1:2
if i < a(i) || a(i) < b(i-1)
disp(a(i)*100)
for j = 1:3
disp(b(j)*10)
end
end
end

Solution:
600
20
10
30
700
20
10
30

(b) Complete the statement below to assign to r a randomly generated value such that r is equally
likely to be any value in the open interval (-7,9). Do not write additional statements.

r = ____ rand*16 - 7 _____________________________ ;

(c) What will be printed when the following script is executed? Use
the specified print format.
Script
x = 9;
y = 2;
z = foo(x,y);
fprintf(y is %d\n, y)
fprintf(z is %d\n, z)

Function
function y = foo(z,x)
z = z-x;
y = x+x;
fprintf(x is %d\n, x)
fprintf(y is %d\n, y)
fprintf(z is %d\n, z)

Solution:
x
y
z
y
z

is
is
is
is
is

2
4
7
2
4
2

Fall 2014 Prelim 1 Solutions

Question 2: (15 points)


(a) The script below will display 10 randomly generated numbers when executed. Write another script
that displays only the smallest of ten randomly generated numbers. Do not use any function other
than rand.
% Display 10 random numbers
for k = 1:10
n = rand;
disp(n)
end
Solution:
minSoFar = rand; % Or init to inf or realmax. Then loop 10 times not 9.
for k = 2:10
n = rand;
if n < minSoFar
minSoFar = n;
end
end
disp(minSoFar)

(b) Implement the following function as specified:


function s = smoothVector(v)
% Smooth vector v by averaging each "interior" value with its left and
% right neighbors.
% v: a vector of real values. Assume that the length of v is > 2.
% s: the smoothed vector that is two components shorter than v.
% For example, if v=[-2 5 3 4 8.6] then s=[2 4 5.2]
% Do NOT use any built-in functions other than length and zeros.
Solution:
s= zeros(1, length(v)-2);

% initialization not necessary

for k= 2:length(v)-1
s(k-1)= (v(k-1) + v(k) + v(k+1))/3;
end

Fall 2014 Prelim 1 Solutions

Question 3: (15 points)


(a) Write one statement on the blank below to complete the script for drawing a histogram of the outcome from rolling three fair, six-sided dice 1000
times. The outcome of rolling three six-sided dice is the sum of the three faces that show up.

data= zeros(1,16); %
%
%
for k = 1:1000
a=ceil(rand*6);

Vector to store the histogram data. There are 16 possible outcome.


data(i) is the number of times that the ith possible outcome occurs,
where i=1,2,...,16.
b=ceil(rand*6);

c=ceil(rand*6);

____ data(a+b+c-2) = data(a+b+c-2) + 1;

% SOLUTION ____

end
% Draw histogram
bar(3:18, data)
xlabel(Outcome of rolling 3 6-sided dice)
ylabel(Number of times each outcome occurs)
(b) A color can be represented by a vector of length three where each component is in the range of 0 to 1. Complete the fragment below to interpolate
between purple and orange. Specifically, display the values of n color vectors such that the first is purple, the last is orange, and the intermediate vectors
are linearly interpolated between purple and orange. Display the vector values only; do not use graphics. The command disp(v) displays the values in
vector v.

Do not modify the given code. You will write the loop body and, if necessary, additional initializations above the loop. Do not use any built-in functions
other than disp and length.

% Interpolate between purple and orange


n= input(Enter an integer that is greater than 2: );
pur= [ .4 .2 .6]; % color vector for purple
ora= [1.0 .4 .1]; % color vector for orange

% Assume n is an integer >2

% Example 1: use linear proportions


for k = 1:n
% Compute and display the kth color vector
f= (k-1)/(n-1);
v= f*ora + (1-f)*pur;
disp(v)
end
% Example 2: add to the "base color" some multiple of the delta difference
delta= (ora-pur)/(n-1);
for k= 1:n
v= pur + (k-1)*delta;
disp(v)
end
% Example 3: mostly non-vectorized version of Example 2
v= pur;
for k= 1:n
disp(v)
for i=1:3
v(i)= v(i) + (ora(i)-pur(i))/(n-1);
end
end

Question 4: (30 points)

Fall 2014 Prelim 1 Solutions

(a) Implement the following function as specified:


function out = flipCoinSet(pvec)
% Flip a set of possibly unfair coins. The length of vector pvec is the number of
% coins in the set. Flip each coin once.
% pvec: pvec(k) is the probability of getting heads on a flip of the kth coin.
%
Assume that 0 < pvec(k) < 1 and the length of vector pvec is > 1.
% out: out is the vector of outcomes after flipping the set of coins. out
%
has the same length as pvec. out(k) is 0 if the kth coin shows tails;
%
out(k) is 1 if the kth coin shows heads.
Solution:
out= zeros(1, length(pvec));
for k= 1:length(pvec)
if rand < pvec(k)
out(k)= 1;
end
end
(b) Complete the implementation of the following function as specified. For full credit, make effective use of
function flipCoinSet (assume it has been implemented correctly and is accessible).
function dvec = simGame(d)
% Simulate a game of flipping 3 fair coins where the player starts with d dollars.
% The player flips the set of coins a prescribed number of rounds or until he or she
% runs out of money (has $0), whichever happens first.
% The prescribed number of rounds is initially 7 but may change during the game.
% In each round, the player flips the set of three coins. If all three are heads, then
% the player wins $10; otherwise the player loses $1. Additionally if all three coins
% are tails, then the number of prescribed rounds increases by 4.
% d: The amount of money the player starts with, a positive integer.
% dvec: The amount of money that the player has during the game. dvec(k) is how much
%
the player has AFTER k rounds have been played.
nRounds= 7;

% initial prescribed number of rounds

k= 0;

% number of rounds so far

while

k<nRounds && d>0

out= flipCoinSet([.5 .5 .5]);


if out(1)==1 && out(2)==1 && out(3)==1

% sum(out)==3

d= d + 10;
else
d= d - 1;
if out(1)==0 && out(2)==0 && out(3)==0
nRounds= nRounds + 4;
end
end
k= k + 1;
dvec(k)= d;
end

% sum(out)==0

Question 5: (25 points)

Fall 2014 Prelim 1 Solutions


6

Complete the function below as specified. Do not


use any built-in functions other than length, zeros,
floor, and ceil. The diagram on the right shows an
example graphic produced by the statement
myTiles(5, 3, 2, 1, .5, .25)
Assume the availability of the function DrawRect. For
example, the command
DrawRect(4, 0, 2, 1, [.5 .5 .5])
draws a gray rectangle of width 2 and height 1 with
the lower-left corner at (4,0). The rgb vector for black
is [0 0 0]. Your code draws only the rectangles; do not
draw the grid lines.

0
0

function myTiles(nr, nc, w, h, dx, dy)


% Draw nr rows by nc columns of rectangles. Each rectangle has width w and
% height h. There is a horizontal gap of dx and a vertical gap of dy
% between rectangles. The lower left corner of the bottom left rectangle is
% at (0,0). The rectangles in the top half (including the middle row) are gray
% and the other rectangles are black.
close all; figure; axis equal; hold on
Example solution:
yChange= h + dy;
xChange= w + dx;
for r= 1:nr

% rectangle height plus vertical gap


% rectangle width plus horizontal gap

% bottom to top

y= (r-1)*yChange;
if r > r/2
colr= [.5 .5 .5];
else
colr= [0 0 0];
end
for c= 1:nc
x= (c-1)*xChange;
DrawRect(x, y, w, h, colr)
end
end
hold off
Alternative for calculating x and y:
Initialize y before outer loop; in each iteration increase by yChange.
Re-initialize x to 0 before inner loop; in each iteration increase by xChange.
OK to work column by column instead of row by row.
6

You might also like