% Test solutions to the spring 2016 Math 230 final and/or provide parts
% that aren't in other functions.


%% Question 1, the squiggle function.

% See file squiggle.m for the answer to the question.

% Not part of the test, but plot the squiggle function, so I can be sure I
% defined it right
n = 200;                                % Number of sample points
xs = linspace( -10, 10, n );
ys = zeros( 1, n );
for i = 1:n
    ys(i) = squiggle( xs(i) );
end
plot( xs, ys );


%% Question 2, total running time of drama films in a movie database

% Use the database from the exam to test the solution on
movies = { 'Gone Girl', 'Drama', 149; ...
            'Interstellar', 'Science Fiction', 169; ...
            'Bridge of Spies', 'Drama', 142 };
 
% Step through each movie, adding its time to a running total if it's a
% drama

totalTime = 0;
for m = 1 : size( movies, 1 )
    if strcmp( movies{m,2}, 'Drama' )
        totalTime = totalTime + movies{m,3};
    end
end

% Not required by the exam, but display the total time so I can see if it's
% right.
disp( totalTime )


%% Question 3, combining images.

% See files combineVectorized.m and combineLooping.m for the answers to
% the exam question.

% Read images from disk to test the two functions on
mars = imread( 'MarsLookoutPoint.jpg' );
baldwin = imread( 'Baldwin.jpg' );

% Test each function.
figure( 1 );
imshow( combineVectorized( mars, baldwin ) );

figure( 2 );
imshow( combineLooping( mars, baldwin ) );


%% Question 4, appointment calendar.

% I'd represent the appointment calendar as a cell array, with each row
% representing one appointment. Columns could be description, starting
% time, and ending time, in that order. For simplicity, all 3 can be
% strings, with the times in hour:minute format. (In a more realistic
% system, the times might be pairs of integers for hour and minute, to make
% it easy to do arithmetic with times elsewhere.) For example...

calendar = { 'Meet with Jane', '9:30', '10:00'; ...
             'Lunch', '12:00', '1:00' }

         
%% Question 5, animated car on race track.

% The basic animation is a loop that updates the car's position on the track,
% draws the car and track, and then pauses. I use vectors of points around
% the track both to draw the track and to find the car's position.

t = linspace( 0, 2 * pi, 200 );
trackX = 20 * cos( t );
trackY = 12 * sin( t );

for carTime = 1 : length( t )
    hold off;
    plot( trackX, trackY, 'b' );
    axis equal;
    axis( [ -25, 25, -15, 15 ] );
    hold on;
    drawCar( trackX( carTime ), trackY( carTime ) );
    pause( 1/30 );
end


%% Question 6, remove comments from lines of text

% See file "decomment.m" for the answer to the exam question.

% Here are some demonstrations of "decomment"
decomment( 'x = 0   # start counting from 0' )
decomment( 'x = 1' )
decomment( '# Nothing but a comment' )


%% Question 7, alternating Math 230 harmonic series

% See file "ahs.m" for the answer to the exam question.

% Here are some demonstrations of "ahs," using numbers slightly larger
% than the 1st, 2nd, and 3rd terms of the series as the error bounds.
ahs( 3.1 )
ahs( 1.6 )
ahs( 1.1 )