function [Counts_out,BinCenters_out]=HW8_Solution_myHist(X,n)

if nargin <2
    n=10; %This is the default for 'hist'
end

%These are the bin boundaries
m=linspace(min(X),max(X),n+1);

%These are bin centers:
BinCenters=(m(2)-m(1))/2 + m(1:end-1);

%This records counts in each bin
Counts=zeros(1,n);

for ii=1:n
    if ii==n %special condition at the end point to get last point
        Counts(ii)=sum(X<=(m(ii+1)) & X>=(m(ii)));
    else
        Counts(ii)=sum(X<(m(ii+1)) & X>=(m(ii)));
    end
end


switch nargout
    case 0 %If no output is requested, make plot. 
    h=bar(BinCenters,Counts);
    h.BarWidth=1; %This makes bars look the same as in hist. 
    
    %Only creating outputs when asked for prevents displaying
    %them to command line when function is called without semicolon.
    
    case 1 %make first output 
        Counts_out=Counts;
    case 2 %make first, second output
        Counts_out=Counts;
        BinCenters_out=BinCenters;
end



end