Вы находитесь на странице: 1из 5

Fall 2013 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.
v = [3 1 2];
w = [4 3 2 2];
for k = 1:3
z(k) = v(k) + w(k+1);
for i = 1:4
w(i) = w(i)*2;
end
end
disp(w)
disp(z)

Output:
32
6

24
5

16
10

16

(b) What will be printed when the following script and function are executed? Use the specified print
format.
Script
a = 1; b = 4; c = b+a;
d = boo(a,a);
fprintf(a is %d\n, a);
fprintf(c is %d\n, c);
fprintf(d is %d\n, d);

Function
function d = boo(b,c)
a = b+c;
c = b+a;
d = a;
fprintf(c is %d\n, c);

Solution:
c
a
c
d

is
is
is
is

3
1
5
2

(c) Write a boolean expression on the blank below so that the resulting fragment keeps prompting
the user to enter a number until the value entered is odd and positive, or until 20 numbers have been
entered, whichever occurs first.
n = input(Enter a positive, odd value: );
k = 1;
while

______________________________________________________________________

n = input(Enter a positive, odd value: );


k = k+1;
end
Example solutions:
( rem(n,2)==0 || n<=0 ) && k< 20
~ ( ( rem(n,2)==1 && n> 0 ) || k>=20

% parens necessary as && before ||


% k==20 also ok, inner parens not necessary

)
2

Fall 2013 Prelim 1 Solutions

Question 2: (15 points)


Given two vectors of the same length, we say that they match if each pair of corresponding components
in the two vectors has the same value. For example, of the three vectors given below, b and c match,
but b and d do not match.
b = [5 3 7 3]
c = [5 3 7 3]
d = [5 7 3 3]
Write a fragment below to determine whether two given vectors, f and g, match. The fragment should
print the message f and g match if the two vectors match; otherwise print the message f and g do
not match. For full credit, use a while-loop to efficiently solve this problem. Do not use any built-in
function other than length. Do not use vectorized code.
%
%
%
%
%

Assume vectors f and g have been created. Vectors f and g have the same
length and the length is greater than 2.
Write a fragment below to determine whether f and g match and print an
appropriate message.
DO NOT use any function other than length. DO NOT use vectorized code.

Example solution:

n= length(f);
k= 1;
while k<=n && f(k)==g(k)

% Check for valid index BEFORE using the index


% to access an element in a vecotr

k= k + 1;
end
if k<=n
disp(f and g do not match)
else
disp(f and g match)
end
% Less efficient:

doing extra conditionals/printing inside loop

% Very inefficient: iterate through entire vector


% BREAK is not allowed

Fall 2013 Prelim 1 Solutions

Question 3: (20 points)


(a) Implement the following function as specified:
function out = roll2dice()
% out is the outcome of rolling two fair 6-sided dice once. The outcome
% is the sum of the two faces that show up.
% The only functions allowed are rand, ceil, floor, round, and sum.
% Example solution:
out= sum( ceil(rand(1,2)*6) );
(b) Estimate and print the probabilities of three scenarios when two fair 6-sided dice are rolled:
Scenario A: the outcome is odd
Scenario B: the outcome is greater than 9
Scenario C: the outcome is even and less than 7
Complete the fragment below to estimate the probabilities using simulation. The estimated probability of an event is the number of times that the event
occurs divided by the total number of trials done. Make effective use of function roll2dice (assume it has been implemented correctly and is accessible).

N = input(Enter the number of trials: );


% Assume that N is a suitably large integer.

Write your code below.

% Example solution:
nA=0; nB=0; nC=0;
for k= 1:N
out= roll2dice();
if

rem(out,2)==1 % A: odd
nA= nA + 1;

elseif

out<7

% C: even and <7, compound condition not necessary

nC= nC + 1;
end
if

out>9
nB= nB + 1;

% B: >9

