CS100M Fall 2006 Sample Prelim 2 Solution
Question 1:
(15 points)
Part (a):
(5 points)
What does the vector
v
look like after the following script is executed?
v = [3 1 2];
Before:
3
1
2
for k = 1:3
v( v(k) ) = v(k);
end
After:
___
1_1_3_
______________
Part (b):
(5 points)
What will be printed when the following script is executed?
Script
Function
w=1;
x=4;
y=7;
z= work(y,x);
disp(sprintf('w is %d\n', w))
disp(sprintf('x is %d\n', x))
disp(sprintf('z is %d\n', z))
function w = work(x,y)
x= x  y;
w= x;
disp(sprintf('y is %d\n', y))
Output:
y is 4
w is 1
x is 4
z is 3
Part (c):
(5 points)
Assume that the following function is available:
function [a, b] = twoBiggest(v)
% Scalars a and b are the two biggest numbers in vector v.
% The length of v is greater than 2.
Add code to the program fragment below to store in a variable
s
the sum of the two biggest numbers in
vector
w
.
You must use function
twoBiggest
.
w= rand(1,20);
[a, b]= twoBiggest(w);
s= a + b;
2
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentCS100M Fall 2006 Sample Prelim 2 Solution
Question 2:
(25 points)
Complete the script below to estimate the winning probabilities for two players in a dice game.
The
number of occurrences of a specific outcome divided by the total number of experimental trials is the
estimated probability of that outcome.
Note:
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '07
 FAN/VANLOAN
 Randomness, Vector Motors, Alice and Bob, Statistical randomness, sample prelim

Click to edit the document details