CS 100 M Fall 2006 Prelim 2 Solutions

CS 100 M Fall 2006 Prelim 2 Solutions - CS100M Fall 2006...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS100M 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:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This homework help was uploaded on 02/23/2008 for the course CS 100 taught by Professor Fan/vanloan during the Spring '07 term at Cornell University (Engineering School).

Page1 / 4

CS 100 M Fall 2006 Prelim 2 Solutions - CS100M Fall 2006...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online