Solutions to Prelim 1 Review Questions NOTE: the original solutions posted to the newsgroup had some errors. We strongly encourage you to solve the problems before looking at the solutions below. It is hard, if not impossible, to learn programming by only looking at examples developed by others. In general, there are many solutions to any problem. We show only some of these, which might not be the ones you came up with. As long as your programs generate the correct result and satisfy the style requirements, they are as good as the ones below. _______________________________________________________________________________ >> Problem 1: Solution 1: in = 'this is the string whose vowels should be replaced'; out = []; % check each character in the string for c = in if c == 'a' out = [out '*']; elseif c == 'e' out = [out '*']; elseif c == 'i' out = [out '*']; elseif c == 'o' out = [out '*']; elseif c == 'u' out = [out '*']; else out = [out c]; end end % show the result out Solution 2: in = 'this is the string whose vowels should be replaced'; out = []; % check each character in the string for c = in if (c == 'a') | (c == 'e') | (c == 'i') | (c == 'o') | (c == 'u') out = [out '*']; else out = [out c]; end end % show the result out Solution 3: in = 'this is the string whose vowels should be replaced'; out = []; % check each character in the string for c = in % use a trick to determine whether the current character % belongs to a set of characters (now, vowels). if sum('aeiou' == c) == 1 out = [out '*']; else out = [out c]; end end % show the result out Note: Even more compact solutions can be found. _______________________________________________________________________________ >> Problem 2: in = 'this is the string whose vowels should be replaced'; out = []; % check each character in the string for c = in % use a trick to determine whether the current character % belongs to a set of characters (now, vowels). if sum('aeiou' == c) == 1 out = [out c c]; else out = [out c]; end end % show the result out Note: All the ideas that solve problem 1 can be immediately adapted to solve this one. _______________________________________________________________________________ >> Problem 3: Solution 1: in = [6 0 -3 -5 4 5 1 -9 0 3]; pos = []; % positive numbers from $in$ neg = []; % negative numbers from $in$ % check each element and place it into the appropriate array if not zero for x = in if x > 0 pos = [pos x]; elseif x < 0 neg = [neg x]; end end % show results pos neg Solution 2: in = [6 0 -3 -5 4 5 1 -9 0 3]; pos = in(in > 0) neg = in(in < 0) _______________________________________________________________________________ >> Problem 4: Solution 1: in = [1 3 2 5 7]; % take care of the first element, which is special out = (in(2) + in(end))/2; % now deal with the 'regular' elements for i = 2:length(in)-1 out = [out (in(i-1)+in(i+1))/2]; end % now take care of the last element, which is also special out = [out (in(end-1)+in(1))/2]; % show the result out Solution 2: no loop, only vector operations. in = [1 3 2 5 7]; out = (in([end 1:end-1]) + in([2:end 1])) / 2 _______________________________________________________________________________ >> Problem 5. % the array below holds the number of days in each month days = [31 28 31 30 31 30 31 31 30 31 30 31]; % loop through each month for month = 1:12 % loop through each day of the month for day = 1:days(month) disp([ '[' num2str(month) ' ' num2str(day) ']' ]); end end _______________________________________________________________________________ >> Problem 6. Note that A is complementary to T, and G is complementary to C. % input string in = 'taCaGCgATAcgtg'; % this will store the output string out = []; % we convert the string to upper case to simplify comparisons for c = upper(in) if c == 'A' out = [out 'T']; elseif c == 'T' out = [out 'A']; elseif c == 'G' out = [out 'C']; else out = [out 'G']; end end % show the resulting string out _______________________________________________________________________________ >> Problem 7. % note: matlab pre-defines $pi$ = 3.14159... N = 100; % number of terms to consider d = 67; % angle in degrees r = pi * d / 180; % angle in radians cosine = 0; % approximation so far (avoid 'cos' because it is a function name) % add all elements for i = 0 : N-1 cosine = cosine + (-1)^i*r^(2*i)/factorial(2*i); end % and this is the value we have computed cosine _______________________________________________________________________________ >> Problem 8. Solution 1: N = 20; x = []; for i = 1:N x = [x floor(5*rand)-2]; end x Solution 2: N = 20; floor(5*(rand(1,N))-2) ================================================== >> Problem 9. Solution 1: N = 100; % the number of steps we will simulate x = 0; y = 0; % current coordinates of the drunkard % simulate for i = 1:N % generate a random number in the range 1..4 step = ceil(4*rand); if step == 1 % step to the right x = x + 1; elseif step == 2 % step to the left x = x - 1; elseif step == 3 % step up y = y + 1; else % step down y = y - 1; end end % compute and show distance reached ($sqrt$ is the square root function) sqrt(x*x + y*y) Solution 2: % note: matlab pre-defines $i$ = sqrt(-1) N = 100; % the number of steps we will simulate % simulate N random steps: position (x,y) is represented by x + i y pos = sum(i .^ floor(4*rand(1,N))); % compute and show distance reached abs(pos)