% Hawk Waiting Times

% Generate a sample of 10 waiting times
T = -0.5 * log(1-rand(1,10));

% Calculate times of sightings
S = 5 + cumsum(T);

% Display waiting times in terms of hours and minutes
fprintf('\nWaiting Times  //  Sighting Times\n')
fprintf('               //   5:00  \n')
for i=1:length(T)
    fprintf('%2.f h  %2.f min',floor(T(i)),round(mod(T(i)*60,60)))
    fprintf('   //  ')
    fprintf('%2.f:%02.f\n',floor(S(i)),round(mod(S(i)*60,60)))
end

% Calculate and print mean waiting time in minutes
meanT = mean(T*60);
fprintf('\nMean waiting time is %4.1f minutes\n\n',meanT)

% Generate a random time at which the birder shows up
R = rand(1,10)*(max(S)-5)+5;

% For each birder, find the time of the next hawk sighting
for i=1:length(R)
   v = min(S(S>R(i))); %time of next sighting
   D(i) = v - R(i);    %time of next sighting - time of birder arrival
end

% Display arrival and waiting times
fprintf('10 Birders randomly show up\n')
fprintf('\nArrival Times  //  Waiting Times\n')
for i=1:length(T)
    fprintf('   %02.f:%02.f',floor(R(i)),round(mod(R(i)*60,60)))
    fprintf('       //  ')
    fprintf('%3.f min\n',D(i)*60)
end

% Calculate and print mean waiting time in minutes of the 10 birders
meanD = mean(D*60);
fprintf('\nMean waiting time of 10 random birders is %4.1f minutes\n\n',meanD)