n=input('enter the number of coin tosses per trial') nt=input('enter the number of trials') A=rand(n,nt)>0.5; %give you a matrix of zeros and ones %each column represents a trial with n coin tosses sumo=0; %compute average maxstrkln for jj=1:nt maxstrkln=0; %the maximum winning streak for a trial winstrkln=0;%local winning streak which changes as you go through the n tosses for ii=1:n %through column jj (which is trial jj) if A(ii,jj)==1 winstrkln=winstrkln+1;%keep track of the number of consecutive ones else%if you get a zero then the winning streak is over %see if it is the best winning streak so far if maxstrkln < winstrkln maxstrkln=winstrkln; end winstrkln=0;%when at zero update this for the next set of consecutive ones end end if maxstrkln < winstrkln%if you end a trial with a one instead of a zero %need to update maxstrkln maxstrkln=winstrkln; end sumo=maxstrkln+sumo;%to find the average maxstrkln end av=sumo/nt