Professional Documents
Culture Documents
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.
(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
for k= 2:length(v)-1
s(k-1)= (v(k-1) + v(k) + v(k+1))/3;
end
data= zeros(1,16); %
%
%
for k = 1:1000
a=ceil(rand*6);
c=ceil(rand*6);
% 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.
k= 0;
while
% 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
0
0
% 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