%
% add2sortedlist.m
%
% function to take a given array, and add a given number to it,
% in sorted order.
%
% a = add2sortedlist([1, 7, 10], 5);
% a would then become [1, 5, 7, 10]
%
%assuming array is sorted
function ar= add2sortedlist(inputarray, inputnum)

% Initialize our variables
ar = [];      % return array 
counter = 1;  % array position
n = 0;        % position that inputnum should go 
 
% Examine each number compared to inputnum
while(counter<= length(inputarray))
    % update n as needed
    if (inputarray(counter) < inputnum)
        % update n 
        location = 'after';
        n = counter;
    else
        location = 'before';
    end
    % consider commenting out the 2 lines below.
    s = sprintf('At index %d is %d %s', counter,inputarray(counter), ...
        location);
    disp(s)
    counter = counter + 1;
    
end
% disp(n);
% Create a new array with inputnum in the appropriate spot.
ar = [inputarray(1:n), inputnum, inputarray(n+1:length(inputarray))];

