Академический Документы
Профессиональный Документы
Культура Документы
(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
______________________________________________________________________
)
2
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)
k= k + 1;
end
if k<=n
disp(f and g do not match)
else
disp(f and g match)
end
% Less efficient:
% 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
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;
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);
10
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;
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