end
% An alternative: use independent if-statements:
% if rem(out,2)==1
%
nA= nA + 1;
% end
% if out>9
%
nB= nB + 1;
% end
% if rem(out,2)==0 && out<7
% must use compound condition
%
nC= nC + 1;
% end
end
nA= nA/N;

nB= nB/N;

nC= nC/N;

fprintf(Probability of Scenario A is %f\n, _______ nA _________ )


fprintf(Probability of Scenario B is %f\n, _______ nB _________ )
fprintf(Probability of Scenario C is %f\n, _______ nC _________ )

Fall 2013 Prelim 1 Solutions

Question 4: (20 points)


(a) The distance between two points (xa , ya) and (xb , yb ) is
following function as specified:

(xa xb )2 + (ya yb )2 . Implement the

function [maxd, ii] = furthest(x,y)


% x and y are vectors of the same length storing the x and y coordinates of a
% set of points. E.g., (x(k),y(k)) are the coordinates of the kth point in
% the set.
% ii is the index of the point in the set that is furthest from the origin,
% and maxd is the distance of that point from the origin.
% Assume that the distances of the points from the origin are different.
% DO NOT use built-in functions max and min.
% Example solution:
maxd= -1;
for k= 1:length(x)
d= sqrt(x(k)^2 + y(k)^2);
if

d > maxd
maxd= d;
ii= k;

end
end

(b) Suppose vectors xpos and ypos store the x and y coordinates of 30 points. Assume that the
distances of the points from the origin are different. Complete the fragment below to determine which
point is furthest from the origin and print the result. Make effective use of function furthest (assume
it has been implemented correctly and is accessible).
xpos= 10*rand(1,30);
ypos= 10*rand(1,30);
% Write your code below.

% Example solution:
[maxd, i] = furthest(xpos,ypos);

fprintf(The x-coordinate of the furthest point is %f\n., __ xpos(i) ______ )


fprintf(The y-coordinate of the furthest point is %f\n., __ ypos(i) ______ )
fprintf(The distance of the furthest point from the origin is %f\n., ___ maxd __ )
5

Question 5: (30 points)

Fall 2013 Prelim 1 Solutions

10

Complete the function below as specified. Do not use


any built-in functions other than length, zeros, sin,
and cos. The diagram on the right shows an example
graphic produced by the function call
stars(4, [1 1 0], [.3 .3 .3]) .
Assume the availability of the function DrawStar. For
example, the command
DrawStar(3,2, .5, [1 1 0])
draws a yellow star of radius 0.5 centered at (3,2).
Your code draws only the stars; do not draw the dotted lines indicating the rings on which the centers of
the stars lie. Math note: A point on a circle with radius r centered about the origin has the Cartesian coordinates (r cos(), r sin()) where is an angle measured from the positive x-axis.

10
10

function stars(n,inCol,outCol)
% Draw n rings of stars centered about the origin. n is an integer greater than 2.
% inCol and outCol are each a vector storing rgb values. The stars on the innermost
% ring have the color inCol; the stars on the outermost ring have the color outCol;
% the rings of stars in between vary uniformly in color (linearly interpolated).
% The innermost ring has 5 stars; each subsequent ring doubles the number
% of stars of the previous ring. Each star has radius 0.5.
% The radii of the rings, starting from the innermost ring, are 2,4,6,...
% DO NOT use any built-in functions other than length, zeros, sin, and cos.
close all; figure; axis equal; hold on

Example solution:
numStars= 5;
r= 2;

% #stars in a ring, starting at innermost ring


% radius of ring from center

for k= 1:n
% Color of stars in kth ring
f= (k-1)/(n-1);
colr= f*outCol + (1-f)*inCol;
% Draw kth ring of stars
theta= 2*pi/numStars;
% Can directly calculate ring diameter:
for i= 1:numStars
x= r*cos(i*theta);
y= r*sin(i*theta);
DrawStar(x,y,.5,colr)
end
numStars= numStars*2;
r= r+2;
end
6

r= 2*k

10

Вам также может понравиться