Unformatted text preview: 3 or 5 in the input vector. For instance, ‘ThreeFiveSum([1 2 3 4 5 6 7 8 9 10])’ should return (3+5+6+9+10) = 33. (Hint: You can find out if a number ‘x’ is a multiple of a number ‘y’ with the ‘mod’ function. If ‘mod(x,y) == 0’, then x is a multiple of y.) function ret = ThreeFiveSum(v)
ret = 0;
for i = 1:length(v)
if (mod( v(i), 3) == 0)  …
(mod( v(i), 5) == 0) % check for multiple
ret = ret + v(i);
end
end
(Continued on next page) Problem 2. Continued 2. [4 pts] Write a function ‘mostlySixes’ that returns ‘true’ if greater than or equal to 50% of the elements in the input vector ‘v’ are sixes, but returns ‘false’ if less than half of the elements are sixes. function ret = mostlySixes(v)
ret = false;
sixCount = 0;
for i=1:length(v)
if (v(i) == 6) % check if six
sixCount = sixCount + 1;
end
end
% See if there were >=50% sixes
% Lots of ways to do this
if (2 * sixCount >= length(v))
ret = true;
end...
View
Full
Document
This note was uploaded on 08/14/2012 for the course COMP 116 taught by Professor Stotts during the Fall '08 term at UNC.
 Fall '08
 Stotts

Click to edit the document details