CS 100 M Spring 2007 Prelim 2 Solutions

CS 100 M Spring 2007 Prelim 2 Solutions - CS100M Spring...

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

View Full Document Right Arrow Icon
CS100M Spring 2007 Sample Prelim 2 Solutions Question 1 (24 points; 4 each ) In each box below, write the output for the code on the left. If there is an error, just write the word “error”. y = 3 5 7 0 99 s = ple ss janfebmar L = 1 1 1 0 0 0 (Expression a<b here is vectorized code) error error w = [7 0]; M = [w; w w] u = [1 2 3]; m = [u; 6:9] a = 1:6; b = 6:-1:1; L = (a < b) c = ['jan' 'feb' 'mar'] s = 'sample string'; s = s(4:8); s(length(s)+1) = 's' y = [3 5 7]; y(5) = 99 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 Spring 2007 Sample Prelim 2 Solutions 3 Question 2 ( 18 points; 6 each ) For all parts of this question, do not use vectorized code; we want to see that you can solve the problems using iteration (i.e., using loops). A. Write a function countMultiples(x, v) that counts and returns the number of values in vector v that are multiples of scalar x. function n = countMultiples(x, v) % n is the number of values in vector v that are multiples of scalar x n= 0; for k= 1:length(v) if rem(v(k),x)==0 n= n+ 1; end end B. Write a function isIncreasing(v) that determines whether the numbers in vector v are in increasing (ascending) order. The function returns 1 if v is in increasing order and 0 otherwise. For example, the
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 test prep 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 / 5

CS 100 M Spring 2007 Prelim 2 Solutions - CS100M Spring...

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