function ev = heads3(n)
%HEADS3: MATLAB function M-file that simulates
%flipping a coin until it turns up heads. The
%input, n, is number of trials, and the 
%output, ev, is expected value.
clear R;
for k=1:n
    flag=0;
    flip1=round(rand);
    flip2=round(rand);
    flip3=round(rand);
    m=3;    %m counts the number of flips
    while(flag==0)
        if (flip1==1 && flip2==1 && flip3 == 1) %if all three heads
            flag=1;
        else
            m=m+1;
        flip1=flip2;
        flip2=flip3;
        flip3=round(rand);
        end
    end
R(k)=m;
end
ev=mean(R);
        