Fall 2007 Prelim 2 Sol - CS100M Fall 2007 Prelim 2 Solution...

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

View Full Document Right Arrow Icon
CS100M Fall 2007 Prelim 2 Solution Question 1: (30 points) Part (a): (2 points) What does vector v look like after the following script is executed? v = [0 1]; Before: 0 1 for k = 1:3 v = [1 v]; end After: ___ 1 1 1 0 1_ _____ Part (b): (2 points) What does vector w look like after the following script is executed? w= [3 2 1]; Before: 3 2 1 w(w(3)) = w(1); After: ___ 3 2 1 _______________ Part (c): (10 points) Assume that a and b are initialized scalars with a < b . Consider the following code fragment: x= linspace(a,b,n); y= sin(x); Write an equivalent fragment that does not use function linspace and only calls the sine function with scalar input values. h= (b-a)/(n-1); for k= 1:n x(k)= a + (k-1)*h; y(k)= sin(x(k)); end Question 1 continues on next page 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
Question 1, continued Part (d): (6 points) Assume that score is an initialized vector containing integer values in the interval [0,100]. (For example, score is a vector of student scores on a test). Write one statement on the blank below to complete the code fragment for drawing a histogram of the scores (with one bar for each score values 0, 1, 2, …, 100). count= zeros(1,101); % count will be used to store the histogram data for k= 1:length(score) count(score(k)+1)= count(score(k)+1) + 1; end bar(0:100, count) % draw a histogram of the scores Part (e): (5 points) Given the following function: f = 11 Output: function f = evaluateQuadratic(a,b,c,x) f= a*(x^2) + b*x + c; What is the output when the following script is executed? a=1; b=-1; c=3; x=2; f= evaluateQuadratic(c,b,a,x) Part (f): (5 points) Given the following function: function y = flip(x) n= length(x); for k= 1:n x(n-k+1)= x(k); y = 10 20 20 10 Output: end y= x; What is the output when the following script is executed? y= [10 20 30 40];
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 note was uploaded on 02/28/2008 for the course CS 100 taught by Professor Fan/vanloan during the Spring '07 term at Cornell University (Engineering School).

Page1 / 6

Fall 2007 Prelim 2 Sol - CS100M Fall 2007 Prelim 2 Solution...

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